views:

49

answers:

4

I'm currently in the process of building a web application which will be used by people in different countries

Currently all the messages that I output (i.e. errors etc) are in English.

What is the best way to store language constants and retrieve them?

It's quite likely that the messages may need bits of information injected into them too like data the user entered etc,

For example we might have the error:

The email address '[email protected]' was not a valid email address.
A: 

I'd store them in database and retrieve by language. For example,

SELECT message FROM messages WHERE key = 'error_email' AND lang = 'de';

Just a very generic idea off the top of my head

It probably makes sense to store languages in a different table and use foreign keys.

Sejanus
The application is not database driven so I don't really want to add a database just for language constants.
Camsoft
In this case I used to use xml. However other suggestions might work better, I'm reading those links now. Didn't know before :)
Sejanus
A: 

Have you looked into XLIFF? I think this article is particularly informative.

Peter Bailey
This looks interesting but a little overkill for my project.
Camsoft
+4  A: 

GNU gettext provides tools and a framework for doing this. This is a decent article about using gettext in PHP.

Tom Castle
+4  A: 

The usual approach for injected info is using printf() codes, normally %s:

The email address '%s' was not a valid email address.

Beyond that, you have a number of options:

  • The gettext functions allow you to use specific tools to build the initial string files and keep track of translation status

  • Good old PHP constants are very handy if you only have a few strings to translate

Also, don't forget that localization does not merely consist on translating text. You also have to take into account stuff like:

  • Date and numeric formats
  • Plural / Singular forms
  • Alphabetical order
Álvaro G. Vicario