Skip to content

Commit

Permalink
feat: i18nextService uses namespace "KoliBri" (#5610)
Browse files Browse the repository at this point in the history
  • Loading branch information
deleonio committed Nov 27, 2023
2 parents 0fb50bf + 043d006 commit 1793d25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions packages/components/src/core/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export interface II18nService {

export class I18nextService implements II18nService {
private static instance: II18nService;
private static namespace = 'KoliBri';

private constructor() {}

public static async getInstance(
public static async createInstance(
lng: Generic.I18n.Locale.ISO_639_1,
translations?:
| Generic.I18n.RegisterPatch<Generic.I18n.Locale.ISO_639_1, string, string>
Expand All @@ -50,9 +51,14 @@ export class I18nextService implements II18nService {

I18nextService.instance = new I18nextService();

await i18next.init({
lng,
});
if (!i18next.isInitialized) {
await i18next.init({
ns: [I18nextService.namespace],
lng,
});
} else {
await i18next.loadNamespaces(I18nextService.namespace);
}

if (translations !== undefined) {
translations.forEach((t) =>
Expand All @@ -67,7 +73,7 @@ export class I18nextService implements II18nService {
}

public static addResourceBundle(lng: Generic.I18n.Locale.ISO_639_1, translationMap: Generic.I18n.Map<string, string>) {
i18next.addResourceBundle(lng, 'translation', translationMap, true);
i18next.addResourceBundle(lng, I18nextService.namespace, translationMap, true);
}

public addResourceBundle(lng: Generic.I18n.Locale.ISO_639_1, translationMap: Generic.I18n.Map<string, string>) {
Expand All @@ -76,6 +82,7 @@ export class I18nextService implements II18nService {

public translate(key: string, options?: ITranslationOptions) {
return i18next.t(key, {
ns: I18nextService.namespace,
count: options?.count,
...options?.placeholders,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const register = async (
options?: RegisterOptions
): Promise<void[]> => {
if (STORE.I18n === undefined) {
const i18n = await I18nextService.getInstance(options?.translation?.name ?? 'de', options?.translations);
const i18n = await I18nextService.createInstance(options?.translation?.name ?? 'de', options?.translations);
Object.defineProperty(STORE, 'I18n', {
value: i18n,
writable: false,
Expand Down

0 comments on commit 1793d25

Please sign in to comment.