Structuring Website Translation files

前端 未结 3 1676
鱼传尺愫
鱼传尺愫 2021-02-08 14:41

I faced this problem several times while building websites. I will explain the using PHP and Laravel as an example but this problem is a common amoung multiple platforms. This w

相关标签:
3条回答
  • 2021-02-08 15:20

    In my experience there is no reason to have different groups other than trying to use your translations somewhere else. I usually put all my project messages in a group named app and for each of my shared libraries I use a separate group name (because I might use them in other projects). An example of a a failure login message in my website would be

    trans('app.username_and_password_do_not_match')
    

    and if it's in a third party library named Auth it would be

    trans('auth.username_and_password_do_not_match')
    

    And remember to write the full message as your message key instead of using short names (like app.login.fail). this way you don't need to check the website content for every translation.

    I didn't fully understand your last problem so you might want to clarify it a bit.

    0 讨论(0)
  • 2021-02-08 15:20

    I would go with option #4, so you'd have something like this:

    /lang/
        /en 
          messages.php
          words.php
        /fr
          message.php
          words.php
        /de
          messages.php
          words.php
    

    This does a few things:

    • It segments out everything very clearly. You know which language to find where. And you know what's in the file associated with the language.
    • The above makes maintenance easier in the future because you can find stuff.
    • It gives you files, by language, that can be translated separately.
    • It puts all the message in one clearly defined place.

    One thing to note, is that if your app gets REALLY big and REALLY international, you may want to use ISO language codes instead. For example, european Portugese (pt_PT) and Brazilian Portugese are different and with a global audience you'd probably want to cover both.

    0 讨论(0)
  • 2021-02-08 15:24

    I tend to group functionality in my Laravel apps into self-contained ‘components’. For example, I’ve been working on email campaign functionality for an application recently so put the service provider class, models, service classes in a folder at app/Email.

    Bearing this in mind, I organise my translations in a similar fashion. So even though on this project we’re not translating strings, if we were I would create a resources/assets/lang/en/email.php file, and put translated strings for the email component in there.

    So in another project, my directory structure might look like this:

    • /resources
      • /lang
        • /en
          • auth.php
          • email.php
          • events.php
          • news.php
          • pagination.php
          • passwords.php
          • validation.php

    Hope this helps.

    0 讨论(0)
提交回复
热议问题