我使用的是Yii2高级模板。我已经实现了本教程之后的翻译(i18n),并复习了这个SO问题。是的,我阅读了文档。
我的翻译不起作用,我在调试器中发现它在前端文件夹中寻找翻译,而不是在消息/摘录创建翻译文件的公用文件夹中:
The message file for category 'app' does not exist: localhost/frontend/messages/es/app.php
我知道最简单的方法是将邮件文件夹移到前端文件夹,因为我没有在后端使用翻译,但我想了解我做错了什么。
这是我的i18n文件,位于common/config:中
'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'languages' => ['es'], //Add languages to the array for the language files to be generated.
'translator' => 'Yii::t',
'sort' => false,
'removeUnused' => false,
'only' => ['*.php'],
'except' => [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/vendor',
],
'format' => 'php',
'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'messages',
'overwrite' => true,
这是我的常用/config/main文件
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii'i18n'PhpMessageSource',
'basePath' => '@common/messages',
],
'backend*' => [
'class' => 'yii'i18n'PhpMessageSource',
'basePath' => '@common/messages',
],
],
],
这是定义别名的地方(default.common/config/bootstrap),回显@common返回common:
Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
您的代码是正确的,但从错误消息来看,您似乎正在调用:
Yii::t('app', '...');
相反,在common/config/main中,您为"frontend*"answers"backend*"声明了条目,但没有为"app*"声明条目。所以Yii将继续在前端存储库文件夹中搜索。
common/config/main应该包含(如果您想使用Yii::t('app',...')
):
'app*' => [
'class' => 'yii'i18n'PhpMessageSource',
'basePath' => '@common/messages',
],
使用您的配置,必须使用调用翻译
Yii::t('frontend','Frontend_string');
对于前端和
Yii::t('backend','Backend_string');
用于后端内容。
参见第6点。在教程中