-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generating supportedLocales array in l10n.dart
Resolves #2.
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
} |