-
-
Notifications
You must be signed in to change notification settings - Fork 2
FAQ
No — that is intentionally out of scope. initphp/translator is a micro
library: key lookup, nested keys, {name} interpolation, and default-language
fallback. If you need CARDINAL/ORDINAL plurals or ICU patterns, format the value
yourself and pass the result through $context:
$count = 3;
$word = $count === 1 ? 'item' : 'items';
$lang->translate('cart.summary', '{n} {word} in your cart', [
'n' => $count,
'word' => $word,
]);So a single absent translation can never take down a page. Missing content degrades gracefully (you get the key, your inline fallback, or the default-language value); missing configuration (bad path, missing file) fails loudly. See Exceptions and Keys & Fallback.
None functionally — _r() is a deprecated alias of translate(), and _e()
is a deprecated alias of render(). They are kept so 0.x code keeps working.
Prefer translate() / render() in new code. See
Migration (0.x → 1.0).
No. File mode is the default, so useFile() is optional. You only need
useDirectory() to opt into the multi-file layout — and it must be called
before the first language loads.
No. Once a language is loaded, calling useFile()/useDirectory() with a
different mode throws a TranslatorException. Re-applying the
same mode is a harmless no-op. Set the mode first, then setDir(), then
setDefault()/change().
No. Each language is read from disk once and cached in memory for the
lifetime of the Translator instance. Switching back to an already-loaded
language does not touch the disk.
The library relies on require, so your PHP OPcache caches the compiled
language files automatically in production. There is no separate translation
cache to configure.
You can, but only string leaves are returned as translations — a non-string
leaf (array, int, etc.) is treated as a miss. Keep translation values as
strings; pass dynamic numbers through $context instead.
Yes — that is exactly what the default-language fallback provides. Put shared or canonical strings in your default language; any key a translation omits falls back to it. See Keys & Fallback.
Translator is mutable (it caches loaded languages and tracks the active
language) and is designed for the typical single-request PHP model. Use one
instance per request; do not share a single instance across concurrent requests
in a long-running worker without your own synchronization.
PHP 8.1+, tested in CI on 8.1, 8.2, 8.3 and 8.4. See Installation.
- Bugs / feature requests: Issues
- Questions / ideas: Discussions
initphp/translator · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Usage
Reference
Practical Guides
Migration & Help