Home >Backend Development >PHP Tutorial >How to use i18n in CakePHP?
CakePHP is an open source PHP framework that supports the integration of multi-language internationalization (i18n). i18n can make your application multilingual, making it easier for you to attract international audiences and better serve local users. In this article, we will take an in-depth look at the usage of CakePHP i18n.
First, you need to enable CakePHP’s multi-language functionality. To do this, update the configuration file config/bootstrap.php and add the following code to your application:
Configure::write('App.defaultLocale', 'en_US'); //设置默认语言 Configure::write('App.languages', ['en_US', 'es_ES', 'de_DE']); //可选语言
Next, you need Create language files in the config/Locale directory. These language files should be named locale%iso-639-1code%iso-3166-1-alpha-2 format.php. For example, en_US.php, es_ES.php, or de_DE.php, etc.
In the language file you can define translation strings. For example, in en_US.php you could write:
return [ 'welcome' => 'Welcome', 'hello' => 'Hello', ];
In es_ES.php you could write:
return [ 'welcome' => 'Bienvenido', 'hello' => 'Hola', ];
In de_DE.php you could write:
return [ 'welcome' => 'Willkommen', 'hello' => 'Hallo', ];
In the view, you can use the __() function to translate text. For example, if you wanted to display "Welcome" in your view, you could write:
<h1><?php echo __('welcome'); ?></h1>
This will automatically use the correct translation string for the current locale. If the locale is en_US, Welcome will be displayed. If the locale is es_ES, Bienvenido will be displayed.
Similarly, you can use __('string', array('name' => 'value')) in the view file to format the output.
<?php echo __('My name is %name%', array('name' => 'Lucas')); ?>
Output My name is Lucas.
In the controller, you can use Configure::write('Config.language', 'langauge') to change the current locale:
Configure::write('Config.language', 'de_DE'); //将语言环境设置为de_DE
You can set different locales in the controller's actions to select different translation strings for each view as needed. This is very useful for dynamic websites.
For example:
public function index() { Configure::write('Config.language', 'es_ES'); $this->set('title', __('home_title')); }
In this example it will load es_ES.php and use the translation string home_title.
CakePHP i18n also supports localized date and time formats. For example:
echo $this->Time->nice(new DateTime('2018-11-20')); //在控制器分配或直接用数组传递时使用 echo $this->Time->nice($dateObject);
This will format the date according to the current locale and display results similar to Nov 20th, 2018. You can modify the date format in app.php.
'defaultDateFormat' => 'yyyy-MM-dd',
As you can see, CakePHP’s i18n is very simple to use. By using the i18n support provided by the framework, you can quickly and easily provide multi-language and localization support to users around the world. Remember to always provide clear, accurate text for your application for the best user experience.
The above is the detailed content of How to use i18n in CakePHP?. For more information, please follow other related articles on the PHP Chinese website!