Centum\Translation
The Translation component provides a simple way to translate strings into multiple languages.
Translated strings are stored within Centum\Interfaces\Translation\LocaleInterface
objects.
Centum\Translate\Locale(
string $code,
array $translations
);
Centum\Translation\Locale
implements Centum\Interfaces\Translation\LocaleInterface
.
use Centum\Translation\Locale;
$enLocale = new Locale(
"en",
[
"_footer" => [
"copyright" => "(c) 2025 Your Name.",
],
"index/index" => [
"greeting" => "Hello, {name}!",
"welcome" => "Welcome to this website.",
],
]
);
Translation strings are stored using domains and keys. This keeps your translations organised, scalable, and maintainable — especially when your project grows or you add more languages.
In the above example, the domains are "_footer"
and "index/index"
and the keys are "copyright"
, "greeting"
, and "copyright"
. You could have a domain for each view template you have.
Once we have a Locale object, we can create a Translator.
use Centum\Translation\Translator;
$translator = new Translator($enLocale);
Centum\Translation\Translator
implements Centum\Interfaces\Translation\TranslatorInterface
.
Translating Strings
To translate a string, use the translate()
method:
$translator->translate($domain, $key, $replaceValues = []);
$domain
: The section or category of the translation (e.g."_footer"
,"index/index"
).$key
: The specific string to translate within that domain (e.g."copyright"
,"greeting"
,"welcome"
).$replaceValues
(optional): An array of placeholder values to be replaced inside the translation string.
For example:
echo $translator->translate("index/index", "welcome");
The output will be:
Welcome to this website.
Placeholders / Replace Values
Strings can include placeholders, written as {placeholder}
. When calling translate()
, you can provide values to replace them. For example:
$translator->translate(
"index/index",
"greeting",
[
"name" => "Alice",
]
);
The output will be:
Hello, Alice!
Centum uses the ICU Message Format which allows placeholders to include data types, pluralisation rules, and conditional selections. You can try it out online using the Online ICU Message Editor.
Quick Summary
Variable Interpolation
{name}
Number formatting
{age, number}
{age, number, integer}
{price, number, currency}
{progress, number, percent}
Pluralisation
{count, plural,
one {1 item}
other {# items}}
{num_guests, plural,
=0 {No guests.}
=1 {Only 1 guest.}
=2 {Just 2 guests.}
other {# guests.}}
Select (conditional)
{gender, select,
male {He}
female {She}
other {They}}
Date Formatting
{date, date, short}
{date, date, medium}
{date, date, long}
{date, date, full}
{date, date, ::d MMMM yyyy}
(available symbols)
Time Formatting
{date, time, short}
{date, time, medium}
{date, time, long}
{date, time, full}