From 2620cd0315dd5721df04f64b12daefd5202aacc0 Mon Sep 17 00:00:00 2001 From: maru Date: Wed, 1 Jul 2020 13:36:02 +0200 Subject: [PATCH 1/2] Add support for multiline text --- example/lib/main.dart | 2 +- example/lib/nstack.dart | 3 ++- lib/src/nstack_builder.dart | 3 ++- lib/src/repository.dart | 20 +++++++++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index bd1e811..c146cb1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,7 +14,7 @@ class MyApp extends StatelessWidget { title: Text(context.localization.defaultSection.title), ), body: Center( - child: Text(context.localization.test.testDoubleQuotationMark), + child: Text(context.localization.test.testMultipleLines), ), ), ), diff --git a/example/lib/nstack.dart b/example/lib/nstack.dart index cdc64f8..3dbd063 100644 --- a/example/lib/nstack.dart +++ b/example/lib/nstack.dart @@ -29,6 +29,7 @@ class _Test extends SectionKeyDelegate { String get testDollarSign => get('testDollarSign', '\$testing'); String get testSingleQuotationMark => get('testSingleQuotationMark', '\'testing\''); String get testDoubleQuotationMark => get('testDoubleQuotationMark', '\"testing\"'); + String get testMultipleLines => get('testMultipleLines', 'testing\nmultiple\nlines'); } const _config = NStackConfig(projectId: 'h6wJremI2TGFM88gbLkdyljWQuwf2hxhxvCH', apiKey: 'zp2S18H32b67eYAbRQh94tVw76ZzaKKXlHjd'); @@ -38,7 +39,7 @@ const _languages = [ ]; const _bundledTranslations = { - 'en-EN': '{\"data\":{\"default\":{\"title\":\"NStack SDK Demo\"},\"test\":{\"testDollarSign\":\"\$testing\",\"testSingleQuotationMark\":\"\'testing\'\",\"testDoubleQuotationMark\":\"\\"testing\\"\"}},\"meta\":{\"language\":{\"id\":56,\"name\":\"English\",\"locale\":\"en-EN\",\"direction\":\"LRM\",\"is_default\":false,\"is_best_fit\":false},\"platform\":{\"id\":515,\"slug\":\"mobile\"}}}', + 'en-EN': '{\"data\":{\"default\":{\"title\":\"NStack SDK Demo\"},\"test\":{\"testDollarSign\":\"\$testing\",\"testSingleQuotationMark\":\"\'testing\'\",\"testDoubleQuotationMark\":\"\\"testing\\"\",\"testMultipleLines\":\"testing\nmultiple\nlines\"}},\"meta\":{\"language\":{\"id\":56,\"name\":\"English\",\"locale\":\"en-EN\",\"direction\":\"LRM\",\"is_default\":false,\"is_best_fit\":false},\"platform\":{\"id\":515,\"slug\":\"mobile\"}}}', }; final _nstack = NStack( diff --git a/lib/src/nstack_builder.dart b/lib/src/nstack_builder.dart index 5e205ef..87e720b 100644 --- a/lib/src/nstack_builder.dart +++ b/lib/src/nstack_builder.dart @@ -141,7 +141,8 @@ import 'package:nstack/partial/section_key_delegate.dart'; return stringValue .replaceAll("'", "\\'") .replaceAll('"', '\\"') - .replaceAll('\$', '\\\$'); + .replaceAll('\$', '\\\$') + .replaceAll('\n', '\\n'); } /// Returns a CamelCase class name from the Localization section key diff --git a/lib/src/repository.dart b/lib/src/repository.dart index 405375c..f7f33c7 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -1,7 +1,7 @@ -import 'package:nstack/models/language.dart'; - import 'dart:convert'; +import 'package:nstack/models/language.dart'; + class LocalizationRepository { // Factory static final LocalizationRepository _instance = @@ -35,7 +35,9 @@ class LocalizationRepository { } void updateLocalization( - Map localizationJson, String bestFitLocale) { + Map localizationJson, + String bestFitLocale, + ) { this._pickedLanguage = _availableLanguages.firstWhere( (element) => element.locale == bestFitLocale, orElse: () => this._pickedLanguage, @@ -52,7 +54,15 @@ class LocalizationRepository { } } - String getSectionKeyValue(String sectionKey, String textKey, String fallbackText) { - return _sectionsMap[sectionKey][textKey] ?? fallbackText; + String getSectionKeyValue( + String sectionKey, + String textKey, + String fallbackText, + ) { + try { + return _sectionsMap[sectionKey][textKey] ?? fallbackText; + } catch (err) { + return fallbackText; + } } } From cdd548ab3d8d26eef531b61aaa4346859c113097 Mon Sep 17 00:00:00 2001 From: maru Date: Fri, 3 Jul 2020 14:44:22 +0200 Subject: [PATCH 2/2] Print error and stack --- lib/src/repository.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/src/repository.dart b/lib/src/repository.dart index f7f33c7..6ea1066 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -49,8 +49,9 @@ class LocalizationRepository { try { _sectionsMap = json.decode(_bundledTranslations[_pickedLanguage.locale])['data']; - } catch (err) { - print('_setupInternalMap() failed --> ${err.toString()}'); + } catch (e, s) { + print(e); + print(s); } } @@ -61,7 +62,9 @@ class LocalizationRepository { ) { try { return _sectionsMap[sectionKey][textKey] ?? fallbackText; - } catch (err) { + } catch (e, s) { + print(e); + print(s); return fallbackText; } }