Table field translation

All the places in the data table where the fields are used will automatically read the translations from the language pack.

{tip} See Multilingual for details on how to use the language package.

Language package name

If the controller is UserProfileController, the corresponding language package is resources/lang/{current language}/user-profile.php (needs to be converted to lower case strikethrough style).

If you want to change the name of the language pack, you can do so in the following two ways

Method 1

  1. use Dcat\Admin\Http\Controllers\AdminController;
  2. class UserController extends AdminController
  3. {
  4. /**
  5. * Specify the name of the translation file
  6. *
  7. * @var string
  8. */
  9. protected $translation = 'user1';
  10. ...
  11. }

Method 2

  1. use Dcat\Admin\Admin;
  2. Admin::translation('user1');

Example

Now suppose that the language package resources/lang/zh_CN/user-profile.php contains the following:

  1. return [
  2. 'fields' => [
  3. 'name' => '名称',
  4. 'age' => '年龄',
  5. 'class' => '班级',
  6. ],
  7. ];

The Grid' field set in the controllerUserProfileController` will automatically read the above translation:

  1. // Don't set labael to read translations automatically.
  2. $grid->id();
  3. $grid->name;
  4. $grid->age;
  5. $grid->class;
  6. $grid->filter(function ($filter) {
  7. $filter->gt('age');
  8. });
  9. // The above code is equivalent to
  10. $grid->name('名称');
  11. $grid->age('年龄');
  12. // Can also be used like this
  13. $grid->id(admin_trans_field('id'));
  14. $grid->name(admin_trans_field('name'));
  15. $grid->age(admin_trans_field('age'));

Public interpretation

When the admin_trans_field function can’t find the translation of a given field in the current controller, it looks for it in global.php. If some fields are present in many data tables, you can write those translations in the resources/lang/{current language}/global.php file.

  1. return [
  2. // Commonly used fields are placed in global.php and can be used by all controllers.
  3. 'fields' => [
  4. 'id' => 'ID',
  5. 'created_at' => '创建时间',
  6. 'updated_at' => '更新时间',
  7. ],
  8. ];