string dict from locales/{code}.php. // // Usage in a page: // require_once __DIR__ . '/../partials/i18n.php'; // adjust relative path // echo t('home.title'); // // Missing keys fall back to the English dict, then to the key itself. if (!function_exists('t')) { function _i18n_lang(): string { // Prefer SCRIPT_NAME: under nginx + php-fpm, PHP_SELF is often empty. $f = basename((string)($_SERVER['SCRIPT_NAME'] ?? $_SERVER['PHP_SELF'] ?? 'index.php')); if ($f === '') $f = 'index.php'; if (str_ends_with($f, '_zh.php')) return 'zh'; if (str_ends_with($f, '_jp.php')) return 'ja'; return 'en'; } function _i18n_load(string $code): array { static $cache = []; if (isset($cache[$code])) return $cache[$code]; $path = __DIR__ . '/../locales/' . $code . '.php'; $cache[$code] = is_file($path) ? (array)include $path : []; return $cache[$code]; } function t(string $key, ?string $lang = null): string { $lang = $lang ?? _i18n_lang(); $dict = _i18n_load($lang); if (array_key_exists($key, $dict)) return $dict[$key]; if ($lang !== 'en') { $en = _i18n_load('en'); if (array_key_exists($key, $en)) return $en[$key]; } return $key; } }