From 2e69f3ed267898fedb7ebc1f10711f86889e235c Mon Sep 17 00:00:00 2001 From: Sam Mousa Date: Tue, 28 Feb 2023 09:23:01 +0100 Subject: [PATCH 1/2] feat: implement locale fallback for all locales --- src/surveyStrings.ts | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/surveyStrings.ts b/src/surveyStrings.ts index 9ed5aa7565..f944c59d67 100644 --- a/src/surveyStrings.ts +++ b/src/surveyStrings.ts @@ -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 { var res = []; From 6b115e09426efcd91468cc7fc0304c739cb4bdaf Mon Sep 17 00:00:00 2001 From: Sam Mousa Date: Tue, 28 Feb 2023 11:27:21 +0100 Subject: [PATCH 2/2] chore: fix code style --- src/surveyStrings.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/surveyStrings.ts b/src/surveyStrings.ts index f944c59d67..626742ab49 100644 --- a/src/surveyStrings.ts +++ b/src/surveyStrings.ts @@ -24,18 +24,18 @@ export var surveyLocalization = { return this.locales[loc]; }, getString: function (strName: string, requestedLocale: string = null) { - const rawOptions = [requestedLocale, this.currentLocale, this.defaultLocale, "en"] + 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] + return this.locales[locale][strName]; } // Try parent locale - const parts = locale.split('-') + const parts = locale.split("-"); if (parts.length > 1 && !!this.locales?.[parts[0]]?.[strName]) { - return this.locales[parts[0]][strName] + return this.locales[parts[0]][strName]; } } }