D
D
Dmitry2014-08-26 03:27:01
Internationalization and localization
Dmitry, 2014-08-26 03:27:01

How to rationally implement a multilingual site on Laravel?

The customer wants a store with a small turnover of goods + a blog. The admin panel will be in one, default language, but all content (name / description of products, blog articles, well, except for comments, I guess) should be in 3 languages.
Let's say the content routes are */{lang?} and the controller uses the $lang variable to make the appropriate fetch from the database.
What is the best way to duplicate fields in tables (content_ru, content_en, etc.) for each model or have separate tables and maybe even separate models for each language (posts_ru, posts_en)? Intuition suggests that the first option is clearly better, but maybe there is some kind of smarter solution, because in the first option it still smacks of code duplication.
Presentation templates: store all phrases for buttons like "Buy" in a separate class and
prescribe like {{ Words::buy($lang) }} Are there any other rational options?
Yes, and this is important, there are no plans to add languages ​​to the site.
PS. I know, people often ask about multilingualism, but I want to talk about it too :-)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2014-08-26
@DmitriyEntelis

Conditionally:
I would store in the goods table data about goods that do not depend on the locale (id, price, etc.), and store locale-dependent data in tables like goods_details_ru, good_details_en, etc.
This will avoid duplication of fields in tables (which is not very cool), will make it possible to easily expand the list of languages, + understandable queries without perversions to select the necessary data.

D
Dmitry, 2014-09-02
@akserdin

Thanks for the option.
I was thinking, reading, and then suddenly I came across a wonderful tutorial.
It still uses additional fields in the table with language prefixes - field_en, field_fr etc.
Then we add methods to the Model:
public function getTitleAttribute()
{
$locale = App::getLocale();
$column = "title_" . $locale;
return $this->{$column};
}
public function getContentAttribute()
{
$locale = App::getLocale();
$column = "content_" . $locale;
return $this->{$column};
}
Then you can use variables in the template like this:
{{ $post->title }}
{{ $post->content }}
Well, for phrases that only concern views (and are not stored in the database), there are trans() or Lang::get(..) methods.
If a language is added, then you will have to add fields to the tables, yes, but it's not so scary , like code duplication and redundant check logic. How do you like this decision?
PS here is the tutorial https://medium.com/laravel-4/laravel-4-multisites-...

L
lynnikvadim, 2015-04-19
@lynnikvadim

sergeymiracle.com/blog/laravel/multiyazychnyy-sayt...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question