Skip to content

Commit 1859500

Browse files
authored
fix: update internal picked language when a persisted locale exists (#39)
* fix: update internal picked language when a persisted locale exists * fix: reload picked language earlier and if app open fails * build: bumped gradle and switched java version * chore: updated gradle wrapper * fix: call `super.initState()`
1 parent f6bc4fb commit 1859500

File tree

7 files changed

+54
-9
lines changed

7 files changed

+54
-9
lines changed

example/android/app/build.gradle

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
2626
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
2727

2828
android {
29-
compileSdkVersion 28
29+
compileSdkVersion 30
3030

3131
sourceSets {
3232
main.java.srcDirs += 'src/main/kotlin'
@@ -40,11 +40,20 @@ android {
4040
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4141
applicationId "dk.nodes.nstack_example"
4242
minSdkVersion 16
43-
targetSdkVersion 28
43+
targetSdkVersion 30
4444
versionCode flutterVersionCode.toInteger()
4545
versionName flutterVersionName
4646
}
4747

48+
compileOptions {
49+
sourceCompatibility JavaVersion.VERSION_1_8
50+
targetCompatibility JavaVersion.VERSION_1_8
51+
}
52+
53+
kotlinOptions {
54+
jvmTarget = "1.8"
55+
}
56+
4857
buildTypes {
4958
release {
5059
// TODO: Add your own signing config for the release build.
@@ -59,5 +68,5 @@ flutter {
5968
}
6069

6170
dependencies {
62-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
71+
6372
}

example/android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
buildscript {
2-
ext.kotlin_version = '1.3.50'
2+
ext.kotlin_version = '1.4.31'
33
repositories {
44
google()
55
jcenter()
66
}
77

88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.5.0'
9+
classpath 'com.android.tools.build:gradle:7.0.2'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111
}
1212
}

example/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip

example/lib/nstack.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class _DefaultSection extends SectionKeyDelegate {
3232
class _Test extends SectionKeyDelegate {
3333
const _Test(): super('test');
3434

35-
String get testDollarSign => get('testDollarSign', "\$testing again sdfsdf");
35+
String get testDollarSign => get('testDollarSign', "\$testing again new");
3636
String get testSingleQuotationMark => get('testSingleQuotationMark', "\'testing\'");
3737
String get testDoubleQuotationMark => get('testDoubleQuotationMark', "\"testing\"");
38-
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines");
38+
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines\nupdated");
3939
}
4040

4141
const _config = NStackConfig(projectId: 'h6wJremI2TGFM88gbLkdyljWQuwf2hxhxvCH', apiKey: 'zp2S18H32b67eYAbRQh94tVw76ZzaKKXlHjd');
@@ -46,7 +46,7 @@ final _languages = [
4646
];
4747

4848
const _bundledTranslations = {
49-
'en-EN': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"$testing again sdfsdf","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"}}}''',
49+
'en-EN': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"$testing again new","testSingleQuotationMark":"'testing'","testDoubleQuotationMark":"\"testing\"","testMultipleLines":"testing\nmultiple\nlines\nupdated"}},"meta":{"language":{"id":56,"name":"English","locale":"en-EN","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
5050
'de-AT': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"__testDollarSign","testSingleQuotationMark":"__testSingleQuotationMark","testDoubleQuotationMark":"__testDoubleQuotationMark","testMultipleLines":"__testMultipleLines"}},"meta":{"language":{"id":7,"name":"German (Austria)","locale":"de-AT","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
5151
};
5252

@@ -89,6 +89,12 @@ class NStackWidget extends StatefulWidget {
8989
class NStackState extends State<NStackWidget> {
9090
final NStack<Localization> nstack = _nstack;
9191

92+
@override
93+
void initState() {
94+
super.initState();
95+
_nstack.initClientLocale();
96+
}
97+
9298
changeLanguage(Locale locale) async {
9399
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
94100
}

lib/nstack.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class NStack<T> {
4141

4242
var _appOpenCalled = false;
4343

44+
Locale? clientLocale;
45+
4446
NStack(
4547
{required this.config,
4648
required this.localization,
@@ -114,6 +116,8 @@ class NStack<T> {
114116
/// 4. Fallback to bundled localizations from last build
115117
Future changeLocalization(Locale locale) async {
116118
try {
119+
clientLocale = locale;
120+
117121
// Direct locale match
118122
var localLanguage = LocalizationRepository().localizeIndexes.firstWhere(
119123
(element) =>
@@ -172,6 +176,18 @@ class NStack<T> {
172176
}
173177
}
174178

179+
Future<String?> initClientLocale() async {
180+
final prefs = await SharedPreferences.getInstance();
181+
if (prefs.containsKey(_prefsSelectedLocale)) {
182+
var languageTag = prefs.getString(_prefsSelectedLocale) ?? clientLocale?.toLanguageTag() ?? '';
183+
LocalizationRepository().overridePickedLanguage(languageTag);
184+
return languageTag;
185+
} else if(clientLocale != null) {
186+
return clientLocale!.toLanguageTag();
187+
}
188+
return null;
189+
}
190+
175191
Future<AppOpenResult> appOpen(
176192
Locale locale, {
177193
AppOpenPlatform? platformOverride,
@@ -191,6 +207,7 @@ class NStack<T> {
191207
languageTag =
192208
prefs.getString(_prefsSelectedLocale) ?? locale.toLanguageTag();
193209
_log("NStack --> User has overwritten device locale to: $languageTag");
210+
LocalizationRepository().overridePickedLanguage(languageTag);
194211
}
195212

196213
_log("NStack --> Calling App Open...");

lib/src/nstack_builder.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ class NStackWidget extends StatefulWidget {
261261
class NStackState extends State<NStackWidget> {
262262
final NStack<Localization> nstack = _nstack;
263263
264+
@override
265+
void initState() {
266+
super.initState();
267+
_nstack.initClientLocale();
268+
}
269+
264270
changeLanguage(Locale locale) async {
265271
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
266272
}

lib/src/repository.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ class LocalizationRepository {
7171
_persistInternalMap();
7272
}
7373

74+
overridePickedLanguage(String locale) {
75+
this._pickedLanguage = _availableLanguages.firstWhere(
76+
(element) => element.locale == locale,
77+
orElse: () => this._pickedLanguage,
78+
);
79+
}
80+
7481
_setupInternalMap() async {
7582
try {
7683
WidgetsFlutterBinding.ensureInitialized();

0 commit comments

Comments
 (0)