如何在yii2中将params文件转换为i18n

时间:2019-02-09 08:05:03

标签: yii2

我尝试在yii2中将params.php字符串转换为i18n,但是无法转换。

另一个字符串成功转换。

这是我的文件。

params.php

<?php

return [
    'bsVersion' => '4.x',
    'bsDependencyEnabled' => false,
    'adminEmail' => 'admin@example.com',
    'unauthorized_error'=>'You are not access this action.',
    'status_change_success'=>'Records status has been changed..',
    'records_save_success'=> Yii::t('params', 'Records save has been success..'),
    'status_change_error'=>'Records status not changed..',
    'records_delete_success'=>'Records has been deleted..',
    'records_delete_error'=>'Records not deleted..',
    'records_not_found'=>'Data not found..',
    'execution_error' => 'Oops!!! There was some problem while executing your request. Try again later.',
    'reset_password_request' => 'Your password reset link has been sent you in your email , please check your email inbox or spam folder',
    'password_change_success'=>'Passwod change successfully!!',
    'password_wrong' => 'Your password is wrong!',
    'inactive_user' => 'You have inactive user. Please contact to website admin',
    'not_registered_user' => 'You have not registered. Please register first then login',
    'enter_user_pass' => 'Enter username and password!',
    'something_wrong' => 'Something went wrong!',
    'server_error' => 'Server error',
    'student_not_found' => 'Student not found.',
    'teacher_not_found' => 'Teachers not found.',
    'validation_error' => 'Validation Error.',
    'comment_success' => 'Your Comment send successfully!.',
    'attendance_save_success' => 'Attendance save successfully!.',
    'homework_save_success' => 'Homework save successfully!.',
    'register_successfully' => 'User registered successfully!',
    'photo_gallery_path' =>'uploads/photo_gallery/',
    'appointment_status_change' =>'Appointments status has been changed!',
    'leave_application_status_change'=>'Leave application status changed..',
    'timetable_import_success' => 'Your timetable import successfully',

    'group_type' =>[
        "7" => "Hostel Rector",
        "8" => "Security"
    ],

    'day' => [
        '1' => Yii::t('params', 'Monday'),
        '2' => Yii::t('params', 'Tuesday'),
        '3' => Yii::t('params', 'Wednesday'),
        '4' => Yii::t('params', 'Thursday'),
        '5' => Yii::t('params', 'Friday'),
        '6' => Yii::t('params', 'Saturday'),
        // '7' => 'Sunday',
    ],

    ....

    'syllabus_status' =>[
        '1' => Yii::t('params', 'Running'),
        '2' => Yii::t('params', 'Finish'),
    ],
    'news_event' =>[
        '1' => Yii::t('params', 'News'),
        '2' => Yii::t('params', 'Event'),
    ],
    'exam_type' => [
        '1' => Yii::t('params', 'Weekly'),
        '2' => Yii::t('params', 'Monthly'),
        '3' => Yii::t('params', 'Yearly'),
    ],

    'lang_list' => [
        '1' => Yii::t('params', 'English'),
        '2' => Yii::t('params', 'Gujarati'),
    ]
];

1 个答案:

答案 0 :(得分:1)

您不能使用Yii::t()直接在配置文件中翻译参数-该文件在应用程序初始化之前就已使用,因此此时Yii无法检测到当前语言并且i18n尚未初始化。最简单的方法是推迟翻译-将未翻译的字符串放在params中:

'lang_list' => [
    '1' => 'English',
    '2' => 'Gujarati',
]

并在需要时进行翻译:

echo Yii::t('param', Yii::$app->params['lang_list']['1']);

或者,您可以在应用初始化后使用beforeRequest event来生成参数:

'on beforeRequest' => function ($event) {
    Yii::$app->params += [
        'syllabus_status' => [
            '1' => Yii::t('params', 'Running'),
            '2' => Yii::t('params', 'Finish'),
        ],
        'news_event' => [
            '1' => Yii::t('params', 'News'),
            '2' => Yii::t('params', 'Event'),
        ],
        'exam_type' => [
            '1' => Yii::t('params', 'Weekly'),
            '2' => Yii::t('params', 'Monthly'),
            '3' => Yii::t('params', 'Yearly'),
        ],
        'lang_list' => [
            '1' => Yii::t('params', 'English'),
            '2' => Yii::t('params', 'Gujarati'),
        ],
    ];
},