Multi-language support


The application involves internationalization support, then you can define the relevant language package file

Open and load the language package

Default system The default language package will be loaded, but if you need automatic detection and automatic switching of multiple languages, you need to add the middleware definition in the global middleware definition file:

'think\middleware\LoadLangPack',

By default, the system loads the configuration default language pack and will not automatically detect the current system language. Multi-language related settings are set in the lang.php configuration file.

The default language is set by the default_lang configuration parameter. The system default setting is:

// 默认语言
'default_lang'    => 'zh-cn',

After enabling the middleware, the system will automatically detect and automatically switch between multiple languages. You can set automatic detection in the configuration file Multi-language variable name:

// 自动侦测的GET变量名
'detect_var' => 'lang',

After turning on automatic detection, it will first check whether the requested URL or cookie contains language variables, and then automatically identify the current language (and load the corresponding language pack) based on HTTP_ACCEPT_LANGUAGE.

If you want to set a list of allowed languages ​​when automatically detecting languages, and languages ​​that are not in the list will still use the default language, you can configure:

// 设置允许的语言
'allow_lang_list'    =>    ['zh-cn', 'en-us']

If you want to use cookies to save languages, You can set

// 使用Cookie保存
'use_cookie' => true,
// Cookie保存变量
'cookie_var' => 'think_lang',

After setting, the automatically detected language will be recorded through Cookie, and the language will be determined directly through Cookie next time.

Language variable definition

The definition of language variable only needs to be written in places where multiple languages ​​need to be used:

Lang::get('add user error');
// 使用系统封装的助手函数
lang('add user error');

In other words, characters String information needs to be represented by the Lang::get method.

Language definitions are generally described in English.

Language file definition

The automatically loaded application language file is located at:

// 单应用模式
app\lang\当前语言.php
// 多应用模式
app\应用\lang\当前语言.php

If you need to load other language packages, you can set it through extend_list , for example:

'extend_list'    =>    [
    'zh-cn'    => [
        app()->getBasePath() . 'lang\zh-cn\app.php',
        app()->getBasePath() . 'lang\zh-cn\core.php',
    ],
]

Currently, the core framework only has built-in zh-cn language package. If you need prompts in other languages, you can load it yourself by extending the language package.

ThinkPHP language file definition adopts the return array method:

return [
     'hello thinkphp'  => '欢迎使用ThinkPHP',
     'data type error' => '数据类型错误',
];

Usually multi-language is used in the controller, but the automatic verification function of the model class will use prompt information, this part also Multilingual features are available.

If the multi-language function is used (assuming that we have defined 'lang_var'=>'Title must!' in the current language package), you can use the following string to replace the original error message .

{%lang_var}

If you want to output language variables in the template without assigning values ​​in the controller, you can directly use the template engine special tag to directly output the value defined by the language:

{$Think.lang.lang_var}

You can output the current language package The lang_var language definition defined inside.

Variable incoming support

When language package is defined, incoming variables are supported. There are two ways

Use named binding method, for example:

'file_format'    =>    '文件格式: {:format},文件大小:{:size}',

Just pass in the variable value when outputting the language string in the template:

{:lang('file_format',['format' => 'jpeg,png,gif,jpg','size' => '2MB'])}

The second way is to use the format string. If you need to use a third-party translation tool, It is recommended to use this method to define variables.

'file_format'    =>    '文件格式: %s,文件大小:%d',

The way to output multiple languages ​​in the template is changed to:

{:lang('file_format',['jpeg,png,gif,jpg','2MB'])}

Language grouping

First you need to enable language grouping in the lang.php configuration file,

// 开启多语言分组
'allow_group'    =>    true

Then you can use grouping when defining multiple languages Define

return [
    'user'    =>    [
         'welcome'  => '欢迎回来',
         'login' => '用户登录',
         'logout' => '用户登出',
    ]
];

and then use the following method to obtain the multilingual variable

Lang::get('user.login');
lang('user.login');