in a multi-language site, why would I store some text strings in a database and write other one into the markup?

coldfusion, internationalization, multilingual, mysql

Solution

You shouldn't store strings/translations in your code, that's bad practice if you want a maintainable i18n'd site.

You should store all your string in the same location, db or a properties file per language. It doesn't matter which, but be consistent. Personally I prefer a properties file as its easy to edit.

welcome_message=Hi {0}, Welcome to my site

Load all your translations in one go in onApplicationStart(), then provide a wrapper class to access them and to format the string with supplied arguments

for example

#i18n.getString(user.getLocale(), "welcome_message", [user.getUsername()])#

You can use java.text.MessageFormat[1] to provide powerful formatting

function getString(string locale, string key, array args) {
  var mf = createobject("java", "java.text.MessageFormat");
  mf.init(variables.strings[arguments.locale][arguments.key]);
  return mf.format(javacast("java.lang.Object[]", args));
}

The above is just an example, and you need to provide error catching and caching to this

Hope that helps point you in a productive direction

[1] http://docs.oracle.com/javase/7/docs/api/index.html?java/text/MessageFormat.html

Problem

I'm working on an existing multi-language site (Coldfusion/MySQL). Why is it that on a lot of pages I'm sitting on, some text strings are always hard-coded into the markup like: ``` <CFIF language = "EN"><p>Hello World</p></CFIF> ``` while others use the database to update text like so: ``` <p><cfoutput>#tx_greetings#</cfoutput></p> ``` What is the best practice here? I thought if I'm going to use a database for translations, it would be easier to store all texts in there (long and small). If I'm not using a database, then all texts should be if-elsed. Mixing it is a little maintenance-heavy, isn't it? Also, is there a limit on text-string-length, which I'm storing to MySQL? Maybe performance-wise? Thanks for some inputs!

Original source