Skip to content

Commit

Permalink
Merge pull request #35 from vanlooverenkoen/feature/#34-skip-language…
Browse files Browse the repository at this point in the history
…-at-runtime

#34: Added the implementation for skipping a language at runtime
  • Loading branch information
vanlooverenkoen authored Mar 26, 2021
2 parents 250a032 + 143b7a9 commit 37fa3a4
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [3.1.0] - 2021-03-25
### Added
-#34 Support for skipping a locale at runtime. example: only use dutch in alpha but not in beta & production

## [3.0.0] - 2021-03-07
### Added
-#32 Nullsafety implementation. Support for flutter 2.0 & dart 2.12.0
Expand Down
1 change: 1 addition & 0 deletions example/lib/screen/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class HomeScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
brightness: Brightness.dark,
title: const Text('locale gen'),
),
body: Center(
Expand Down
14 changes: 6 additions & 8 deletions example/lib/util/locale/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@ class Localization {
}
final jsonContent = await rootBundle
.loadString('assets/locale/${locale.languageCode}.json');
// ignore: avoid_as
localizations._localisedValues =
json.decode(jsonContent) as Map<String, dynamic>;
json.decode(jsonContent) as Map<String, dynamic>; // ignore: avoid_as
return localizations;
}

String _t(String key, {List<dynamic>? args}) {
try {
// ignore: avoid_as
var value = _localisedValues[key] as String;
final value = _localisedValues[key] as String?; // ignore: avoid_as
if (value == null) return '$key';
if (args == null || args.isEmpty) return value;
args
.asMap()
.forEach((index, arg) => value = _replaceWith(value, arg, index + 1));
return value;
var newValue = value;
args.asMap().forEach(
(index, arg) => newValue = _replaceWith(newValue, arg, index + 1));
return newValue;
} catch (e) {
return '⚠$key⚠';
}
Expand Down
22 changes: 20 additions & 2 deletions example/lib/util/locale/localization_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@ import 'package:locale_gen_example/util/locale/localization.dart';
//============================================================//
//THIS FILE IS AUTO GENERATED. DO NOT EDIT//
//============================================================//

typedef LocaleFilter = bool Function(String languageCode);

class LocalizationDelegate extends LocalizationsDelegate<Localization> {
static LocaleFilter? localeFilter;
static const defaultLocale = Locale('en');
static const supportedLanguages = [
static const _supportedLanguages = [
'en',
'nl',
];

static const supportedLocales = [
static const _supportedLocales = [
Locale('en'),
Locale('nl'),
];

static List<String> get supportedLanguages {
if (localeFilter == null) return _supportedLanguages;
return _supportedLanguages
.where((element) => localeFilter?.call(element) ?? true)
.toList();
}

static List<Locale> get supportedLocales {
if (localeFilter == null) return _supportedLocales;
return _supportedLocales
.where((element) => localeFilter?.call(element.languageCode) ?? true)
.toList();
}

Locale? newLocale;
Locale? activeLocale;
bool showLocalizationKeys;
Expand Down
33 changes: 24 additions & 9 deletions lib/src/locale_gen_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ class LocaleGenWriter {
..writeln(' }')
..writeln(
" final jsonContent = await rootBundle.loadString('${params.assetsDir}\${locale.languageCode}.json');")
..writeln(' // ignore: avoid_as')
..writeln(
' localizations._localisedValues = json.decode(jsonContent) as Map<String, dynamic>;')
' localizations._localisedValues = json.decode(jsonContent) as Map<String, dynamic>; // ignore: avoid_as')
..writeln(' return localizations;')
..writeln(' }')
..writeln()
..writeln(' String _t(String key, {List<dynamic>? args}) {')
..writeln(' try {')
..writeln(' // ignore: avoid_as')
..writeln(' var value = _localisedValues[key] as String;')
..writeln(
' final value = _localisedValues[key] as String?; // ignore: avoid_as')
..writeln(" if (value == null) return '\$key';")
..writeln(' if (args == null || args.isEmpty) return value;')
..writeln(' var newValue = value;')
..writeln(
' args.asMap().forEach((index, arg) => value = _replaceWith(value, arg, index + 1));')
..writeln(' return value;')
' args.asMap().forEach((index, arg) => newValue = _replaceWith(newValue, arg, index + 1));')
..writeln(' return newValue;')
..writeln(' } catch (e) {')
..writeln(" return '⚠\$key⚠';")
..writeln(' }')
Expand Down Expand Up @@ -147,21 +147,37 @@ class LocaleGenWriter {
..writeln('//THIS FILE IS AUTO GENERATED. DO NOT EDIT//')
..writeln(
'//============================================================//')
..writeln()
..writeln('typedef LocaleFilter = bool Function(String languageCode);')
..writeln()
..writeln(
'class LocalizationDelegate extends LocalizationsDelegate<Localization> {')
..writeln(' static LocaleFilter? localeFilter;')
..writeln(
" static const defaultLocale = Locale('${params.defaultLanguage}');")
..writeln(' static const supportedLanguages = [');
..writeln(' static const _supportedLanguages = [');
params.languages.forEach((language) => sb.writeln(" '$language',"));
sb
..writeln(' ];')
..writeln()
..writeln(' static const supportedLocales = [');
..writeln(' static const _supportedLocales = [');
params.languages
.forEach((language) => sb.writeln(" Locale('$language'),"));
sb
..writeln(' ];')
..writeln()
..writeln(' static List<String> get supportedLanguages {')
..writeln(' if (localeFilter == null) return _supportedLanguages;')
..writeln(
' return _supportedLanguages.where((element) => localeFilter?.call(element) ?? true).toList();')
..writeln(' }')
..writeln()
..writeln(' static List<Locale> get supportedLocales {')
..writeln(' if (localeFilter == null) return _supportedLocales;')
..writeln(
' return _supportedLocales.where((element) => localeFilter?.call(element.languageCode) ?? true).toList();')
..writeln(' }')
..writeln()
..writeln(' Locale? newLocale;')
..writeln(' Locale? activeLocale;')
..writeln(' bool showLocalizationKeys;')
Expand All @@ -188,7 +204,6 @@ class LocaleGenWriter {
..writeln(' @override')
..writeln(
' bool shouldReload(LocalizationsDelegate<Localization> old) => true;')
..writeln()
..writeln('}');

// Write to file
Expand Down
5 changes: 2 additions & 3 deletions 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: 3.0.0
version: 3.1.0
homepage: https://github.com/vanlooverenkoen/locale_gen

environment:
Expand All @@ -13,5 +13,4 @@ dependencies:

dev_dependencies:
flutter_test:
sdk: flutter
test: ^1.16.5
sdk: flutter
2 changes: 1 addition & 1 deletion test/format_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:locale_gen/src/translation_writer.dart';
import 'package:test/test.dart';

void main() {
group('Tests without arguments', () {
Expand Down

0 comments on commit 37fa3a4

Please sign in to comment.