Multilingual

It is very convenient to use multi-language translation in Dcat Admin. The fields of data table, data form, data detail and model tree all support automatic read language package translation, which can be referred to [form field translation] (model-grid-trans.md), [form field translation] (model-form-trans.md), [data detail field-translation] (model-show-trans.md).

Language package files

The file types of language packages are roughly as follows

  1. resources/lang
  2. ├── ...
  3. └── en
  4. ├── admin.php # System content language package, including menu TITLE translation, etc. are included
  5. ├── global.php # Controller Common Language Package
  6. ├── {xxx}.php # Controller language pack, one language pack for one controller.
  7. └── ...

For example, if the controller name is UserProfileController, the corresponding language pack is resources/lang/{current-language}/user-profile.php (which needs to be converted to lower-case strikethrough style).

Controller language package content format

The contents of the controller language package (including global.php) are divided into three categories.

  • fields Translation of data fields, which is placed under this category
  • labels Custom Content Translation, under this category is the translation of content outside of the data field, which can be any custom content
  • options Translation of enumeration options

The following are examples:

Suppose that the controller language package user-profile.php reads as follows:

  1. <?php
  2. return [
  3. 'labels' => [
  4. 'UserProfile' => '用户中心',
  5. 'list' => '列表',
  6. 'pagination' => [
  7. 'range' => '从 :first 到 :last ,总共 :total 条',
  8. ],
  9. ],
  10. 'fields' => [
  11. 'name' => '名称',
  12. 'published' => '发布',
  13. 'author' => '作者',
  14. 'status' => '状态',
  15. ],
  16. 'options' => [
  17. 'status' => [
  18. 0 => '未激活',
  19. 1 => '已激活',
  20. ],
  21. ],
  22. ];

then language packs can be used like this

  1. class UserProfileController extend AdminController
  2. {
  3. public function title()
  4. {
  5. // translates label UserProfile, which eventually translates to “用户中心”
  6. return admin_trans_label('UserProfile');
  7. }
  8. // fields and options translation Example
  9. public function grid()
  10. {
  11. $grid = new Grid(new UserProfile());
  12. // Displays the call language pack translation, where the "name" field is translated to "name"
  13. $grid->name(admin_trans_field('name'));
  14. // Implicitly using language pack translation, the "author" field is automatically translated to “作者”
  15. $grid->author;
  16. // Call options translation
  17. $grid->status()->using(admin_trans('user-profile.options.status'));
  18. return $grid;
  19. }
  20. }

use

admin_trans_field

This function is used to translate the content under the category fields, and will automatically find the translation file under the corresponding controller, if the translation does not exist, it will find the translation in global.php.

  1. admin_trans_field('name');

admin_trans_label

This function is used to translate the content under the category labels, and will automatically find the translation file under the corresponding controller, if the translation does not exist, it will find the translation in global.php.

  1. admin_trans_label('Posts');
  2. admin_trans_label('pagination.range', ['first' => 1, 'last' => 1, 'total' => 0]);

admin_trans_option

This function is used to translate the contents under the category options, it will automatically find the translation file under the corresponding controller, if the translation does not exist, it will find the translation in global.php.

  1. admin_trans_option(1, 'status');

admin_trans

This method is the same as the trans method Usage that comes with the Laravel framework; the only difference is that when the translation is not found, it goes to global.php and looks for it again.

  1. // Go to user.php and look for the translation of first_name, if you can't find it, go to global.php and look for it.
  2. admin_trans('user.first_name');

Publicly translated documents

All common translations can be placed in resources/lang/{current language}/global.php and the public translation file will be read for translation when the controller translation file does not exist.

  1. <?php
  2. return [
  3. 'fields' => [
  4. 'id' => 'ID',
  5. 'name' => '名称',
  6. 'username' => '用户名',
  7. 'email' => '邮箱',
  8. 'password' => '密码',
  9. ],
  10. 'labels' => [
  11. 'list' => '列表',
  12. 'edit' => '编辑',
  13. 'detail' => '详细',
  14. 'create' => '创建',
  15. 'root' => '顶级',
  16. 'scaffold' => '代码生成器',
  17. ],
  18. 'options' => [
  19. ],
  20. ];

Default breadcrumb translation

For example, if your access path is /admin/my-users and the controller is MyUserController, then you can add the following to the translation file corresponding to the controller

  1. return [
  2. 'labels' => [
  3. 'my-users' => '用户',
  4. ],
  5. ...
  6. ];