Configuration parameter
Using the i18n function requires a total of three steps
- Import language text in the resource folder
- Configure related parameters in the configuration file
- Call related methods to complete text conversion
1. Import text
We can create language group folders and text files under the resource/language/ folder. There can be multiple template files in a language group, as shown below.
# resource/language
|-- zh
|-- default.php
|-- msg.php
`-- en
|-- default.php
|-- msg.php
The text format is composed of associative arrays. The key value is text, and we can insert parameters in it. The format is as follows:
// ../en/default.php
return [
// 文本中可用大括号注入参数
'sayhello' => 'Hey {name}!',
'saygoodbye' => 'Bye!',
];
// ../en/msg.php
return [
'sayhello' => "Wath's up! {name}",
'saygoodbye' => 'See you tomorrow!',
];
// ../zh/default.php
return [
'sayhello' => "早上好,{name}",
'saygoodbye' => '再见',
];
// ../zh/msg.php
return [
'sayhello' => "晚上好,{name}",
'saygoodbye' => '明天见',
];
2. Related configuration
The i18n related function configuration is very simple. Just configure the following parameters in the app/bean.php configuration file to enable the internationalization function. See the comments for the parameter description.
return [
// .... 其他配置
'i18n' => [
// 设置到文本资源目录
'resoucePath' => '@resource/language/', // 结尾斜线必须
// 设置默认文本文件夹名称
// 未填写则默认 en 文件夹
'defaultLanguage' => 'en',
// 设置默认文本文件名称
// 未填写则默认 default.php
'defualtCategory' => 'default',
],
// .... 其他配置
];
At this point, the relevant parameters have been configured, you can use this function below.