1

I am building my first multilingual Laravel website.

For the translations i am using this package: laravel-localization.

In my database i have the columns set up like this:

  • title (english title)
  • title_nl (dutch title)
  • title_fr (french title)

I have read the laravel documentation on Localization and set up my error messages this way. But now i'm confused on how to correctly show the different languages for my views.

Should i do something like this:

@if( LaravelLocalization::getCurrentLocale() == 'nl')
<p>{{$post->title_nl}}</p>

@elseif(LaravelLocalization::getCurrentLocale() == 'fr' )
 <p>{{$post->title_fr}}</p>

@else
  <p>{{$post->title}}</p>
 @endif

This seems extremely messy and not the correct way to handle this, because expanding this would be a nightmare.

Or do i need to use the build in localization functions for this like so:

{{ trans('post.title') }}

Or does that defeat the purpose of the package i am using?

Christophvh
  • 12,586
  • 7
  • 48
  • 70
  • You can use only `{{ trans('post.title') }}` , you dont need to add lang suffixes. Only detect lang `@if( LaravelLocalization::getCurrentLocale() == 'nl')` etc. – mcklayin May 30 '16 at 11:24
  • @mcklayin so that means i have to load my model in my post.php file for each language ? and loop trough the fields in the language file itself? – Christophvh May 30 '16 at 11:28

1 Answers1

1

Using key value pair of multidimensional arrays

Add translations in DB and get load in array when required

$language['word_key']="word";

keep word key in your code as it is just load translations in array based on language

in DB create 2 tables 1 for keywords which is primary table and other for translations which have foreign key of keywords table

Write a language class that can provide you array of key => value pair to use in code

example in simple php

echo $language['kw_hi'];

if you run with english it prints "Hi" in French "Salut" code remains same just content changes

{!! Form::label($dictionary['apo_add_subject'], $dictionary['apo_add_subject']) !!}

Above the dictionary has keyword apo_add_subject . i have created a table with keywords id,keyword_name and second table translations id , kw_id , language , translation then based on language and module i am loading it in array using middle were and then passing to view where code written above will used .

Just load different values in array keeping same keywords

fKnight
  • 36
  • 7
  • I don't really understand this solution fully. Can you make an example with the example i gave in my opening post? – Christophvh May 30 '16 at 12:31