Internationalization (I18N)
Scooter Framework Supports I18N
Maybe you have noticed. When you first start your browser and hit the home page of your application, there is a welcome message displayed in your browser's local language setting. Yes, Scooter Framework supports i18n out-of-box. You can easily port your application to many different languages.
Config Message Files
You put locale-specific display messages in a property file under the config/locales directory.
A welcome message in French:welcome.message=Bienvenue!
The name of the property file should follow the convention as outlined in the Java Doc of java.util.ResourceBundle. Basically the name should start with a base message file name followed by ISO Language Code, optional ISO Country Code and optional variants separated by underscores.
The default base name of all message files is messages. You can change it by specifying your own base name in the message.resources.file.base property of the environment.properties file.
For our example above, we can put the French version of the welcome message in a file named messages_fr.properties.
Since Scooter Framework loads property files automatically, you do not need to restart your server when you drop in a new locale messages file or remove one.
The way Scooter Framework loads messages is the same as Java's ResourceBundle operates. It first looks through the specified Locale's language, country and variant, then through the default Locale's language, country and variant and finally using just the base:
base + "_" + localeLanguage + "_" + localeCountry + "_" + localeVariant base + "_" + localeLanguage + "_" + localeCountry base + "_" + localeLanguage base + "_" + defaultLanguage + "_" + defaultCountry + "_" + defaultVariant base + "_" + defaultLanguage + "_" + defaultCountry base + "_" + defaultLanguage base
Set Locale For The Whole Server
Optionally you can specify your application's Locale through the following properties in the environment.properties file:
- locale.language
- locale.country
- locale.variant
Again, you do not need to restart your web server when you make the change, as Scooter Framework can detect and reload updated property files instantly.
HTTP Request Header Priority
Scooter Framework uses HTTP request header priority.
When a HTTP request comes in, Scooter detects the language value in the request's Accept-Language header and sends back a response based on that value.
You can change the language option of your browser and view Scooter-powered web page displayed in your selected locale language.
Update Locale
You may retrieve and update the Locale object at runtime as the following code does.
//retrieve locale public Locale getLocale() { return ACH.getAC().getLocale();//This guarantees a locale object. //return ACH.getAC().getLocale(ActionContext.SCOPE_SESSION);//This may return null. } //update locale public void updateLocale(Locale newLocale) { ACH.getAC().setLocale(newLocale, ActionContext.SCOPE_SESSION); }
Read Localized Messages
In your Java code, you may directly use the get methods in the com.scooterframework.i18n.Messages class.
String errorMessage = Messages.get("validation.cannot_be_blank", "username") System.out.println("errorMessage: " + errorMessage);The above code will load the "validation.cannot_be_blank" property from a messages file.
validation.cannot_be_blank={0} cannot be blank.The output message will be:
errorMessage: username cannot be blank.
In your jsp view files, you can use the same com.scooterframework.i18n.Messages class as above too, but you are encouraged to use message methods from the view helper classe com.scooterframework.web.util.W. If you need to display friendly time message, you should use the message methods in com.scooterframework.web.util.D.
This blog was posted <%=D.message((Date)blog.getField("created_dt"))%>.You will then see this on the browser:
This blog was posted 3 days ago.