0

What is the best logic to separate language files in Laravel?

I am about to make a decision out of two choices.

First is to collect all application button localization strings in buttons.php, link localization strings in links.php, placeholders localization strings in placeholders.php.

Like this :

/resources
    /lang
        /en
            buttons.php
            links.php
            placeholders.php
        /es
            buttons.php
            links.php
            placeholders.php

Second is to collect localization strings for each view in a separate file.

Like this :

/resources
    /lang
        /en
            login_page.php
            sign_up.php
            home.php
        /es
            login_page.php
            sign_up.php
            home.php

In first choice, I will end up with less number of language files as I will have about 10 files.

In second choice, I will end up with large number of language files as I will have a lang file for each view I have in project.

Which choice would be better?

gurkan
  • 3,457
  • 4
  • 25
  • 38

2 Answers2

0

In my experience, having a single translation file for all the ui works best, ordering the list in alphabetical order so you don't have multiple files with the same translation (for example: Submit, which may be used in multiple screens)

JoeGalind
  • 3,545
  • 2
  • 29
  • 33
0

So, the question is: "which method to separate translation/localization files is better: per view name or per element name?"

In my opinion, separating by view name does not make sense. If I make 3 view (view_a, view_b, view_c), each have a submit button, then I the localization for en (English) will have to define "submit" translation 3 times. Ex:

/resources
    /lang
        /en
            view_a.php // contains ["submit"=>"Submit form"]
            view_b.php // contains ["submit"=>"Submit form"]
            view_c.php // contains ["submit"=>"Submit form"]

This is not very DRY

Let's see by comparing how some open source laravel projects on github separate their localization files:

In Attendize/Attendize, there are localization files for each models instead. They also have some localization for common/reusable elements like basic

For example, this file: https://github.com/Attendize/Attendize/blob/develop/resources/lang/en/Order.php defines what translation for things that are talking about "Order" model. Therefore, if I'm showing message that says that there's no recent order, I'll just call en.Order.no_recent_orders which translates to Looks like there are no recent orders.; no matter where I call it from (from any view or from any controller)

Kristian
  • 2,456
  • 8
  • 23
  • 23