Remove i18n package. Add xml files with strings to flutter assets. Create method for passing localized strings to channel.

This commit is contained in:
Ivan Murashov
2018-02-07 00:28:47 +03:00
parent 8c4287a21b
commit c15108fda9
40 changed files with 600 additions and 930 deletions

View File

@@ -1,18 +1,33 @@
import 'package:intl/intl.dart';
import 'i18n/messages_all.dart';
import 'package:sprintf/sprintf.dart';
import 'dart:async';
import 'package:sprintf/sprintf.dart';
import 'package:xml/xml.dart' as xml;
import 'package:flutter/services.dart';
class StringsLocalization {
static String localeCode;
static Map<String, String> strings = new Map<String, String>();
static Future load(String locale) async {
return initializeMessages(locale);
localeCode = locale;
var document = xml.parse(await loadStrings(locale));
strings.clear();
document.findAllElements('string').forEach((node) {
strings[node.attributes[0].value] = node.text.toString();
});
return strings;
}
static String declineCurrency(int num, int code) {
static Future<String> loadStrings(String locale) async {
return await rootBundle.loadString('assets/values-$locale/strings.xml');
}
static String declineCurrency(int num, int code) {
int residual = num % 100;
if (residual >= 20) {
if (residual >= 20) {
residual %= 10;
}
@@ -31,7 +46,6 @@ class StringsLocalization {
}
static List<String> currencies(int code) {
String nominative, singular, plural;
switch (code) {
@@ -62,88 +76,76 @@ class StringsLocalization {
break;
}
return [nominative, singular, plural];
}
static String confirmPurchase(String val, int code) {
String trimmedVal =val.substring(0, val.length - 3);
return sprintf(Intl.message('confirm_purchase', name: 'confirm_purchase', locale: Intl.defaultLocale), [val, declineCurrency(int.parse(trimmedVal), code)]);
String trimmedVal = val.substring(0, val.length - 3);
return sprintf(strings['confirm_purchase'], [val, declineCurrency(int.parse(trimmedVal), code)]);
}
static String purchaseCompleted(String val, int code) {
String trimmedVal =val.substring(0, val.length - 3);
return sprintf(Intl.message('purchase_complite', name: 'purchase_complite', locale: Intl.defaultLocale), [val, declineCurrency(int.parse(trimmedVal), code)]);
String trimmedVal = val.substring(0, val.length - 3);
return sprintf(strings['purchase_complite'], [val, declineCurrency(int.parse(trimmedVal), code)]);
}
static String registration() => Intl.message('registration', name: 'registration', locale: Intl.defaultLocale);
static String usage() => Intl.message('usage', name: 'usage', locale: Intl.defaultLocale);
static String support() => Intl.message('support', name: 'support', locale: Intl.defaultLocale);
static String common() => Intl.message('common', name: 'common', locale: Intl.defaultLocale);
static String idStore() => Intl.message('ID_Store', name: 'ID_Store', locale: Intl.defaultLocale);
static String signUp() => Intl.message('sign_up', name: 'sign_up', locale: Intl.defaultLocale);
static String specifyDinStore() => Intl.message('specify_din_store', name: 'specify_din_store', locale: Intl.defaultLocale);
static String confirmation() => Intl.message('confirmation', name: 'confirmation', locale: Intl.defaultLocale);
static String askChangeStore() => Intl.message('ask_change_store', name: 'ask_change_store', locale: Intl.defaultLocale);
static String yes() => Intl.message('yes', name: 'yes', locale: Intl.defaultLocale);
static String no() => Intl.message('no', name: 'no', locale: Intl.defaultLocale);
static String requestSentWaitActivation() => Intl.message('request_sent_wait_activ', name: 'request_sent_wait_activ', locale: Intl.defaultLocale);
static String refreshActivationStatus() => Intl.message('update_activ_status', name: 'update_activ_status', locale: Intl.defaultLocale);
static String appActivated() => Intl.message('app_activ', name: 'app_activ', locale: Intl.defaultLocale);
static String completeRegistration() => Intl.message('complite_activ', name: 'complite_activ', locale: Intl.defaultLocale);
static String cardScanner() => Intl.message('card_scaner', name: 'card_scaner', locale: Intl.defaultLocale);
static String card() => Intl.message('card', name: 'card', locale: Intl.defaultLocale);
static String reward() => Intl.message('reward', name: 'reward', locale: Intl.defaultLocale);
static String sum() => Intl.message('sum', name: 'sum', locale: Intl.defaultLocale);
static String bonus() => Intl.message('bonus', name: 'bonus', locale: Intl.defaultLocale);
static String couponsUsed() => Intl.message('coupons_used', name: 'coupons_used', locale: Intl.defaultLocale);
static String selectCoupons() => Intl.message('select_coupons', name: 'select_coupons', locale: Intl.defaultLocale);
static String carryingPurchase() => Intl.message('carry_purchase', name: 'carry_purchase', locale: Intl.defaultLocale);
static String completePurchase() => Intl.message('complite_purchase', name: 'complite_purchase', locale: Intl.defaultLocale);
static String scan() => Intl.message('scan', name: 'scan', locale: Intl.defaultLocale);
static String buyer() => Intl.message('buyer', name: 'buyer', locale: Intl.defaultLocale);
static String bonusHint() => Intl.message('bonus_hint', name: 'bonus_hint', locale: Intl.defaultLocale);
static String bonusExplanation() => Intl.message('bonus_explanation', name: 'bonus_explanation', locale: Intl.defaultLocale);
static String discountRate() => Intl.message('discount_rate', name: 'discount_rate', locale: Intl.defaultLocale);
static String discountSum() => Intl.message('discount_sum', name: 'discount_sum', locale: Intl.defaultLocale);
static String bonusPlus() => Intl.message('bonus_plus', name: 'bonus_plus', locale: Intl.defaultLocale);
static String bonusMinus() => Intl.message('bonus_minus', name: 'bonus_minus', locale: Intl.defaultLocale);
static String idNotFound() => Intl.message('ID_not_found', name: 'ID_not_found', locale: Intl.defaultLocale);
static String settings() => Intl.message('settings', name: 'settings', locale: Intl.defaultLocale);
static String help() => Intl.message('help', name: 'help', locale: Intl.defaultLocale);
static String exit() => Intl.message('exit', name: 'exit', locale: Intl.defaultLocale);
static String logout() => Intl.message('logout', name: 'logout', locale: Intl.defaultLocale);
static String currency() => Intl.message('currency', name: 'currency', locale: Intl.defaultLocale);
static String locale() => Intl.message('locale', name: 'locale', locale: Intl.defaultLocale);
// Валюты
static String nominativeRuble() => Intl.message('nominative_ruble', name: 'nominative_ruble', locale: Intl.defaultLocale);
static String singularRuble() => Intl.message('singular_ruble', name: 'singular_ruble', locale: Intl.defaultLocale);
static String pluralRuble() => Intl.message('plural_ruble', name: 'plural_ruble', locale: Intl.defaultLocale);
static String nominativeEuro() => Intl.message('nominative_euro', name: 'nominative_euro', locale: Intl.defaultLocale);
static String singularEuro() => Intl.message('singular_euro', name: 'singular_euro', locale: Intl.defaultLocale);
static String pluralEuro() => Intl.message('plural_euro', name: 'plural_euro', locale: Intl.defaultLocale);
static String nominativeDollar() => Intl.message('nominative_dollar', name: 'nominative_dollar', locale: Intl.defaultLocale);
static String singularDollar() => Intl.message('singular_dollar', name: 'singular_dollar', locale: Intl.defaultLocale);
static String pluralDollar() => Intl.message('plural_dollar', name: 'plural_dollar', locale: Intl.defaultLocale);
static String nominativeHryvna() => Intl.message('nominative_hryvna', name: 'nominative_hryvna', locale: Intl.defaultLocale);
static String singularHryvna() => Intl.message('singular_hryvna', name: 'singular_hryvna', locale: Intl.defaultLocale);
static String pluralHryvna() => Intl.message('plural_hryvna', name: 'plural_hryvna', locale: Intl.defaultLocale);
static String nominativeTenge() => Intl.message('nominative_tenge', name: 'nominative_tenge', locale: Intl.defaultLocale);
static String singularTenge() => Intl.message('singular_tenge', name: 'singular_tenge', locale: Intl.defaultLocale);
static String pluralTenge() => Intl.message('plural_tenge', name: 'plural_tenge', locale: Intl.defaultLocale);
static String registrationGuide() => Intl.message('registration_guide', name: 'registration_guide', locale: Intl.defaultLocale);
static String usageGuide() => Intl.message('usage_guide', name: 'usage_guide', locale: Intl.defaultLocale);
static String commonGuide() => Intl.message('common_guide', name: 'common_guide', locale: Intl.defaultLocale);
static String supportGuide(String phone, String url) {
return sprintf(Intl.message('support_guide', name: 'support_guide', locale: Intl.defaultLocale), [phone, url]);
}
static String registration() => strings['registration'];
static String usage() => strings['usage'];
static String support() => strings['support'];
static String common() => strings['common'];
static String idStore() => strings['ID_Store'];
static String signUp() => strings['sign_up'];
static String specifyDinStore() => strings['specify_din_store'];
static String confirmation() => strings['confirmation'];
static String askChangeStore() => strings['ask_change_store'];
static String yes() => strings['yes'];
static String no() => strings['no'];
static String requestSentWaitActivation() => strings['request_sent_wait_activ'];
static String refreshActivationStatus() => strings['update_activ_status'];
static String appActivated() => strings['app_activ'];
static String completeRegistration() => strings['complite_activ'];
static String cardScanner() => strings['card_scaner'];
static String card() => strings['card'];
static String reward() => strings['reward'];
static String sum() => strings['sum'];
static String bonus() => strings['bonus'];
static String couponsUsed() => strings['coupons_used'];
static String selectCoupons() => strings['select_coupons'];
static String carryingPurchase() => strings['carry_purchase'];
static String completePurchase() => strings['complite_purchase'];
static String scan() => strings['scan'];
static String buyer() => strings['buyer'];
static String bonusHint() => strings['bonus_hint'];
static String bonusExplanation() => strings['bonus_explanation'];
static String discountRate() => strings['discount_rate'];
static String discountSum() => strings['discount_sum'];
static String bonusPlus() => strings['bonus_plus'];
static String bonusMinus() => strings['bonus_minus'];
static String idNotFound() => strings['ID_not_found'];
static String settings() => strings['settings'];
static String help() => strings['help'];
static String exit() => strings['exit'];
static String logout() => strings['logout'];
static String currency() => strings['currency'];
static String locale() => strings['locale'];
static String nominativeRuble() => strings['nominative_ruble'];
static String singularRuble() => strings['singular_ruble'];
static String pluralRuble() => strings['plural_ruble'];
static String nominativeEuro() => strings['nominative_euro'];
static String singularEuro() => strings['singular_euro'];
static String pluralEuro() => strings['plural_euro'];
static String nominativeDollar() => strings['nominative_dollar'];
static String singularDollar() => strings['singular_dollar'];
static String pluralDollar() => strings['plural_dollar'];
static String nominativeHryvna() => strings['nominative_hryvna'];
static String singularHryvna() => strings['singular_hryvna'];
static String pluralHryvna() => strings['plural_hryvna'];
static String nominativeTenge() => strings['nominative_tenge'];
static String singularTenge() => strings['singular_tenge'];
static String pluralTenge() => strings['plural_tenge'];
static String registrationGuide() => strings['registration_guide'];
static String usageGuide() => strings['usage_guide'];
static String commonGuide() => strings['common_guide'].replaceAll('\n', "\n");
static String supportGuide(String phone, String url) => sprintf(strings['support_guide'], [phone, url]);
}