-
Notifications
You must be signed in to change notification settings - Fork 6
Adding a Language
Android has decent support for rendering most languages, and most of them will just work. However, there are exceptions and it may be necessary to manually add a font so that a language may be rendered correctly.
As an example, we will do a walkthrough of what is necessary to add Khmer.
An Instrument must have both a language and an alignment. These are constants located in the config/settings.yml file in the corresponding Rails application. If Android does not render a desired language correctly, then the font will have to be included in the assets/fonts directory of the Android application. For example, Khmer is khmerOS.ttf.
Next, in order to render the correct Typeface, the Android application must be made aware of this font. To do so, link the language constant specified in the Rails application settings.yml to the font file placed in the Android application's font assets directory. This is done in the Typeface getTypeFace(Context) method in the Instrument model. For example, khmer is linked to its correct font as follows:
public Typeface getTypeFace(Context context) {
if (getLanguage().equals("khmer")) {
return Typeface.createFromAsset(context.getAssets(), "fonts/khmerOS.ttf");
} else {
return Typeface.DEFAULT;
}
}
If a font does not match, then it just returns the default typeface for Android for the detected language.
If Android correctly renders a font, it should also correctly set its alignment. However, to allow flexibility if Android does not recognize a right-to-left language the alignment can be manually set through a constant in the rails application. This returns the corresponding Gravity constant that may be used on UI elements.
public int getDefaultGravity() {
if (getAlignment().equals("left")) {
return Gravity.LEFT;
} else {
return Gravity.RIGHT;
}
}