Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pistonite/celera",
"version": "0.3.0",
"version": "0.3.1",
"type": "module",
"private": true,
"description": "In-house UI framework",
Expand Down
15 changes: 8 additions & 7 deletions src/i18n/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import type { LocaleOptions } from "./types.ts";
import { createBackend } from "./backend.ts";
import Strings from "./strings.yaml";
import { registerTranslationLoader } from "./loaders.ts";
import { once } from "@pistonite/pure/sync";

export const CELERA_NAMESPACE = "celerans";

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

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

const loadCeleraTranslations = async (language: string): Promise<Record<string, string>> => {
const SupportedLocales = [
Expand Down
14 changes: 8 additions & 6 deletions src/i18n/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ const namedspacedLoaders: Map<string, LoadLanguageFn> = new Map();
const namespacedAwaiters: Map<string, ((loader: LoadLanguageFn) => void)[]> = new Map();

/** Register a translation loader for a namespace */
export const registerTranslationLoader = (namespace: string, loader: LoadLanguageFn) => {
export const registerTranslationLoader = async (
namespace: string,
loader: LoadLanguageFn,
): Promise<void> => {
if (namedspacedLoaders.has(namespace)) {
log.error(`translation namespace '${namespace}' is already registered`);
return;
}
namedspacedLoaders.set(namespace, loader);
const awaiters = namespacedAwaiters.get(namespace);
if (!awaiters) {
return;
if (awaiters) {
namespacedAwaiters.delete(namespace);
awaiters.forEach((x) => x(loader));
}
namespacedAwaiters.delete(namespace);
awaiters.forEach((x) => x(loader));
void i18next.loadNamespaces(namespace);
await i18next.loadNamespaces(namespace);
};

export const getTranslationLoaderForNamespace = (namespace: string): Promise<LoadLanguageFn> => {
Expand Down
Loading