Options for localizing web app

2019-07-29 09:59发布

问题:

I'm working on an international website (PHP, custom lightweight MVC framework) which needs to show content in the user's native language. Content will primarily be text, but may be images as well.

Our current solution is one giant file, with separate functions for every piece of text to be translated, and a switch case in each function. This has become terrible to maintain and ins't particularly scalable either.

Our priorities are performance, maintainability (if we need to fix a bug or make a change, should happen in as few places as possible), and extensibility (allowing translators to add more languages). We expect to be working with at least 10–12 languages. We also need to consider HTML content inside text, such as <p></p> and links. Currently, we have the HTML inline in the returned text (which, obviously, isn't particularly maintainable). Some of the options we've considered:

Database table for translations, with columns for each language
+ Scalable to lots of content
- Not easy for translators to add/change content
- Lots of DB transactions, slow performance

One flat text file (CSV)
+ Converts to spreadsheets, easy for translators to work with
- Loading a giant file into memory may impact performance (not sure how much of an impact this will actually make)
- May be hard to maintain

One flat text file per language
+ Easy for translators to work with
? Better for performance (maybe)

Continuous integration system that creates multiple versions of each (MVC) view with embedded, localized content
+ Best for performance
+ Can be combined with CSV or other format that is easy for translators to use
- Complex to set up
- May be difficult to maintain

We've been looking for guides on how to deliver translated content in web apps, but haven't come across anything useful. Which one of these options is the best, and what other implementations have we not considered?

回答1:

Localization can be achieved using gettext extension of php and .po file type. Po file type is associated with FNU gettext. Using POEdit we can create .po and .mo file .po file contains text messages with your conversion as below

eng.po

msgid ""

msgstr ""

ch.po

msgid ""

msgstr ""

For detail please read here http://php.net/manual/en/book.gettext.php