问题描述
任何人都可以分享自己的经验和使用 Yii2 实现多语言网站的最佳实践吗?我想翻译存储在数据库中的用户输入.例如文章,它的名称可能是三种不同的语言、正文和一些可翻译的属性.
Can anyone share own experience and best practices implementing multilingual sites with Yii2? I want translate user input that is stored in database. For example article, that may have its name in three different languages, body and some translatable attributes as well.
Yii2 是否有内置功能来翻译动态内容?或者我应该使用像下面这样的第三方扩展程序:
Does Yii2 have built in features to translate the dynamic content? Or should I use third party extensions like these ones below:
https://github.com/creocoder/yii2-translateable
https://github.com/LAV45/yii2-translated-behavior
https://github.com/lajax/yii2-translate-manager
您的帮助将不胜感激.
推荐答案
好吧,我只能根据我所做的来告诉你我的观点.
Well, I can give you my point of view only based on what I have done.
有工作翻译的地方
- 使用 i18n 和消息系统管理的非动态字符串来自yii,这将帮助您处理静态内容.
- 使用引导类动态处理翻译后的路由,允许您在构建应用程序时构建此路由.
- 处理包含支持翻译的列的表格,如title_en、title_es",以及您需要翻译的数量.实际上,在您的管理界面中,您可能希望使用 yandex 之类的工具来帮助您将内容转换为该字段.
现在我来解释一下:
i18n 消息翻译是基于在您的视图、模型中以及在某些情况下(例如在引导类中)翻译字符串.
The i18n Message Translation is based on translating strings in your views, models, and in some cases like on the bootstrapped class.
您将使用 Yii::t('app/main', 'Your name is {0}'
作为示例来翻译存储在消息 php 文件中的字符串.
You will en using Yii::t('app/main', 'Your name is {0}'
as an example to translate strings that are stored on message php files.
现在,如果您翻译 stings,您将需要翻译路线,因此当您更改语言时,您将使用/articles 和/articulos 等路线.为此,您需要构建一个实现 BootstrapInterface 的类,并将在引导应用程序的过程中调用该类.
Now if you translate stings you will want to translate the routes so you will en with routes like /articles and /articulos when you change the language.for this purpose you will like to build a class that implements BootstrapInterface and that will be called from the process of bootstrapping your app.
这是我用于此目的的 settings.php 的示例
So this is an example of my settings.php that I use for this
namespace app\base;
use Yii;
use yii\base\BootstrapInterface;
class settings implements BootstrapInterface {
public function __construct() { }
public function bootstrap($app) {
/// Dynamic translated routes
$t_articles = Yii::t('app/route', 'articles');
$app->getUrlManager()->addRules([
'/'.$t_articles => '/articles',
], false);
}
}
记得在你的配置文件中引导类«即web.php»
And remember to bootstrap the class in your config file «i.e. web.php»
'bootstrap' => [
'log',
'app\base\settings',
],
最后要翻译数据库中的文本,您可能需要制作一个支持翻译文本的表格,例如:
And finally to translate text from the database you may want to make a table that supports the translated text like:
CREATE TABLE articles (
id INT,
title_en VARCHAR(20),
title_es VARCHAR(20)
);
因此,当您调用您的应用程序时,您可以在操作中使用类似以下内容的内容(仅是一个简单示例):
So when you call your app you can pull your data using something like the following on the action (only a simple example):
$articles = ArticlesA::find()->where(['id' => 1])->one();
$lang = $this->module->language;
return $thi
s->render('index',['articles'=>$articles,'lang'=>$lang]);
s->render('index',['articles'=>$articles, 'lang'=>$lang]);
或在视图中为:
<p class="lead"><?=$articles['title_'.$lang]?></p>
我希望这能解释我翻译应用程序的方式.
I hope this explains the way I have been translating my apps.
这篇关于Yii2 翻译动态内容的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!