-
Notifications
You must be signed in to change notification settings - Fork 266
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 Could not fetch translation json for locale: 'de' #1493
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b9f5f0
Replace `{}` by `null` for missing locale
7c71ac1
Add AppLocalization.variations
2ca86f7
Use same locale name for logging and loading from file
1410824
Merge branch 'develop' into language-variations
Nils1729 cfa26d2
Merge branch 'develop' into language-variations
Nils1729 82a2037
Change type for translations in element-desktop
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ export class AppLocalization { | |
private readonly localizedComponents?: Set<Component>; | ||
|
||
public constructor({ store, components = [] }: { store: TypedStore; components: Component[] }) { | ||
counterpart.registerTranslations(FALLBACK_LOCALE, this.fetchTranslationJson("en_EN")); | ||
counterpart.registerTranslations(FALLBACK_LOCALE, this.fetchTranslationJson("en_EN") ?? {}); | ||
counterpart.setFallbackLocale(FALLBACK_LOCALE); | ||
counterpart.setSeparator("|"); | ||
|
||
|
@@ -91,25 +91,35 @@ export class AppLocalization { | |
this.resetLocalizedUI(); | ||
} | ||
|
||
// Fallbacks to a locale (e.g. en-gb to [en-gb, en, en-en] or just en to [en, en-en]) | ||
private variations(locale: string): string[] { | ||
const [genericPart, specificPart] = locale.split("-"); | ||
const candidates = [locale]; | ||
if (specificPart) { | ||
candidates.push(genericPart); | ||
} | ||
if (genericPart != specificPart) { | ||
candidates.push(`${genericPart}-${genericPart}`); | ||
} | ||
return candidates; | ||
} | ||
|
||
// Format language strings from normalized form to non-normalized form (e.g. en-gb to en_GB) | ||
private denormalize(locale: string): string { | ||
if (locale === "en") { | ||
locale = "en_EN"; | ||
} | ||
const parts = locale.split("-"); | ||
if (parts.length > 1) { | ||
parts[1] = parts[1].toUpperCase(); | ||
} | ||
return parts.join("_"); | ||
} | ||
|
||
public fetchTranslationJson(locale: string): Record<string, string> { | ||
public fetchTranslationJson(locale: string): object | null { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
try { | ||
console.log("Fetching translation json for locale: " + locale); | ||
return loadJsonFile(__dirname, "i18n", "strings", `${this.denormalize(locale)}.json`); | ||
console.log(`Fetching translation json for locale: ${locale}`); | ||
return loadJsonFile(__dirname, "i18n", "strings", `${locale}.json`); | ||
} catch (e) { | ||
console.log(`Could not fetch translation json for locale: '${locale}'`, e); | ||
return {}; | ||
return null; | ||
} | ||
} | ||
|
||
|
@@ -120,13 +130,16 @@ export class AppLocalization { | |
locales = [locales]; | ||
} | ||
|
||
const loadedLocales = locales.filter((locale) => { | ||
const translations = this.fetchTranslationJson(locale); | ||
if (translations !== null) { | ||
counterpart.registerTranslations(locale, translations); | ||
} | ||
return !!translations; | ||
}); | ||
const loadedLocales = locales | ||
.flatMap(this.variations) | ||
.map(this.denormalize) | ||
.filter((locale) => { | ||
const translations = this.fetchTranslationJson(locale); | ||
if (translations !== null) { | ||
counterpart.registerTranslations(locale, translations); | ||
} | ||
return !!translations; | ||
}); | ||
|
||
counterpart.setLocale(loadedLocales[0]); | ||
this.store.set(AppLocalization.STORE_KEY, locales); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
getNormalizedLanguageKeys
frommatrix-web-i18n
to remain consistent across the layers?