Skip to content

Commit

Permalink
Merge b8e04e3 into c8d2644
Browse files Browse the repository at this point in the history
  • Loading branch information
vanlooverenkoen committed Oct 12, 2021
2 parents c8d2644 + b8e04e3 commit 3e22b15
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 45 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Changelog
## [4.4.1] - 2021-05-07
## [4.6.0] - 2021-10-12
### Updated
- Version bump to locale_gen [3.4.1](https://github.com/vanlooverenkoen/locale_gen/releases/tag/v3.4.1)
- Version bump to locale_gen [3.6.0](https://github.com/vanlooverenkoen/locale_gen/releases/tag/v3.6.0)
(Update translations at runtime is now available)

## [4.5.0] - 2021-10-06
### Updated
- Version bump to locale_gen [3.5.0](https://github.com/vanlooverenkoen/locale_gen/releases/tag/v3.5.0)

## [4.4.1] - 2021-05-07
### Updated
- Version bump to locale_gen [3.4.1](https://github.com/vanlooverenkoen/locale_gen/releases/tag/v3.4.1)

## [4.4.0] - 2021-05-06
### Updated
- Version bump to locale_gen [3.4.0](https://github.com/vanlooverenkoen/locale_gen/releases/tag/v3.4.0)
Expand Down
4 changes: 3 additions & 1 deletion example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ analyzer:
missing_return: error
todo: ignore
sdk_version_async_exported_from_core: ignore
strong-mode:
implicit-casts: false
implicit-dynamic: false
exclude:
- '**.g.dart'
- 'lib/util/locale/**'

linter:
rules:
Expand Down
6 changes: 4 additions & 2 deletions example/assets/localization/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"test": "Testing in english",
"test": "Testing in English",
"test_arg1": "Testing argument %1$s",
"test_arg2": "Testing argument %1$d",
"test_arg3": "Testing argument %1$s %2$d",
"test_arg4": "Testing argument %1$s %2$d %1$s"
"test_arg4": "Testing argument %1$s %2$d %1$s",
"test_new_line": "Testing\nargument\n\n%1$s %2$d %1$s",
"test_new_line_carriage_return": "Carriage\r\nReturn"
}
5 changes: 4 additions & 1 deletion example/assets/localization/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"test_arg1": "Test argument %1$s",
"test_arg2": "Test argument %1$d",
"test_arg3": "Test argument %1$s %2$d",
"test_arg4": "Test argument %1$s %2$d %1$s"
"test_arg4": "Test argument %1$s %2$d %1$s",
"welcome_message": "Hallo daar",
"test_new_line": "Test\nargument\n\n%1$s %2$d %1$s",
"test_new_line_carriage_return": "Carriage\r\nReturn"
}
9 changes: 5 additions & 4 deletions example/lib/screen/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@ class HomeScreen extends StatelessWidget {
appBar: AppBar(
title: const Text('icapps translations'),
backgroundColor: Colors.black45,
brightness: Brightness.dark,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
MaterialButton(
child: const Text('System Language (Not translated)'),
onPressed: Provider.of<LocaleViewModel>(context)
.onSwitchToSystemLanguage,
),
TextButton(
MaterialButton(
child: const Text('English (Not translated)'),
onPressed:
Provider.of<LocaleViewModel>(context).onSwitchToEnglish,
),
TextButton(
MaterialButton(
child: const Text('Nederlands (Not translated)'),
onPressed: Provider.of<LocaleViewModel>(context).onSwitchToDutch,
),
Container(height: 32),
const SizedBox(height: 32),
Text(Localization.of(context).test),
Text(Localization.of(context).testArg1('string')),
Text(Localization.of(context).testArg2(1.0)),
Expand Down
103 changes: 82 additions & 21 deletions example/lib/util/locale/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,64 @@ import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:icapps_translations_example/util/locale/localization_keys.dart';
import 'package:icapps_translations_example/util/locale/localization_overrides.dart';

//============================================================//
//THIS FILE IS AUTO GENERATED. DO NOT EDIT//
//============================================================//
class Localization {
Map<String, dynamic> _localisedValues = Map();
var _localisedValues = <String, dynamic>{};
var _localisedOverrideValues = <String, dynamic>{};

static Localization of(BuildContext context) =>
Localizations.of<Localization>(context, Localization)!;

static Future<Localization> load(Locale locale,
{bool showLocalizationKeys = false}) async {
final localizations = Localization();
/// The locale is used to get the correct json locale.
/// It can later be used to check what the locale is that was used to load this Localization instance.
final Locale locale;

Localization({required this.locale});

static Future<Localization> load(
Locale locale, {
LocalizationOverrides? localizationOverrides,
bool showLocalizationKeys = false,
bool useCaching = true,
}) async {
final localizations = Localization(locale: locale);
if (showLocalizationKeys) {
return localizations;
}
final jsonContent = await rootBundle
.loadString('assets/localization/${locale.languageCode}.json');
// ignore: avoid_as
if (localizationOverrides != null) {
final overrideLocalizations =
await localizationOverrides.getOverriddenLocalizations(locale);
localizations._localisedOverrideValues = overrideLocalizations;
}
final jsonContent = await rootBundle.loadString(
'assets/localization/${locale.languageCode}.json',
cache: useCaching);
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;
if (value == null) return '$key';
final value =
(_localisedOverrideValues[key] ?? _localisedValues[key]) as String?;
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;
// ignore: avoid_annotating_with_dynamic
args.asMap().forEach((index, dynamic arg) =>
newValue = _replaceWith(newValue, arg, index + 1));
return newValue;
} catch (e) {
return '⚠$key⚠';
}
}

String _replaceWith(String value, arg, argIndex) {
String _replaceWith(String value, Object? arg, int argIndex) {
if (arg == null) return value;
if (arg is String) {
return value.replaceAll('%$argIndex\$s', arg);
Expand All @@ -52,18 +70,61 @@ class Localization {
return value;
}

/// Translations:
///
/// en: **'Testing in English'**
///
/// nl: **'Test in het Nederlands'**
String get test => _t(LocalizationKeys.test);

String testArg1(String arg1) => _t(LocalizationKeys.testArg1, args: [arg1]);
/// Translations:
///
/// en: **'Testing argument [arg1 string]'**
///
/// nl: **'Test argument [arg1 string]'**
String testArg1(String arg1) =>
_t(LocalizationKeys.testArg1, args: <dynamic>[arg1]);

String testArg2(num arg1) => _t(LocalizationKeys.testArg2, args: [arg1]);
/// Translations:
///
/// en: **'Testing argument [arg1 number]'**
///
/// nl: **'Test argument [arg1 number]'**
String testArg2(num arg1) =>
_t(LocalizationKeys.testArg2, args: <dynamic>[arg1]);

/// Translations:
///
/// en: **'Testing argument [arg1 string] [arg2 number]'**
///
/// nl: **'Test argument [arg1 string] [arg2 number]'**
String testArg3(String arg1, num arg2) =>
_t(LocalizationKeys.testArg3, args: [arg1, arg2]);
_t(LocalizationKeys.testArg3, args: <dynamic>[arg1, arg2]);

/// Translations:
///
/// en: **'Testing argument [arg1 string] [arg2 number] [arg1 string]'**
///
/// nl: **'Test argument [arg1 string] [arg2 number] [arg1 string]'**
String testArg4(String arg1, num arg2) =>
_t(LocalizationKeys.testArg4, args: [arg1, arg2]);
_t(LocalizationKeys.testArg4, args: <dynamic>[arg1, arg2]);

/// Translations:
///
/// en: **'Testing\nargument\n\n[arg1 string] [arg2 number] [arg1 string]'**
///
/// nl: **'Test\nargument\n\n[arg1 string] [arg2 number] [arg1 string]'**
String testNewLine(String arg1, num arg2) =>
_t(LocalizationKeys.testNewLine, args: <dynamic>[arg1, arg2]);

/// Translations:
///
/// en: **'Carriage\r\nReturn'**
///
/// nl: **'Carriage\r\nReturn'**
String get testNewLineCarriageReturn =>
_t(LocalizationKeys.testNewLineCarriageReturn);

String getTranslation(String key, {List<dynamic>? args}) =>
_t(key, args: args ?? []);
_t(key, args: args ?? <dynamic>[]);
}
54 changes: 42 additions & 12 deletions example/lib/util/locale/localization_delegate.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:icapps_translations_example/util/locale/localization.dart';
import 'package:icapps_translations_example/util/locale/localization_overrides.dart';

//============================================================//
//THIS FILE IS AUTO GENERATED. DO NOT EDIT//
//============================================================//

typedef LocaleFilter = bool Function(String languageCode);

class LocalizationDelegate extends LocalizationsDelegate<Localization> {
static const defaultLocale = Locale('nl');
static const supportedLanguages = [
'en',
'nl',
];
static LocaleFilter? localeFilter;
static const defaultLocale = Locale.fromSubtags(
languageCode: 'nl', scriptCode: null, countryCode: null);

static const supportedLocales = [
Locale('en'),
Locale('nl'),
static const _supportedLocales = [
Locale.fromSubtags(languageCode: 'en', scriptCode: null, countryCode: null),
Locale.fromSubtags(languageCode: 'nl', scriptCode: null, countryCode: null),
];

static List<String> get supportedLanguages {
final supportedLanguageTags =
_supportedLocales.map((e) => e.toLanguageTag()).toList(growable: false);
if (localeFilter == null) return supportedLanguageTags;
return supportedLanguageTags
.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();
}

LocalizationOverrides? localizationOverrides;
Locale? newLocale;
Locale? activeLocale;
final bool useCaching;
bool showLocalizationKeys;

LocalizationDelegate({this.newLocale, this.showLocalizationKeys = false}) {
LocalizationDelegate({
this.newLocale,
this.localizationOverrides,
this.showLocalizationKeys = false,
this.useCaching = !kDebugMode,
}) {
if (newLocale != null) {
activeLocale = newLocale;
}
Expand All @@ -36,8 +62,12 @@ class LocalizationDelegate extends LocalizationsDelegate<Localization> {
Future<Localization> load(Locale locale) async {
final newActiveLocale = newLocale ?? locale;
activeLocale = newActiveLocale;
return Localization.load(newActiveLocale,
showLocalizationKeys: showLocalizationKeys);
return Localization.load(
newActiveLocale,
localizationOverrides: localizationOverrides,
showLocalizationKeys: showLocalizationKeys,
useCaching: useCaching,
);
}

@override
Expand Down
39 changes: 39 additions & 0 deletions example/lib/util/locale/localization_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,52 @@
//THIS FILE IS AUTO GENERATED. DO NOT EDIT//
//============================================================//
class LocalizationKeys {
/// Translations:
///
/// en: **'Testing in English'**
///
/// nl: **'Test in het Nederlands'**
static const test = 'test';

/// Translations:
///
/// en: **'Testing argument [arg1 string]'**
///
/// nl: **'Test argument [arg1 string]'**
static const testArg1 = 'test_arg1';

/// Translations:
///
/// en: **'Testing argument [arg1 number]'**
///
/// nl: **'Test argument [arg1 number]'**
static const testArg2 = 'test_arg2';

/// Translations:
///
/// en: **'Testing argument [arg1 string] [arg2 number]'**
///
/// nl: **'Test argument [arg1 string] [arg2 number]'**
static const testArg3 = 'test_arg3';

/// Translations:
///
/// en: **'Testing argument [arg1 string] [arg2 number] [arg1 string]'**
///
/// nl: **'Test argument [arg1 string] [arg2 number] [arg1 string]'**
static const testArg4 = 'test_arg4';

/// Translations:
///
/// en: **'Testing\nargument\n\n[arg1 string] [arg2 number] [arg1 string]'**
///
/// nl: **'Test\nargument\n\n[arg1 string] [arg2 number] [arg1 string]'**
static const testNewLine = 'test_new_line';

/// Translations:
///
/// en: **'Carriage\r\nReturn'**
///
/// nl: **'Carriage\r\nReturn'**
static const testNewLineCarriageReturn = 'test_new_line_carriage_return';
}
10 changes: 10 additions & 0 deletions example/lib/util/locale/localization_overrides.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:flutter/widgets.dart';

//============================================================//
//THIS FILE IS AUTO GENERATED. DO NOT EDIT//
//============================================================//
abstract class LocalizationOverrides {
Future<void> refreshOverrideLocalizations();

Future<Map<String, dynamic>> getOverriddenLocalizations(Locale locale);
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: icapps_translations
description: Dart tool to generate flutter translations code from the icapps translations tool
version: 4.5.0
version: 4.6.0
homepage: https://github.com/icapps/flutter-icapps-translations

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
http: ^0.13.3
locale_gen: ^3.5.0
locale_gen: ^3.6.1
path: ^1.8.0

dev_dependencies:
Expand Down

0 comments on commit 3e22b15

Please sign in to comment.