Skip to content

fix: update internal picked language when a persisted locale exists #39

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

Merged
merged 5 commits into from
Oct 5, 2021
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
15 changes: 12 additions & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 28
compileSdkVersion 30

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

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

buildTypes {
release {
// TODO: Add your own signing config for the release build.
Expand All @@ -59,5 +68,5 @@ flutter {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

}
4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.4.31'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
12 changes: 9 additions & 3 deletions example/lib/nstack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class _DefaultSection extends SectionKeyDelegate {
class _Test extends SectionKeyDelegate {
const _Test(): super('test');

String get testDollarSign => get('testDollarSign', "\$testing again sdfsdf");
String get testDollarSign => get('testDollarSign', "\$testing again new");
String get testSingleQuotationMark => get('testSingleQuotationMark', "\'testing\'");
String get testDoubleQuotationMark => get('testDoubleQuotationMark', "\"testing\"");
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines");
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines\nupdated");
}

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

const _bundledTranslations = {
'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"}}}''',
'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"}}}''',
'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"}}}''',
};

Expand Down Expand Up @@ -89,6 +89,12 @@ class NStackWidget extends StatefulWidget {
class NStackState extends State<NStackWidget> {
final NStack<Localization> nstack = _nstack;

@override
void initState() {
super.initState();
_nstack.initClientLocale();
}

changeLanguage(Locale locale) async {
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
}
Expand Down
17 changes: 17 additions & 0 deletions lib/nstack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class NStack<T> {

var _appOpenCalled = false;

Locale? clientLocale;

NStack(
{required this.config,
required this.localization,
Expand Down Expand Up @@ -114,6 +116,8 @@ class NStack<T> {
/// 4. Fallback to bundled localizations from last build
Future changeLocalization(Locale locale) async {
try {
clientLocale = locale;

// Direct locale match
var localLanguage = LocalizationRepository().localizeIndexes.firstWhere(
(element) =>
Expand Down Expand Up @@ -172,6 +176,18 @@ class NStack<T> {
}
}

Future<String?> initClientLocale() async {
final prefs = await SharedPreferences.getInstance();
if (prefs.containsKey(_prefsSelectedLocale)) {
var languageTag = prefs.getString(_prefsSelectedLocale) ?? clientLocale?.toLanguageTag() ?? '';
LocalizationRepository().overridePickedLanguage(languageTag);
return languageTag;
} else if(clientLocale != null) {
return clientLocale!.toLanguageTag();
}
return null;
}

Future<AppOpenResult> appOpen(
Locale locale, {
AppOpenPlatform? platformOverride,
Expand All @@ -191,6 +207,7 @@ class NStack<T> {
languageTag =
prefs.getString(_prefsSelectedLocale) ?? locale.toLanguageTag();
_log("NStack --> User has overwritten device locale to: $languageTag");
LocalizationRepository().overridePickedLanguage(languageTag);
}

_log("NStack --> Calling App Open...");
Expand Down
6 changes: 6 additions & 0 deletions lib/src/nstack_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ class NStackWidget extends StatefulWidget {
class NStackState extends State<NStackWidget> {
final NStack<Localization> nstack = _nstack;

@override
void initState() {
super.initState();
_nstack.initClientLocale();
}

changeLanguage(Locale locale) async {
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
}
Expand Down
7 changes: 7 additions & 0 deletions lib/src/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class LocalizationRepository {
_persistInternalMap();
}

overridePickedLanguage(String locale) {
this._pickedLanguage = _availableLanguages.firstWhere(
(element) => element.locale == locale,
orElse: () => this._pickedLanguage,
);
}

_setupInternalMap() async {
try {
WidgetsFlutterBinding.ensureInitialized();
Expand Down