Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 16x 5x 5x 4x 5x 3x 2x 5x 5x 5x 5x 13x 12x 29x 15x 14x 12x 7x 5x 5x 6x 10x 3x 7x 5x 5x 5x 1x 5x 5x 2x 2x 1x 1x 8x 9x 11x 3x 3x 5x 11x 11x 1x 11x 5x 1x 5x | import { getLocales } from "expo-localization";
import { de } from "./translations/de";
import { en } from "./translations/en";
import { es } from "./translations/es";
import { fr } from "./translations/fr";
import { it } from "./translations/it";
import { ja } from "./translations/ja";
import { ko } from "./translations/ko";
import { pt } from "./translations/pt";
import type { TranslationFunction } from "./types";
// RN sets global __DEV__; read it off globalThis so this package type-checks
// standalone (no dependency on the React Native global typings).
const isDev = (globalThis as { __DEV__?: boolean }).__DEV__ ?? false;
// Matches Neurona's locale set (en, de, es, fr, pt, ja, ko) plus Italian.
export type SupportedLocale = "en" | "de" | "es" | "fr" | "it" | "pt" | "ja" | "ko";
export const SUPPORTED_LOCALES: SupportedLocale[] = [
"en",
"de",
"es",
"fr",
"it",
"pt",
"ja",
"ko",
];
/**
* Translation bundles. Glimme's string set is small, so all locales are bundled
* eagerly (Neurona lazy-loads its much larger set via require()).
*/
const BUNDLES: Record<SupportedLocale, Record<string, unknown>> = {
en: en as unknown as Record<string, unknown>,
de: de as unknown as Record<string, unknown>,
es: es as unknown as Record<string, unknown>,
fr: fr as unknown as Record<string, unknown>,
it: it as unknown as Record<string, unknown>,
pt: pt as unknown as Record<string, unknown>,
ja: ja as unknown as Record<string, unknown>,
ko: ko as unknown as Record<string, unknown>,
};
function warn(message: string): void {
if (isDev) {
console.warn(`[i18n] ${message}`);
}
}
function loadTranslations(locale: SupportedLocale): Record<string, unknown> {
return BUNDLES[locale] ?? BUNDLES.en;
}
/**
* Detect the best supported locale from the device settings.
*/
function detectLocale(): SupportedLocale {
try {
const deviceLocales = getLocales();
for (const { languageCode } of deviceLocales) {
if (languageCode && SUPPORTED_LOCALES.includes(languageCode as SupportedLocale)) {
return languageCode as SupportedLocale;
}
}
} catch {
// expo-localization not available (e.g. in tests)
}
return "en";
}
/** Current active locale */
let currentLocale: SupportedLocale = detectLocale();
/** Get the current locale */
export function getLocale(): SupportedLocale {
return currentLocale;
}
/** Set the active locale (device override / manual switch / tests) */
export function setLocale(locale: SupportedLocale): void {
currentLocale = locale;
}
/**
* Get nested value by dot path: 'pairing.title' -> 'Pair with your partner'
*/
function getByPath(obj: Record<string, unknown>, path: string): string {
const result = path.split(".").reduce<unknown>((acc, key) => {
if (acc && typeof acc === "object" && key in acc) {
return (acc as Record<string, unknown>)[key];
}
return undefined;
}, obj);
if (typeof result === "string") {
return result;
}
// Return the key as a fallback (helps surface missing translations).
warn(`Missing translation: ${path}`);
return path;
}
/**
* Get raw nested value (object/array/string) at a dot path.
* Returns undefined if the path doesn't exist.
*/
function getRawByPath(obj: Record<string, unknown>, path: string): unknown {
return path.split(".").reduce<unknown>((acc, key) => {
if (acc && typeof acc === "object" && key in acc) {
return (acc as Record<string, unknown>)[key];
}
return undefined;
}, obj);
}
/**
* Get raw typed value at a path from the current locale, falling back to English.
*/
export function tRaw<T = unknown>(key: string): T | undefined {
let value = getRawByPath(loadTranslations(currentLocale), key);
if (value === undefined && currentLocale !== "en") {
value = getRawByPath(BUNDLES.en, key);
}
return value as T | undefined;
}
/**
* Normalize an array-like translation subtree into a real array.
*/
export function tArray(key: string): string[] {
const raw = tRaw<Record<string, string> | string[]>(key);
if (!raw) return [];
Iif (Array.isArray(raw)) return raw;
return Object.keys(raw)
.sort((a, b) => Number(a) - Number(b))
.map((itemKey) => raw[itemKey] ?? "");
}
/**
* Interpolate values into a translation string.
* Replaces {{key}} with values from the params object.
*/
function interpolate(text: string, params?: Record<string, string | number>): string {
if (!params) return text;
return text.replace(/\{\{(\w+)\}\}/g, (match, key) => {
return params[key] !== undefined ? String(params[key]) : match;
});
}
/**
* Translation function for use anywhere (inside or outside React).
* Tries the current locale first, falls back to English if the key is missing.
*/
export const t: TranslationFunction = (
key: string,
params?: Record<string, string | number>,
): string => {
let text = getByPath(loadTranslations(currentLocale), key);
if (text === key && currentLocale !== "en") {
text = getByPath(BUNDLES.en, key);
}
return interpolate(text, params);
};
/**
* Translation hook for React components.
*/
export function useTranslation() {
return { t, locale: currentLocale };
}
export { en };
export type { TranslationFunction, TranslationKey, TranslationParams } from "./types";
|