Skip to content
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

FIX: Enabling users to manually set the locale. #64

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions src/i18n-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export class I18nGenerator implements IDisposable, InsertActionProviderDelegate
private generateLocales(template: string, config: I18nConfig): string {
let localesContent = "";
let casesContent = "";
let getLocale = "";

const languageCodes: any = {};
for (let locale of config.locales) {
Expand All @@ -258,16 +259,24 @@ export class I18nGenerator implements IDisposable, InsertActionProviderDelegate
}

for (let languageCode in languageCodes) {
const normalized = languageCodes[languageCode];
if (languageCodes.hasOwnProperty(languageCode)) {
const normalized = languageCodes[languageCode];
casesContent += ` else if ("${languageCode}" == languageCode) {\n`;
casesContent += ` return SynchronousFuture<WidgetsLocalizations>(const _I18n_${normalized}());\n`;
casesContent += " }\n";
}

if (getLocale.length > 0) {
getLocale += " else ";
}
getLocale += `if ("${languageCode}" == languageCode) {\n`;
getLocale += ` return _I18n_${normalized}();\n`;
getLocale += ` }\n`;
}

let result = template.replace("{locales}", localesContent);
result = result.replace("{cases}", casesContent);
result = result.replace("{getLocale}", getLocale);
return result;
}

Expand Down Expand Up @@ -639,9 +648,20 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocali
};
}


I18n getLocale(Locale locale) {
assert(locale != null);
final languageCode = locale.languageCode;

{getLocale}

return I18n();
}


@override
Future<WidgetsLocalizations> load(Locale locale) {
I18n._locale ??= locale;
I18n._locale = locale;
I18n._shouldReload = false;
final String lang = I18n._locale != null ? I18n._locale.toString() : "";
final String languageCode = I18n._locale != null ? I18n._locale.languageCode : "";
Expand Down