Skip to content

Commit

Permalink
Add generating supportedLocales array in l10n.dart
Browse files Browse the repository at this point in the history
Resolves #2.
  • Loading branch information
Albert221 committed Jun 22, 2020
1 parent 47c2f9f commit 4bbfa5d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/src/generator/l10n_dart_generator.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:arbify/src/language_identifier_parser/locale.dart';
import 'package:arbify/src/language_identifier_parser/locale_parser.dart';

import '../icu_parser/icu_parser.dart';
import '../arb_parser/arb_file.dart';

class L10nDartGenerator {
const L10nDartGenerator();

String generate(ArbFile template, List<String> locales) {
final localeItems = locales.map((locale) => "\n '$locale',").join();
final messagesBuilder = StringBuffer();

template.messages.forEach((message) {
Expand Down Expand Up @@ -50,6 +52,13 @@ class L10nDartGenerator {

final messages = messagesBuilder.toString();

final parsedLocales = locales
.map((locale) => LanguageIdentifierParser().parse(locale))
.toList();
final supportedLocales = _generateSupportedLocalesArray(parsedLocales);
final localeItems =
parsedLocales.map((locale) => "\n '${locale.language}',").join();

return """// File generated with arbify_flutter.
// DO NOT MODIFY BY HAND.
// ignore_for_file: lines_longer_than_80_chars, non_constant_identifier_names
Expand Down Expand Up @@ -82,6 +91,9 @@ class S {
class ArbifyLocalizationsDelegate extends LocalizationsDelegate<S> {
const ArbifyLocalizationsDelegate();
List<Locale> get supportedLocales => [
$supportedLocales ];
@override
bool isSupported(Locale locale) => [$localeItems
].contains(locale.languageCode);
Expand All @@ -94,4 +106,21 @@ class ArbifyLocalizationsDelegate extends LocalizationsDelegate<S> {
}
""";
}

String _generateSupportedLocalesArray(List<Locale> locales) {
final supportedLocales = StringBuffer();

locales.forEach((locale) {
final languageCode = "languageCode: '${locale.language}'";
final scriptCode =
locale.script == null ? '' : ", scriptCode: '${locale.script}'";
final countryCode =
locale.region == null ? '' : ", countryCode: '${locale.region}'";

supportedLocales.writeln(
' Locale.fromSubtags($languageCode$scriptCode$countryCode),');
});

return supportedLocales.toString();
}
}
7 changes: 7 additions & 0 deletions lib/src/language_identifier_parser/locale.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Locale {
final String language;
final String script;
final String region;

Locale({this.language, this.script, this.region});
}
25 changes: 25 additions & 0 deletions lib/src/language_identifier_parser/locale_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://www.unicode.org/reports/tr35/#Unicode_language_identifier
import 'package:arbify/src/language_identifier_parser/locale.dart';
import 'package:petitparser/petitparser.dart';

class LanguageIdentifierParser {
Parser get alphanum => letter() | digit();
Parser get sep => char('_') | char('-');

Parser get language =>
(letter().repeat(2, 3) | letter().repeat(5, 8)).flatten();

Parser get script => letter().times(4).flatten();
Parser get region => (letter().times(2) | digit().times(3)).flatten();

Parser get scriptPart => (sep & script).map((value) => value[1]).optional();
Parser get regionPart => (sep & region).map((value) => value[1]).optional();

Parser get id => language & scriptPart & regionPart;

Locale parse(String text) => id
.map((value) =>
Locale(language: value[0], script: value[1], region: value[2]))
.parse(text)
.value;
}
39 changes: 39 additions & 0 deletions test/locale_parser_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:arbify/src/language_identifier_parser/locale_parser.dart';
import 'package:test/test.dart';

void main() {
test('parses en-US', () {
final locale = LanguageIdentifierParser().parse('en-US');
expect(locale.language, equals('en'));
expect(locale.script, isNull);
expect(locale.region, equals('US'));
});

test('parses en_GB', () {
final locale = LanguageIdentifierParser().parse('en_GB');
expect(locale.language, equals('en'));
expect(locale.script, isNull);
expect(locale.region, equals('GB'));
});

test('parses es-419', () {
final locale = LanguageIdentifierParser().parse('es-419');
expect(locale.language, equals('es'));
expect(locale.script, isNull);
expect(locale.region, equals('419'));
});

test('parses uz-Cyrl', () {
final locale = LanguageIdentifierParser().parse('uz-Cyrl');
expect(locale.language, equals('uz'));
expect(locale.script, equals('Cyrl'));
expect(locale.region, isNull);
});

test('parses zn-Hans-TW', () {
final locale = LanguageIdentifierParser().parse('zn-Hans-TW');
expect(locale.language, equals('zn'));
expect(locale.script, equals('Hans'));
expect(locale.region, equals('TW'));
});
}

0 comments on commit 4bbfa5d

Please sign in to comment.