Skip to content

Commit

Permalink
fix: locale override logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropapa committed Nov 9, 2024
1 parent befc284 commit 8259dee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
15 changes: 5 additions & 10 deletions config/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ export type ScriptLocale<T extends keyof typeof LocaleDefaults> = Locale<
>;

export const t = <T extends keyof typeof LocaleDefaults>(
phase: T,
langKey: T,
vars?: ScriptLocale<T>["variables"]
): string => {
const scriptLanguage = SETTINGS.DEFAULT_LANG as keyof typeof locales;

const locale = locales[scriptLanguage] || [];
const locale = locales[scriptLanguage] || {};

const overrides = localeOverrides(scriptLanguage);

let phrase: string = overrides[phase] || locale[phase];
let phrase: string = overrides[langKey] || locale[langKey];

if (!vars) return phrase;

Expand All @@ -31,13 +31,8 @@ export const t = <T extends keyof typeof LocaleDefaults>(
return phrase;
};

const localeOverrides = (language: string) => {
if (typeof LOCALE_OVERRIDES !== "undefined" && LOCALE_OVERRIDES?.[language]) {
return LOCALE_OVERRIDES[language];
}

return null;
};
const localeOverrides = (language: string) =>
LOCALE_OVERRIDES?.[language] ?? {};

const getLocaleVars = (locale: typeof LocaleDefaults) =>
Object.values(locale).reduce((acc, curr, index) => {
Expand Down
6 changes: 6 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ global.SETTINGS = {
},
},
};

global.LOCALE_OVERRIDES = {
// "en-us": {
// fish_pull_hint: "My custom hint",
// },
};

0 comments on commit 8259dee

Please sign in to comment.