.json` ({ "English source": "Translation" }), passed to the * editor as window.ZADEX.i18n and read by `t()` (src/editor/core/i18n.ts). WordPress' own `wp.i18n` (text * domain `zadex`) still wins when a site ships real .mo/.json translations. * * The language is the user locale without its region (`ar`, `ar_SA`, `ary` -> `ar`); only languages * with a bundled JSON file get a dictionary, everything else gets `[]` (the editor then shows the English * source). Parsed dictionaries are cached per request and in the object cache keyed by file mtime. */ final class Translations { /** Languages with a bundled editor dictionary. */ public const LANGUAGES = ['ar']; /** @var array> */ private static array $cache = []; /** Regional variants that share a bundled dictionary (Moroccan, Algerian, Egyptian Arabic). */ private const ALIASES = ['ary' => 'ar', 'arq' => 'ar', 'arz' => 'ar']; public static function language(string $locale): string { // `ar`, `ar_SA`, `ar-EG` -> `ar`; `arg` (Aragonese) is not Arabic. $lang = strtolower((string) preg_replace('/[_-].*$/', '', trim($locale))); $lang = self::ALIASES[$lang] ?? $lang; return in_array($lang, self::LANGUAGES, true) ? $lang : ''; } public static function file(string $lang): string { return ZADEX_PATH . 'languages/zadex-' . $lang . '.json'; } /** @return array */ public static function for_locale(string $locale): array { $lang = self::language($locale); if ($lang === '') { return []; } if (isset(self::$cache[$lang])) { return self::$cache[$lang]; } $file = self::file($lang); if (!is_readable($file)) { return self::$cache[$lang] = []; } $key = 'zadex_i18n_' . $lang . '_' . (int) @filemtime($file); $cached = function_exists('wp_cache_get') ? wp_cache_get($key, 'zadex') : false; if (is_array($cached)) { return self::$cache[$lang] = $cached; } $json = wp_json_file_decode($file, ['associative' => true]); $dict = []; if (is_array($json)) { foreach ($json as $source => $translation) { if (is_string($source) && is_string($translation) && $translation !== '') { $dict[$source] = $translation; } } } if (function_exists('wp_cache_set')) { wp_cache_set($key, $dict, 'zadex'); } return self::$cache[$lang] = $dict; } /** Test helper. */ public static function flush(): void { self::$cache = []; } }