问题
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?
回答1:
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:
- Django Porject - i18n, Definition
- PHP textdomain function
- What is bindtextdomain, textdomain in gettext?
- How to determine which catalog to be used
回答2:
This is a standard approach for other framework, e.g. 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'
来源:https://stackoverflow.com/questions/29929306/best-way-to-organize-your-localized-translation-file