Search code examples
javajavascriptdatabasewebserverappserver

Translation of a few strings in a web application.


How to translate a few strings in a web application to a different language when the user wants to see those strings in his language?


Solution

  • are you familiar with i18n? it's short for "internationalization" which is a 20 letter word: first "i", then 18 letters, and then "n" :D

    some people like to use properties-files for this. you name the property-file after its i18n language name. so you would have for example en-us.properties and sw-se.properties for english vs swedish texts. in these files you would then have place holder names for some texts you want to display, and what their actual values would be. remember that place holders can't have spaces!

    en-us.properties would contain

    welcome_message = "welcome %user%\nplease check your weapons at the door :)"
    logout_button_text = "log out"
    good_bye_message = "godspeed to you %user%!"
    

    sw-se.properties would contain

    welcome_message = "välkommen %user%\nvänligen lämna dina vapen vid dörren :)"
    logout_button_text = "logga ut"
    good_bye_message = "%user%, vi önskar dig en lycklig resa!"
    

    i've made up this functionality where things within % signs are placeholders to be replaced after the language text has been retained, so that data from the session or the database can be placed within the message.

    note how the language files both contain the exact same keys, but with different values.

    when you want to display text,

    • you check what language you're in (maybe this comes from the url),
    • load the corresponding properties file,
    • get the text for the conceptual place holder that's needed at the given point,
    • replace any data place holders within this string,
    • and push the final result out to the user.