Skip to content

Commit

Permalink
updated example project
Browse files Browse the repository at this point in the history
  • Loading branch information
jorre127 committed Sep 26, 2022
1 parent c26326c commit 07c36cf
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 50 deletions.
7 changes: 6 additions & 1 deletion example/assets/localization/en.json
Expand Up @@ -5,5 +5,10 @@
"test_arg3": "Testing argument %1$s %2$d",
"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"
"test_new_line_carriage_return": "Carriage\r\nReturn",
"test_non_positional": "Testing non positional argument %s and %.02f",
"test_plural": {
"one": "%d hour",
"other": "%d hours"
}
}
7 changes: 6 additions & 1 deletion example/assets/localization/nl.json
Expand Up @@ -6,5 +6,10 @@
"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"
"test_new_line_carriage_return": "Carriage\r\nReturn",
"test_non_positional": "Test niet positioneel argument %s en %f",
"test_plural": {
"one": "%d uur",
"other": "%d uren"
}
}
11 changes: 7 additions & 4 deletions example/lib/screen/home_screen.dart
Expand Up @@ -31,12 +31,15 @@ class HomeScreen extends StatelessWidget {
child: const Text('Nederlands (Not translated)'),
onPressed: Provider.of<LocaleViewModel>(context).onSwitchToDutch,
),
const SizedBox(height: 32),
const SizedBox(height: 22),
Text(Localization.test),
Text(Localization.testArg1('string')),
Text(Localization.testArg2(1.0)),
Text(Localization.testArg3('string', 1.0)),
Text(Localization.testArg4('string', 1.0)),
Text(Localization.testArg2(1)),
Text(Localization.testArg3('string', 1)),
Text(Localization.testArg4('string', 1)),
Text(Localization.testNonPositional('string', 1)),
Text(Localization.testPlural(4, 4)),
Text(Localization.testPlural(1, 1)),
],
),
),
Expand Down
81 changes: 38 additions & 43 deletions example/lib/util/locale/localization.dart
Expand Up @@ -4,6 +4,8 @@ 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';
import 'package:intl/intl.dart';
import 'package:sprintf/sprintf.dart';

//============================================================//
//THIS FILE IS AUTO GENERATED. DO NOT EDIT//
Expand Down Expand Up @@ -75,53 +77,34 @@ class Localization {
(_localisedOverrideValues[key] ?? _localisedValues[key]) as String?;
if (value == null) return key;
if (args == null || args.isEmpty) return value;
var newValue = value;
// ignore: avoid_annotating_with_dynamic
args.asMap().forEach((index, dynamic arg) =>
newValue = _replaceWith(newValue, arg, index + 1));
return newValue;
return sprintf(value, args);
} catch (e) {
return '⚠$key⚠';
}
}

static String _nonPositionalT(String key, {List<dynamic>? args}) {
static String _plural(String key, {required num count, List<dynamic>? args}) {
try {
final value =
(_localisedOverrideValues[key] ?? _localisedValues[key]) as String?;
final value = (_localisedOverrideValues[key] ?? _localisedValues[key])
as Map<String, dynamic>?;
if (value == null) return key;
if (args == null || args.isEmpty) return value;
var newValue = value;
args.asMap().forEach(
// ignore: avoid_annotating_with_dynamic
(index, dynamic arg) => newValue = _replaceFirstWith(newValue, arg),
);
return newValue;

final pluralValue = Intl.plural(
count,
zero: value['zero'] as String?,
one: value['one'] as String?,
two: value['two'] as String?,
few: value['few'] as String?,
many: value['many'] as String?,
other: value['other'] as String,
);
if (args == null || args.isEmpty) return pluralValue;
return sprintf(pluralValue, args);
} catch (e) {
return '⚠$key⚠';
}
}

static String _replaceWith(String value, Object? arg, int argIndex) {
if (arg == null) return value;
if (arg is String) {
return value.replaceAll('%$argIndex\$s', arg);
} else if (arg is num) {
return value.replaceAll('%$argIndex\$d', '$arg');
}
return value;
}

static String _replaceFirstWith(String value, Object? arg) {
if (arg == null) return value;
if (arg is String) {
return value.replaceFirst('%s', arg);
} else if (arg is num) {
return value.replaceFirst('%d', '$arg');
}
return value;
}

/// Translations:
///
/// nl: **'Test in het Nederlands'**
Expand All @@ -142,23 +125,23 @@ class Localization {
/// nl: **'Test argument [arg1 number]'**
///
/// en: **'Testing argument [arg1 number]'**
static String testArg2(num arg1) =>
static String testArg2(int arg1) =>
_t(LocalizationKeys.testArg2, args: <dynamic>[arg1]);

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

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

/// Translations:
Expand All @@ -173,7 +156,7 @@ class Localization {
/// nl: **'Test\nargument\n\n[arg1 string] [arg2 number] [arg1 string]'**
///
/// en: **'Testing\nargument\n\n[arg1 string] [arg2 number] [arg1 string]'**
static String testNewLine(String arg1, num arg2) =>
static String testNewLine(String arg1, int arg2) =>
_t(LocalizationKeys.testNewLine, args: <dynamic>[arg1, arg2]);

/// Translations:
Expand All @@ -184,10 +167,22 @@ class Localization {
static String get testNewLineCarriageReturn =>
_t(LocalizationKeys.testNewLineCarriageReturn);

/// Translations:
///
/// nl: **'Test niet positioneel argument %s en %f'**
///
/// en: **'Testing non positional argument %s and %.02f'**
static String testNonPositional(String arg1, double arg2) =>
_t(LocalizationKeys.testNonPositional, args: <dynamic>[arg1, arg2]);

/// Translations:
///
/// nl: **'{one: %d uur, other: %d uren}'**
///
/// en: **'{one: %d hour, other: %d hours}'**
static String testPlural(num count, int arg1) =>
_plural(LocalizationKeys.testPlural, count: count, args: <dynamic>[arg1]);

static String getTranslation(String key, {List<dynamic>? args}) =>
_t(key, args: args ?? <dynamic>[]);

static String getTranslationNonPositional(String key,
{List<dynamic>? args}) =>
_nonPositionalT(key, args: args ?? <dynamic>[]);
}
14 changes: 14 additions & 0 deletions example/lib/util/locale/localization_keys.dart
Expand Up @@ -57,4 +57,18 @@ class LocalizationKeys {
///
/// nl: **'Carriage\r\nReturn'**
static const testNewLineCarriageReturn = 'test_new_line_carriage_return';

/// Translations:
///
/// en: **'Testing non positional argument %s and %.02f'**
///
/// nl: **'Test niet positioneel argument %s en %f'**
static const testNonPositional = 'test_non_positional';

/// Translations:
///
/// en: **'{one: %d hour, other: %d hours}'**
///
/// nl: **'{one: %d uur, other: %d uren}'**
static const testPlural = 'test_plural';
}
1 change: 1 addition & 0 deletions example/pubspec.yaml
Expand Up @@ -13,6 +13,7 @@ dependencies:
sdk: flutter
provider: ^5.0.0
shared_preferences: ^2.0.3
sprintf: ^6.0.2

dev_dependencies:
build_runner: ^1.11.5
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Expand Up @@ -8,7 +8,7 @@ environment:

dependencies:
http: ^0.13.3
locale_gen: ^7.0.0
locale_gen: ^8.0.0
path: ^1.8.0

dev_dependencies:
Expand Down

0 comments on commit 07c36cf

Please sign in to comment.