Skip to content

Commit 7adcc83

Browse files
authored
fix: namespace not loaded in registerTranslationLoader (#11)
1 parent f85c543 commit 7adcc83

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pistonite/celera",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"type": "module",
55
"private": true,
66
"description": "In-house UI framework",

src/i18n/init.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ import type { LocaleOptions } from "./types.ts";
77
import { createBackend } from "./backend.ts";
88
import Strings from "./strings.yaml";
99
import { registerTranslationLoader } from "./loaders.ts";
10+
import { once } from "@pistonite/pure/sync";
1011

1112
export const CELERA_NAMESPACE = "celerans";
1213

13-
/**
14-
* Initialize locale system in Pure and connect it with I18next
15-
*
16-
* This function calls `initLocale` internally, so you don't need to do that yourself.
17-
*/
18-
export const initLocale = async <TLocale extends string>(options: LocaleOptions<TLocale>) => {
19-
registerTranslationLoader(CELERA_NAMESPACE, loadCeleraTranslations);
14+
const initLocaleInternal = async <TLocale extends string>(options: LocaleOptions<TLocale>) => {
15+
await registerTranslationLoader(CELERA_NAMESPACE, loadCeleraTranslations);
2016

2117
const defaultLocale = options.default;
2218
let instance = i18next;
@@ -52,6 +48,11 @@ export const initLocale = async <TLocale extends string>(options: LocaleOptions<
5248
ns: [CELERA_NAMESPACE],
5349
});
5450
};
51+
/**
52+
* Initialize locale system in Celera and connect it with I18next
53+
* @function
54+
*/
55+
export const initLocale = once({ fn: initLocaleInternal });
5556

5657
const loadCeleraTranslations = async (language: string): Promise<Record<string, string>> => {
5758
const SupportedLocales = [

src/i18n/loaders.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ const namedspacedLoaders: Map<string, LoadLanguageFn> = new Map();
77
const namespacedAwaiters: Map<string, ((loader: LoadLanguageFn) => void)[]> = new Map();
88

99
/** Register a translation loader for a namespace */
10-
export const registerTranslationLoader = (namespace: string, loader: LoadLanguageFn) => {
10+
export const registerTranslationLoader = async (
11+
namespace: string,
12+
loader: LoadLanguageFn,
13+
): Promise<void> => {
1114
if (namedspacedLoaders.has(namespace)) {
1215
log.error(`translation namespace '${namespace}' is already registered`);
1316
return;
1417
}
1518
namedspacedLoaders.set(namespace, loader);
1619
const awaiters = namespacedAwaiters.get(namespace);
17-
if (!awaiters) {
18-
return;
20+
if (awaiters) {
21+
namespacedAwaiters.delete(namespace);
22+
awaiters.forEach((x) => x(loader));
1923
}
20-
namespacedAwaiters.delete(namespace);
21-
awaiters.forEach((x) => x(loader));
22-
void i18next.loadNamespaces(namespace);
24+
await i18next.loadNamespaces(namespace);
2325
};
2426

2527
export const getTranslationLoaderForNamespace = (namespace: string): Promise<LoadLanguageFn> => {

0 commit comments

Comments
 (0)