Locale guide
The Locale module provides a mechanism to build apps that adapt to users using different languages, timezones, currencies and other local unit standards.
To allow your app to work with different languages and localization settings, you must at least set up the availableLocales
, defaultLocale
, canonicalLocale
and availableLanguages
configuration keys in the config/Locale.config.php
file like this:
availableLocales
The different localizations your app will support. See the Locale configuration for an explanation of the available options.defaultLocale
The name of the locale to use as default.canonicalLocale
The name of the locale to be considered the main locale of the app.availableLanguages
An array of the languages that will be available in the app, from the availableLANGUAGE_?
constants.
If you don't setup your own
availableLocales
, or if you don't create a Locale configuration file at all, a default locale namedmain
will be used with the default configuration values.
Once your availableLocales
are in place, you can choose the locale to use by calling the Locale::setLocale method. From that point on, all the texts, dates, timezones and other localized data will be retrieved using that locale's setup. For example:
Multiple locales
If your app supports multiple localized versions with different languages or localization configurations, you just add more locales to your Locale.config.php
file. Imagine we have a web site with a global version in english, and a local version for Spain:
Now, we can switch the working locale whenever we need, and Locale will act accordingly:
See this example working in the Cherrycake documentation examples site.
Last updated