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

implement locale fallback for all locales #5718

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/surveyStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@ export var surveyLocalization = {
getLocaleStrings(loc: string): any {
return this.locales[loc];
},
getCurrentStrings(locale?: string): any {
let loc = locale && this.locales[locale];
if (!loc) loc = this.currentLocale ? this.locales[this.currentLocale] : this.locales[this.defaultLocale];
if (!loc) loc = this.locales[this.defaultLocale];
if (!loc) loc = this.locales["en"];
return loc;
},
getString: function (strName: string, locale: string = null) {
const originalLocale = locale;
locale = locale || this.currentLocale || this.defaultLocale;
var loc = this.getCurrentStrings(locale);
if (!!loc[strName]) return loc[strName];
const index = !!locale ? locale.indexOf("-") : -1;
if(index > -1) return this.getString(strName, locale.substring(0, index));
loc = this.locales[this.defaultLocale];
var result = loc[strName];
if (result === undefined) {
result = this.locales["en"][strName];
getString: function (strName: string, requestedLocale: string = null) {
const rawOptions = [requestedLocale, this.currentLocale, this.defaultLocale, "en"];

for (let locale of rawOptions) {
if (!!locale) {
// Try exact locale
if (!!this.locales?.[locale]?.[strName]) {
return this.locales[locale][strName];
}
// Try parent locale
const parts = locale.split("-");
if (parts.length > 1 && !!this.locales?.[parts[0]]?.[strName]) {
return this.locales[parts[0]][strName];
}
}
}
if(result === undefined) return this.onGetExternalString(strName, originalLocale);
return result;
return this.onGetExternalString(strName, requestedLocale);
},
getLocales: function (removeDefaultLoc: boolean = false): Array<string> {
var res = [];
Expand Down