CAKE-272 | added parameters to script; added possibility to generate locales.dart from arb file list; added loadLocaleList() method to language_service.dart

This commit is contained in:
OleksandrSobol 2021-03-24 18:52:30 +02:00
parent 8603e3f2bf
commit ff8ed30483
7 changed files with 115 additions and 9165 deletions

2
.gitignore vendored
View file

@ -95,4 +95,4 @@ vendor/
android/app/.cxx/**
ios/Flutter/.last_build_id
lib/generated/i18n.dart
/lib/generated/**

View file

@ -1,8 +1,9 @@
import 'package:cake_wallet/generated/locales.dart';
import 'package:devicelocale/devicelocale.dart';
import 'package:intl/intl.dart';
class LanguageService {
static const Map<String, String> list = {
static const Map<String, String> supportedLocales = {
'en': 'English',
'de': 'Deutsch (German)',
'es': 'Español (Spanish)',
@ -16,6 +17,15 @@ class LanguageService {
'uk': 'Українська (Ukrainian)',
'zh': '中文 (Chinese)'
};
static final list = <String, String> {};
static void loadLocaleList() {
supportedLocales.forEach((key, value) {
if (locales.contains(key)) {
list[key] = value;
}
});
}
static Future<String> localeDetection() async {
var locale = await Devicelocale.currentLocale;

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/entities/language_service.dart';
import 'package:cake_wallet/entities/order.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@ -135,6 +136,7 @@ Future<void> initialSetup(
@required Box<TransactionDescription> transactionDescriptions,
FlutterSecureStorage secureStorage,
int initialMigrationVersion = 13}) async {
LanguageService.loadLocaleList();
await defaultSettingsMigration(
secureStorage: secureStorage,
version: initialMigrationVersion,

View file

@ -5,11 +5,8 @@ import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/entities/language_service.dart';
// import 'package:cake_wallet/src/stores/settings/settings_store.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
@ -62,7 +59,5 @@ class LanguageListPage extends BasePage {
});
},
));
return null;
}
}

View file

@ -1,54 +1,114 @@
import 'dart:io';
import 'dart:convert';
import 'localization/locale_list.dart';
import 'localization/localization_constants.dart';
import 'utils/utils.dart';
const inputPath = 'res/values/';
const outputPath = 'lib/generated/i18n.dart';
const outputPath = 'lib/generated/';
const localizationFileName = 'i18n.dart';
const localeListFileName = 'locales.dart';
const srcDir = 'srcDir';
const defaultLocale = 'en';
Future<void> main() async {
var output = '';
output += part1;
output += textDirectionDeclaration;
for (var locale in locales) {
final inputContent = File(inputPath + 'strings_$locale.arb').readAsStringSync();
final config = json.decode(inputContent) as Map<String, dynamic>;
if (locale == locales.first) {
output += localizedStrings(config: config, hasOverride: false);
output += '}' + '\n\n';
Future<void> main(List<String> args) async {
final extraInfo = args.isNotEmpty ?
args.fold(<String, dynamic>{}, (Map<String, dynamic> acc, String arg) {
final parts = arg.split('=');
var key = normalizeKeyName(parts[0]);
if (key.contains('--')) {
key = key.substring(2);
}
acc[key] = parts.length > 1
? parts[1].isNotEmpty
? parts[1]
: inputPath
: inputPath;
return acc;
})
: <String, dynamic> {srcDir : inputPath};
output += 'class \$$locale extends S {' + '\n';
output += ' const \$$locale();' + '\n';
extraInfo.forEach((key, dynamic value) async {
if (key == srcDir) {
final dirPath = value as String;
final dir = Directory(dirPath);
if (locale != locales.first) {
output += textDirectionDeclaration;
output += localizedStrings(config: config, hasOverride: true);
if (await dir.exists()) {
final localePath = <String, dynamic>{};
await dir.list(recursive: false).forEach((element) {
try {
final shortLocale = element.path.split('_',)[1].split('.')[0];
localePath[shortLocale] = element.path;
} catch (e) {
print('Wrong file: ${element.path}');
}
});
if (localePath.keys.contains(defaultLocale)) {
try {
var output = '';
var locales = 'const locales = [';
output += part1;
output += textDirectionDeclaration;
var inputContent =
File(localePath[defaultLocale].toString()).readAsStringSync();
var config = json.decode(inputContent) as Map<String, dynamic>;
output += localizedStrings(config: config, hasOverride: false);
output += '}' + '\n\n';
localePath.forEach((key, dynamic value) {
inputContent = File(localePath[key].toString()).readAsStringSync();
config = json.decode(inputContent) as Map<String, dynamic>;
locales += "'$key', ";
output += 'class \$$key extends S {' + '\n';
output += ' const \$$key();' + '\n';
if (key != defaultLocale) {
output += textDirectionDeclaration;
output += localizedStrings(config: config, hasOverride: true);
}
output += '}' + '\n\n';
});
output += classDeclaration;
localePath.keys.forEach((key) {
output += ' Locale("$key", ""),' + '\n';
});
output += part2;
localePath.keys.forEach((key) {
output += ' case "$key":' + '\n';
output += ' S.current = const \$$key();' + '\n';
output += ' return SynchronousFuture<S>(S.current);' + '\n';
});
output += part3;
await File(outputPath + localizationFileName).writeAsString(output);
locales += '];';
await File(outputPath + localeListFileName).writeAsString(locales);
} catch (e) {
print(e.toString());
}
} else {
print("Locale list doesn't contain $defaultLocale");
}
} else {
print('Wrong directory path: $dirPath');
}
} else {
print('Wrong key: $key');
}
output += '}' + '\n\n';
}
output += classDeclaration;
for (var locale in locales) {
output += ' Locale("$locale", ""),' + '\n';
}
output += part2;
for (var locale in locales) {
output += ' case "$locale":' + '\n';
output += ' S.current = const \$$locale();' + '\n';
output += ' return SynchronousFuture<S>(S.current);' + '\n';
}
output += part3;
await File(outputPath).writeAsString(output);
});
}
String localizedStrings({Map<String, dynamic> config, bool hasOverride}) {

View file

@ -1,3 +0,0 @@
const locales = ['en', 'de', 'es', 'hi',
'ja', 'ko', 'nl', 'pl',
'pt', 'ru', 'uk', 'zh'];