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 Could not fetch translation json for locale: 'de' #1493

Closed
wants to merge 6 commits into from
Closed
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
43 changes: 28 additions & 15 deletions src/language-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("|");

Expand All @@ -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[] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use getNormalizedLanguageKeys from matrix-web-i18n to remain consistent across the layers?

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is too loose here, please use a better type

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;
}
}

Expand All @@ -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);
Expand Down