@shelchin/svelte-i18n
Version:
The last Svelte i18n library you'll ever need. Type-safe, AI-powered, zero-config.
70 lines (69 loc) • 1.96 kB
JavaScript
/**
* Utility functions for translation management
* These functions help organize and access translations
*/
/**
* Get all available locales from a translation registry
*/
export function getAvailableLocales(registry) {
const locales = new Set();
for (const namespace of Object.values(registry)) {
for (const locale of Object.keys(namespace)) {
locales.add(locale);
}
}
return Array.from(locales);
}
/**
* Get translation for a specific namespace and locale
*/
export function getTranslation(registry, namespace, locale) {
return registry[namespace]?.[locale];
}
/**
* Merge all translations for a specific locale across namespaces
*/
export function getMergedTranslations(registry, locale) {
const merged = {};
// First add app translations (no prefix)
const appTranslation = registry.app?.[locale];
if (appTranslation) {
Object.assign(merged, appTranslation);
}
// Then add namespaced translations
for (const [namespace, translations] of Object.entries(registry)) {
if (namespace === 'app')
continue;
const translation = translations[locale];
if (translation) {
// Add with namespace prefix
for (const [key, value] of Object.entries(translation)) {
merged[`${namespace}.${key}`] = value;
}
}
}
return merged;
}
/**
* Check if a locale is available in the registry
*/
export function isLocaleAvailable(registry, locale) {
for (const namespace of Object.values(registry)) {
if (locale in namespace) {
return true;
}
}
return false;
}
/**
* Get all namespaces in the registry
*/
export function getNamespaces(registry) {
return Object.keys(registry);
}
/**
* Get all locales for a specific namespace
*/
export function getNamespaceLocales(registry, namespace) {
return Object.keys(registry[namespace] || {});
}