Skip to content

Commit

Permalink
Merge pull request #87 from vanlooverenkoen/feature/AssetBundle
Browse files Browse the repository at this point in the history
Support loading from different bundle
  • Loading branch information
vanlooverenkoen authored Nov 14, 2022
2 parents fd44350 + 5e348d0 commit d0275b8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## [10.0.0] - 2022-11-14
- Updated the signature of `load` to support getting the bundle to load from. Falls back to `rootBubdle`

## [9.0.1] - 2022-10-06
### Updated
- Updated travis to linux from macOS
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ LocaleViewModel.localizationInstance.translation2;
LocaleViewModel.localizationInstance.translation3;
```

### Custom asset bundle
Since version *10.0.0* you can specify the bundle to load the assets from in the `load` function.
This can be used as an alternative to overriding translations, fetching them from the network, ...

### Migration steps <7.0.0 to >=7.0.0
With the newest version of locale_gen the context no longer needs to be provided when accessing the translations. This means there are a couple of breaking changes.

Expand Down
3 changes: 2 additions & 1 deletion example/lib/util/locale/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -68,7 +69,7 @@ class Localization {
await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString(
final jsonContent = await (bundle ?? rootBundle).loadString(
'assets/locale/${currentLocale.toLanguageTag()}.json',
cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/locale_gen_sb_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class LocaleGenSbWriter {
..writeln(' LocalizationOverrides? localizationOverrides,')
..writeln(' bool showLocalizationKeys = false,')
..writeln(' bool useCaching = true,')
..writeln(' AssetBundle? bundle,')
..writeln(' }) async {')
..writeln(' final currentLocale = locale ?? defaultLocale;')
..writeln(' this.locale = currentLocale;')
Expand All @@ -116,7 +117,7 @@ class LocaleGenSbWriter {
..writeln(' _localisedOverrideValues = overrideLocalizations;')
..writeln(' }')
..writeln(
" final jsonContent = await rootBundle.loadString('${params.assetsDir}\${currentLocale.toLanguageTag()}.json', cache: useCaching);")
" final jsonContent = await (bundle ?? rootBundle).loadString('${params.assetsDir}\${currentLocale.toLanguageTag()}.json', cache: useCaching);")
..writeln(
' _localisedValues = json.decode(jsonContent) as Map<String, dynamic>;')
..writeln(' }')
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: locale_gen
description: Dart tool that will convert your default locale json to dart code.
version: 9.0.1
version: 10.0.0
homepage: https://github.com/vanlooverenkoen/locale_gen

environment:
Expand Down
21 changes: 14 additions & 7 deletions test/locale_gen_sb_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -250,7 +251,7 @@ class Localization {
final overrideLocalizations = await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
final jsonContent = await (bundle ?? rootBundle).loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
}
Expand Down Expand Up @@ -326,6 +327,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -338,7 +340,7 @@ class Localization {
final overrideLocalizations = await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
final jsonContent = await (bundle ?? rootBundle).loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
}
Expand Down Expand Up @@ -426,6 +428,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -438,7 +441,7 @@ class Localization {
final overrideLocalizations = await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
final jsonContent = await (bundle ?? rootBundle).loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
}
Expand Down Expand Up @@ -535,6 +538,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -547,7 +551,7 @@ class Localization {
final overrideLocalizations = await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
final jsonContent = await (bundle ?? rootBundle).loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
}
Expand Down Expand Up @@ -645,6 +649,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -657,7 +662,7 @@ class Localization {
final overrideLocalizations = await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
final jsonContent = await (bundle ?? rootBundle).loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
}
Expand Down Expand Up @@ -757,6 +762,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -769,7 +775,7 @@ class Localization {
final overrideLocalizations = await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
final jsonContent = await (bundle ?? rootBundle).loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
}
Expand Down Expand Up @@ -865,6 +871,7 @@ class Localization {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
AssetBundle? bundle,
}) async {
final currentLocale = locale ?? defaultLocale;
this.locale = currentLocale;
Expand All @@ -877,7 +884,7 @@ class Localization {
final overrideLocalizations = await localizationOverrides.getOverriddenLocalizations(currentLocale);
_localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
final jsonContent = await (bundle ?? rootBundle).loadString('assets/locale/${currentLocale.toLanguageTag()}.json', cache: useCaching);
_localisedValues = json.decode(jsonContent) as Map<String, dynamic>;
}
Expand Down
16 changes: 16 additions & 0 deletions tool/test_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

echo "==="
echo "First create a file with all other files imported so flutter test coverage uses all files"
file=test/coverage_helper_test.dart
echo "// Helper file to make coverage work for all dart files\n" > $file
echo "// ignore_for_file: unused_import" >> $file
find lib -name '*.dart' | cut -c4- | awk -v package=$1 '{printf "import '\''package:%s%s'\'';\n", "locale_gen", $1}' >> $file
echo "" >> $file
echo "void main(){}" >> $file
echo "==="

flutter test --coverage || exit -1;

genhtml coverage/lcov.info -o coverage/html
open coverage/html/index.html

0 comments on commit d0275b8

Please sign in to comment.