简体   繁体   中英

Best way to organize your localized translation file

As I've started building a project, there will be quite a few entries in the .po translation file. I use Poedit to build these.

My question is, what is the best practice for entries within this file? I was thinking, instead of referencing entries such as:

echo _('This is an entry.');

I was thinking of organizing them like:

echo _('error_pwd');
echo _('error_user_taken');

Which, once ran through the translation file, would output something like:

Password incorrect. Please try again.
Username is already taken. Please try another.

So, all my translations can be organized by type, such as error_ , msg_ , status_ , tip_ , etc.

Has anyone seen it done this way, or have any suggestions on a more organized method?

This is a standard approach for other framework, eg Symfony/Laravel:

trans('error.validation');

But it has a downfall, if you forget to translate one phrase on your site it will appear like the keyword 'error.validation'

In fact it doesn't matter!

It's just up to you.

However, I advise you do not split translations in sections. No there's any benefit in doing so. Actually, the most projects use the one file approach for all msgid entries. Like django, see .

Of course, If you still want split translation by sections, might you want take a look on Domains :

From PHP doc:

This function (textdomain()) sets the domain to search within when calls are made to gettext(), usually the named after an application.

Also, as earlier said, the advantage when using msgid as a real phrase ou word (instead underline or dotted notation key) is that it stays as default message if no there's a translation for entry.

And here goes some helpful links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM