Multilingual texts
Locale allows you to change the language of your app based on the selected locale.
To use the multilingual text features of the Locale module, Cherrycake uses the cherrycake_locale_texts and cherrycake_locale_textcategories database tables with the following structure:
You can create this tables in your database by importing the locale.sql file you'll find in the Cherrycake skeleton repository, under the install/database directory.

Text categories

Optionally, texts in the database can be organized in categories, this might come in handy if you have lots of texts.
Add your text categories as rows to the cherrycake_locale_textcategories table, here's the columns specification:
  • code A unique identifier for the category, you'll be using it later to refer to this category in your code.
  • description Describe the kind of texts you'll be storing in this category.

Adding texts

Add translated texts as rows to the cherrycake_locale_texts table, with this column specification:
  • textCategories_id The category id assigned in the cherrycake_locale_textcategories table.
  • code A unique identifier for the text, you'll be using it later to refer to this text in your code.
  • description Describe this text, and the context where it will be used.
  • text_en The text in english.

Retrieving multilingual texts

To retrieve a text based on the current locale language, use the Locale::getText method. Imagine the cherrycake_locale_textcategories like this:
id
code
description
1
general
General texts used throughout the site.
And the cherrycake_locale_texts tables like this:
id
textCategories_id
code
description
text_en
text_es
1
1
test
The typical test text used as an example.
Hello world
Hola mundo
You would access the text like this:
echo $e->Locale->getText("general/test");
$e->Locale->setLocale("spain");
echo $e->Locale->getText("general/test");
Hello world
Hola mundo
See this example working in the Cherrycake documentation examples site.

Adding new languages

To add new languages, just add new columns to the cherrycake_locale_texts table. The column name must follow the syntax text_<language code> For example, to add a text field for spanish, add the a field named text_es.

Variables in localized texts

You can use simple variables in localized texts to help you build more complex sentences, like this:
textCategories_id
code
text_en
text_es
1
newMessages
You have {numberOfMessages} new messages.
Tienes {numberOfMessages} mensajes nuevos.
echo $e->Locale->getText("general/newMessages", [
"variables" => [
"numberOfMessages" => 5
]
]);
You have 5 new messages.
See this example working in the Cherrycake documentation examples site.
Copy link
On this page
Text categories
Adding texts
Retrieving multilingual texts
Adding new languages
Variables in localized texts