Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#1 #2

Merged
merged 3 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [1.0.1] - 2020-02-18
### Removed
- Removed ⚠ before and after keys.
### Added
- Support for `\n`

## [1.0.0] - 2020-02-10
### Added
-Added support for getting the Localization.keys
Expand Down
6 changes: 4 additions & 2 deletions bin/icapps_translations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Future<void> parsePubspec(File pubspecYaml) async {
Future<void> _buildJson(String language) async {
print('Updating $language...');
final headers = Map<String, String>()
..putIfAbsent('Content-type', () => 'application/json')
..putIfAbsent('Authorization', () => 'Token token=${params.apiKey}');
final url = '$baseUrl$language.json';
final response = await http.get(url, headers: headers);
Expand All @@ -59,7 +60,8 @@ Future<void> _buildJson(String language) async {
}
final file = File(join(Directory.current.path, assetsDir, '$language.json'));
const encoder = JsonEncoder.withIndent(' ');
final body = json.decode(response.body);
final changedBody = response.body.replaceAll(r'\\n', r'\n');
final body = json.decode(changedBody);
final translations = body['translations'] ?? Map<String, dynamic>();
file.writeAsStringSync(encoder.convert(translations));
if (language == params.defaultLanguage) {
Expand Down Expand Up @@ -130,7 +132,7 @@ void createLocalizationFile() {
..writeln(' String _t(String key, {List<dynamic> args}) {')
..writeln(' try {')
..writeln(' String value = _localisedValues[key];')
..writeln(" if (value == null) return '\$key';")
..writeln(" if (value == null) return '\$key';")
..writeln(' if (args == null || args.isEmpty) return value;')
..writeln(
' args.asMap().forEach((index, arg) => value = _replaceWith(value, arg, index + 1));')
Expand Down
4 changes: 3 additions & 1 deletion example/.flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# This is a generated file; do not edit or check into version control.
shared_preferences=/Users/vanlooverenkoen/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.3+4/
shared_preferences=/Users/hannesvandenberghe/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.6+1/
shared_preferences_macos=/Users/hannesvandenberghe/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_macos-0.0.1+5/
shared_preferences_web=/Users/hannesvandenberghe/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_web-0.1.2+3/
2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"shared_preferences","dependencies":[]}]}
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]}]}
6 changes: 3 additions & 3 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/vanlooverenkoen/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/vanlooverenkoen/Documents/work/flutter-icapps-translations/example"
export "FLUTTER_ROOT=/Users/hannesvandenberghe/Developer/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/hannesvandenberghe/Developer/FlutFlut/flutter-icapps-translations/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_FRAMEWORK_DIR=/Users/vanlooverenkoen/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_FRAMEWORK_DIR=/Users/hannesvandenberghe/Developer/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
2 changes: 1 addition & 1 deletion example/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MyApp extends StatelessWidget {
home: HomeScreen(),
),
),
builder: (context) => kiwi.Container().resolve()..init(),
create: (context) => kiwi.Container().resolve()..init(),
);
}
}
5 changes: 2 additions & 3 deletions example/lib/util/locale/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Localization {
String _t(String key, {List<dynamic> args}) {
try {
String value = _localisedValues[key];
if (value == null) return '$key';
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;
Expand All @@ -45,7 +45,6 @@ class Localization {
return value;
}

String get appTitle => _t(LocalizationKeys.appTitle);
String getTranslation(String key, {List<dynamic> args}) => _t(key, args: args ?? List());

String get welcomeMessage => _t(LocalizationKeys.welcomeMessage);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: icapps_translations
description: Dart tool to generate flutter translations code from the icapps translations tool
version: 1.0.0
version: 1.0.1
homepage: https://github.com/icapps/flutter-icapps-translations

environment:
Expand Down
32 changes: 16 additions & 16 deletions test/test_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,83 @@ void main() {
test('Test null translations', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', null);
expect(sb.toString(), equals(" String get appTitle => _t('app_title');\n\n"));
expect(sb.toString(), equals(' String get appTitle => _t(LocalizationKeys.appTitle);\n\n'));
});
test('Test empty translations', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', '');
expect(sb.toString(), equals(" String get appTitle => _t('app_title');\n\n"));
expect(sb.toString(), equals(' String get appTitle => _t(LocalizationKeys.appTitle);\n\n'));
});
test('Test translations without arguments', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo');
expect(sb.toString(), equals(" String get appTitle => _t('app_title');\n\n"));
expect(sb.toString(), equals(' String get appTitle => _t(LocalizationKeys.appTitle);\n\n'));
});
});

group('Tests with string arguments', () {
test('Test translations with 1 string argument', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$s');
expect(sb.toString(), equals(" String appTitle(String arg1) => _t('app_title', args: [arg1]);\n\n"));
expect(sb.toString(), equals(" String appTitle(String arg1) => _t(LocalizationKeys.appTitle, args: [arg1]);\n\n"));
});

test('Test translations with 2 string arguments', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$s %2\$s');
expect(sb.toString(), equals(" String appTitle(String arg1, String arg2) => _t('app_title', args: [arg1, arg2]);\n\n"));
expect(sb.toString(), equals(" String appTitle(String arg1, String arg2) => _t(LocalizationKeys.appTitle, args: [arg1, arg2]);\n\n"));
});

test('Test translations with 1 string argument but 2 string replacements', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$s %1\$s');
expect(sb.toString(), equals(" String appTitle(String arg1) => _t('app_title', args: [arg1]);\n\n"));
expect(sb.toString(), equals(' String appTitle(String arg1) => _t(LocalizationKeys.appTitle, args: [arg1]);\n\n'));
});

test('Test translations with 11 arguments of the same index and same type', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s');
expect(sb.toString(), equals(" String appTitle(String arg1) => _t('app_title', args: [arg1]);\n\n"));
expect(sb.toString(), equals(' String appTitle(String arg1) => _t(LocalizationKeys.appTitle, args: [arg1]);\n\n'));
});
});

group('Tests with number arguments', () {
test('Test translations with 1 number argument', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$d');
expect(sb.toString(), equals(" String appTitle(num arg1) => _t('app_title', args: [arg1]);\n\n"));
expect(sb.toString(), equals(' String appTitle(num arg1) => _t(LocalizationKeys.appTitle, args: [arg1]);\n\n'));
});

test('Test translations with 2 number arguments', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$d %2\$d');
expect(sb.toString(), equals(" String appTitle(num arg1, num arg2) => _t('app_title', args: [arg1, arg2]);\n\n"));
expect(sb.toString(), equals(' String appTitle(num arg1, num arg2) => _t(LocalizationKeys.appTitle, args: [arg1, arg2]);\n\n'));
});

test('Test translations with 1 number argument but 2 number replacements', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$d %1\$d');
expect(sb.toString(), equals(" String appTitle(num arg1) => _t('app_title', args: [arg1]);\n\n"));
expect(sb.toString(), equals(" String appTitle(num arg1) => _t(LocalizationKeys.appTitle, args: [arg1]);\n\n"));
});

test('Test translations with 11 arguments of the same index and same type', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$d %1\$d %1\$d %1\$d %1\$d %1\$d %1\$d %1\$d %1\$d %1\$d %1\$d');
expect(sb.toString(), equals(" String appTitle(num arg1) => _t('app_title', args: [arg1]);\n\n"));
expect(sb.toString(), equals(' String appTitle(num arg1) => _t(LocalizationKeys.appTitle, args: [arg1]);\n\n'));
});
});

group('Tests with mixed arguments', () {
test('Test translations with 1 number argument', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$s %2\$d');
expect(sb.toString(), equals(" String appTitle(String arg1, num arg2) => _t('app_title', args: [arg1, arg2]);\n\n"));
expect(sb.toString(), equals(' String appTitle(String arg1, num arg2) => _t(LocalizationKeys.appTitle, args: [arg1, arg2]);\n\n'));
});

test('Test translations with 11 arguments of the same index and same type', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %1\$s %2\$d');
expect(sb.toString(), equals(" String appTitle(String arg1, num arg2) => _t('app_title', args: [arg1, arg2]);\n\n"));
expect(sb.toString(), equals(' String appTitle(String arg1, num arg2) => _t(LocalizationKeys.appTitle, args: [arg1, arg2]);\n\n'));
});

test('Test translations with 11 different arguments', () {
Expand All @@ -92,21 +92,21 @@ void main() {
expect(
sb.toString(),
equals(
" String appTitle(String arg1, String arg2, String arg3, String arg4, String arg5, String arg6, String arg7, String arg8, String arg9, String arg10, String arg11) => _t('app_title', args: [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11]);\n\n"));
' String appTitle(String arg1, String arg2, String arg3, String arg4, String arg5, String arg6, String arg7, String arg8, String arg9, String arg10, String arg11) => _t(LocalizationKeys.appTitle, args: [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11]);\n\n'));
});
});

group('Tests with invalid arguments', () {
test('Test translations with unsupported type', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$z');
expect(sb.toString(), equals(" String get appTitle => _t('app_title');\n\n"));
expect(sb.toString(), equals(' String get appTitle => _t(LocalizationKeys.appTitle);\n\n'));
});

test('Test translations with 1 number argument', () {
final sb = StringBuffer();
FileWriter.buildTranslationFunction(sb, 'app_title', 'hallo %1\$s %1\$d');
expect(sb.toString(), equals(" String get appTitle => _t('app_title');\n\n"));
expect(sb.toString(), equals(' String get appTitle => _t(LocalizationKeys.appTitle);\n\n'));
});
});
}