Search code examples
databaselaravelmultilingual

Laravel - Translating data after fetching from databsase OR to have translated text in database?


I want to start a multilingual website with Laravel.

Is it a good idea to:

  1. Translate an string from fetched data from database using trans()?

OR

  1. Have three columns for each language with different titles?

For example in number 1 I have:

ID - title
1  - hello
2  - how_are_you

$texts = Text::all();
$translatedText = trans('somefile.'.$texts[0]->title);

And for number 2 I have:

ID - title_fa - title_en    - title_ar
1  - درود     - Hello       - سلام
2  - خوبی؟    - How are you - کیف احوالک؟


$texts = Text::all();
$translatedText = $texts[0]->title_fa;

Which one is better from different aspects?


Solution

  • It depnds on your requirements and the amount of data you would like to change. Both have their own pros and cons.

    1. Using translation file:

    Your database would be clean and you don't need to worrie about the conditional fields display. So only the effective data would be there no redundant data.

    If you are uncentain about text, would be difficult to add translation about that.


    2. Using database:

    You would have more flexibility to change/update the translated text dynamically from UI interface if you wish.

    You can add conditions in Accessor for setting attribute values so not need to worrie inside blade.

    You don't need to worrie about the field content becuase there would be separate translated field for that.

    Database performance could be impacted if you have large set of data.


    So based on your requirements of application you can choose the approach best fit to you.

    At last, if you are going with database approach, you should create different translation tables instead of inserting columns in same table.

    Like you have 5 fields in table1 where you need to keep translation for 3 fields then you should create different table like table1_translation with those 3 fields and keep only 2 fields in table1