Skip to content

Commit

Permalink
Support for script code
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Aug 14, 2021
1 parent b948500 commit 6e6660e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 30 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,10 @@ Fix for #159
## [0.22.4]

Fix for #167
Dependencies upgrade
Dependencies upgrade

## [0.30.0]

Support for script code
Support for toml format
Fix for #173
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ To use this library, you must create a folder in your project's root: the `baseP
>
> /assets/locales
Inside this folder, you'll put the *json* or *yaml* files containing the translated keys. You have two options:
Inside this folder, you'll put the *json*, *yaml*, *xml* or *toml* files containing the translated keys. You have different options:

- If you want to specify the country code

> `basePath`/{languageCode}_{countryCode}.json
- If you want to specify the script code

> `basePath`/{languageCode}_{scriptCode}.json
- If you want to specify both

> `basePath`/{languageCode}_{scriptCode}_${countryCode}_.json
- otherwise

> `basePath`/{languageCode}.json
Expand Down
46 changes: 23 additions & 23 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ packages:
name: cli_util
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.3.3"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -105,7 +105,7 @@ packages:
name: cupertino_icons
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
version: "1.0.3"
fake_async:
dependency: transitive
description:
Expand All @@ -119,7 +119,7 @@ packages:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.1.2"
file:
dependency: transitive
description:
Expand All @@ -143,7 +143,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.22.4"
version: "0.30.0"
flutter_localizations:
dependency: transitive
description: flutter
Expand All @@ -170,21 +170,21 @@ packages:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.0"
version: "0.13.3"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
version: "3.0.1"
http_parser:
dependency: transitive
description:
Expand All @@ -198,7 +198,7 @@ packages:
name: integration_test
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2+2"
version: "1.0.2+3"
intl:
dependency: transitive
description:
Expand All @@ -212,7 +212,7 @@ packages:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.0.3"
js:
dependency: transitive
description:
Expand All @@ -226,7 +226,7 @@ packages:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.0.1"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -275,21 +275,21 @@ packages:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
version: "2.0.2"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.2"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.2"
path_provider_platform_interface:
dependency: transitive
description:
Expand All @@ -303,14 +303,14 @@ packages:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.3"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.0"
version: "1.11.1"
petitparser:
dependency: transitive
description:
Expand All @@ -331,7 +331,7 @@ packages:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
pool:
dependency: transitive
description:
Expand Down Expand Up @@ -366,7 +366,7 @@ packages:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.2.0"
shelf_packages_handler:
dependency: transitive
description:
Expand All @@ -380,14 +380,14 @@ packages:
name: shelf_static
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.1.0"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.0.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -532,7 +532,7 @@ packages:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.2.5"
xdg_directories:
dependency: transitive
description:
Expand All @@ -546,7 +546,7 @@ packages:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.2"
version: "5.1.2"
xml2json:
dependency: transitive
description:
Expand All @@ -562,5 +562,5 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0"
dart: ">=2.13.0 <3.0.0"
flutter: ">=2.0.0"
11 changes: 8 additions & 3 deletions lib/loaders/file_translation_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FileTranslationLoader extends TranslationLoader implements IFileContent {
final String fallbackFile;
final String basePath;
final bool useCountryCode;
final bool useScriptCode;
AssetBundle assetBundle = rootBundle;

Map<dynamic, dynamic> _decodedMap = Map();
Expand All @@ -31,6 +32,7 @@ class FileTranslationLoader extends TranslationLoader implements IFileContent {
{this.fallbackFile = "en",
this.basePath = "assets/flutter_i18n",
this.useCountryCode = false,
this.useScriptCode = false,
forcedLocale,
decodeStrategies}) {
this.forcedLocale = forcedLocale;
Expand Down Expand Up @@ -90,15 +92,18 @@ class FileTranslationLoader extends TranslationLoader implements IFileContent {
/// Compose the file name using the format languageCode_countryCode
@protected
String composeFileName() {
return "${locale!.languageCode}${composeCountryCode()}";
return "${locale!.languageCode}${_composeSuffixCode()}";
}

/// Return the country code to attach to the file name, if required
@protected
String composeCountryCode() {
String _composeSuffixCode() {
String countryCode = "";
if(useScriptCode && locale!.scriptCode != null) {
countryCode = "${countryCode}_${locale!.scriptCode}";
}
if (useCountryCode && locale!.countryCode != null) {
countryCode = "_${locale!.countryCode}";
countryCode = "${countryCode}_${locale!.countryCode}";
}
return countryCode;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/loaders/local_translation_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class LocalTranslationLoader extends FileTranslationLoader {
LocalTranslationLoader(
{basePath = "assets/flutter_i18n",
useCountryCode = false,
useScriptCode = false,
forcedLocale,
decodeStrategies})
: super(
basePath: basePath,
useCountryCode: useCountryCode,
useScriptCode: useScriptCode,
forcedLocale: forcedLocale,
decodeStrategies: decodeStrategies);

Expand Down
3 changes: 3 additions & 0 deletions lib/loaders/namespace_file_translation_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NamespaceFileTranslationLoader extends FileTranslationLoader {
final String fallbackDir;
final String basePath;
final bool useCountryCode;
final bool useScriptCode;
final List<String>? namespaces;
AssetBundle assetBundle = rootBundle;

Expand All @@ -18,11 +19,13 @@ class NamespaceFileTranslationLoader extends FileTranslationLoader {
this.fallbackDir = "en",
this.basePath = "assets/flutter_i18n",
this.useCountryCode = false,
this.useScriptCode = false,
forcedLocale,
decodeStrategies})
: super(
basePath: basePath,
useCountryCode: useCountryCode,
useScriptCode: useScriptCode,
forcedLocale: forcedLocale,
decodeStrategies: decodeStrategies) {
assert(namespaces != null);
Expand Down
1 change: 1 addition & 0 deletions lib/loaders/network_file_translation_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NetworkFileTranslationLoader extends FileTranslationLoader {
forcedLocale,
fallbackFile = "en",
useCountryCode = false,
useScriptCode = false,
decodeStrategies})
: super(
fallbackFile: fallbackFile,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ packages:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.2"
version: "5.1.2"
xml2json:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_i18n
description: i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json file!
version: 0.22.4
version: 0.30.0
homepage: https://github.com/ilteoood/flutter_i18n
issue_tracker: https://github.com/ilteoood/flutter_i18n/issues

Expand Down

0 comments on commit 6e6660e

Please sign in to comment.