使用 Laravel 进行多语言数据库管理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19088897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multilanguage database management with Laravel
提问by Maciej Swic
I'm creating an app with a backend in Laravel. The backend needs to manage a collection of objects which are downloaded to the app. The objects must be localised depending on the device language.
我正在 Laravel 中创建一个带有后端的应用程序。后端需要管理下载到应用程序的对象集合。必须根据设备语言对对象进行本地化。
Is there a simple way to do this in Laravel? Perhaps an eloquent or Laravel-plugin? I'd like to avoid writing the localisation support myself.
在 Laravel 中是否有一种简单的方法可以做到这一点?也许是一个雄辩的或 Laravel 插件?我想避免自己编写本地化支持。
(The built in localisation in Laravel is only for the interface, it doesn't support Eloquent objects)
(Laravel 内置的本地化只针对接口,不支持 Eloquent 对象)
回答by Glad To Help
You will need to write that on your own. First you will have to model your database tables to support multilanguage content and then in your model you will be able to say something like:
你需要自己写。首先,您必须对数据库表进行建模以支持多语言内容,然后在您的模型中,您将能够说出以下内容:
class Content extends Eloquent
{
public function scopeEnglish($query)
{
return $query->where('language', '=', 'en');
}
public function scopeSpanish($query)
{
return $query->where('language', '=', 'es');
}
}
class Post extends Eloquent
{
public function content()
{
return $this->hasMany('Content');
}
}
and then you can use it like:
然后你可以像这样使用它:
$englishContent = Posts::find($id)->content->english()->get();
$spanishContent = Posts::find($id)->content->spanish()->get();
回答by adrianthedev
Glad To Help's answer seems perfect for multilingual site's with many languages. I've found out that it's kind of clunky to do that when your site has just two/three languages.
Glad To Help 的回答对于多语言的多语言网站来说似乎是完美的。我发现当您的网站只有两种/三种语言时,这样做有点笨拙。
What I've done is having two columns for each translatable fields.
for the title
attribute I have in the database title_en
and title_es
.
After that, in the controller just set this method to do an automated translation.
我所做的是为每个可翻译字段设置两列。对于title
我在数据库中的属性title_en
和title_es
. 之后,在控制器中只需设置此方法即可进行自动翻译。
public function getTitleAttribute()
{
$locale = App::getLocale();
$column = "title_" . $locale;
return $this->{$column};
}
Now when you call Post::find($id)->title
it will automatically get the one for the current language.
现在,当您调用Post::find($id)->title
它时,它会自动获取当前语言的那个。
Hope it helps. Cheers!
希望能帮助到你。干杯!
回答by Alex
I did similar but more universal
我做了类似但更通用的
Schema::create('index', function(Blueprint $table) {
$table->increments('id');
$table->string('title_uk');
$table->string('title_ru');
$table->string('title_en');
$table->string('heading_uk');
$table->string('heading_ru');
$table->string('heading_en');
$table->string('photo');
$table->timestamps();
});
The model
该模型
public function TextTrans($text)
{
$locale=App::getLocale();
$column=$text.'_'.$locale;
return $this->{$column};
}
Now I for each language version as well as for each field will not prescribe a specific function, and cause all this:
现在我对于每个语言版本以及每个领域都不会规定一个特定的功能,并导致这一切:
$text=Index::find('1');
$text->TextTrans('title');
$text->TextTrans('heading');
$text=Index::find('1');
$text->TextTrans('title');
$text->TextTrans('heading');
回答by Marlon Marcello
Following the @user1067281 answer I found a great advanced tutorial for multi languages site.
在@user1067281 回答之后,我找到了一个很棒的多语言网站高级教程。
You should totally check this out: https://medium.com/laravel-4/26cdc75e4810
你应该完全检查一下:https: //medium.com/laravel-4/26cdc75e4810