-
Notifications
You must be signed in to change notification settings - Fork 30
Multi Language Support
This section explains how the library supports language localization.
An Android client application wishing to use MLS needs to take the following steps:
- Provide a single placeholder-injected JsonForm where String literals to be translated are replaced with placeholders of the form
{{string_identifier}}
, wherestring_identifier
is a unique identifier for the String literal being replaced. - Provide a translations property file that contains mappings of the
string_identifier
s from 1. to the actual String literals thestring_identifier
s replace in the JsonForm from 1. This property file should be stored in theresources
folder of your Android project. - Append the name of the property file from 2. to the placeholder-injected JsonForm from 1. This would take the form:
"properties_file_name": "basic_form"
whereproperties_file_name
is the key of the field to append to the placeholder-injected JsonForm from 1. andbasic_form
is the name of the property file from 2. Notice that there is no need to append the locale-specific identifier or the.properties
extension. - Step 2. will be repeated for each language that the Android client wishes to support.
- The
string_identifier
s would remain the same but the String literals would change - taking on their translated versions for each locale. - To distinguish between the different property files for each locale, the following convention should be used e.g.
basic_form_en_US.properties
for US English andbasic_form_fr.properties
for French.
- The
- Set the
JsonFormConstants.PERFORM_FORM_TRANSLATION
intent extra to true when launching each JsonForm to activate MLS for that form (and its sub-forms if any).
The process of generating the assets in steps 1-3 may be a bit tedious and error prone. To this end, a utility is provided to ease the process. The tool (JsonForm MLS Asset Generator
) is described below.
This section describes how to configure JMAG for use with Android Studio.
JMAG is a pure Java class, meaning it DOES NOT and SHOULD NOT contain Android dependencies. Additionally, the elements it interacts with should not have Android dependencies as part of their instantiation process, otherwise the tool won't run.
JMAG is run through Android Studio as part of an Android Studio configuration. Here is some background on adding an Android Studio configuration.
As part of the JMAG configuration, the following steps will need to be taken:
- On the Android Studio menu click
Run -> Edit configuration
. - On the
Run/Debug Configurations
dialog, clickplus icon -> Application
. - Add the following configurations:
- Name:
JsonForm MLS Asset Generator
- Main class:
com.vijay.jsonwizard.utils.JsonFormMLSAssetGenerator
- Use classpath of module:
<your-module-name>
- JRE:
Android API 21 Platform
(you can use a different API level)
- Name:
- Within the same
Run/Debug Configurations
dialog, specify the JsonForm against which assets are to be generated:- Create an environment variable named
FORM_TO_TRANSLATE
and set its value to the absolute path of the JsonForm to translate. - This environment variable will need to be reset any time a different form needs to be translated.
- Optionally create an environment variable named
JSON_FORM_INTERACTOR_NAME
and specify the fully qualified name of a customJsonFormInteractor
.
- Create an environment variable named
- Press
Apply
andOK
- To run JMAG: switch to your newly created configuration and click the
Run
button.
The final configuration should look something like:
Two asset files will be created under the /tmp
folder : a translations property file and a placeholder-injected JsonForm.
Alternatively, a different folder can be specified by setting the MLS_ASSETS_FOLDER
environment variable, just ensure the folder exists and has the correct write permissions.
For a form named example_form
the generated assets will follow the following naming convention : example_form.json
and example_form.properties
.
The properties file can then be copied over to the resources
folder of your Android project under src/main
. The placeholder-injected JsonForm will typically be copied over to the assets
folder of your Android project (although not mandatory).
Android Support Client wishing to translate database keys or strings should take into account the following
- In native form library there exists a method called
translateDatabaseString
in classNativeFormLangUtils
. The function accepts as string to translate and a context which is an instance of Android Intent Context. - The string has to meet the following criteria:
- should be separated by
. (dot)
- The first index should be the same as the resource bundle name which contains keys and value to translate. e.g
anc_profile.danger_signs.danger_none.text
.Hereanc_profile
is the resource bundle name - Should be a
.text
extension iv) Should exist in the resource bundle name specified. If not, an exception is thrown.
- The function will take care of MLS support based on the
context
passed. Default isen
or ifcontext
is null. i.e If context has fr as the locale and resource bundle name isanc_profile_fr
then it will locate the key and if exists return the value as a string.
Code snippet to translate a string from database
/**
* @param key is the string to translate e.g anc_profile.step4.text
* This key has a first item of anc_profile which is the resource bundle name. For this to work, rename texts with bundle_name as the first item separated by .
* @param context this is the application context which is an instance of Android.intent.Context
* @return The value as a string depending on locale if found
*/
public static String translateDatabaseString(String key, Context context) {
String resourceBundleName = key.split("\\.")[0].trim();
Locale currLocale = context == null ? Locale.getDefault() : getLocale(context);
ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceBundleName, currLocale);
if (!resourceBundle.containsKey(key)) {
Timber.e("Could not translate String %s. String not found in resource bundle %s ", key, resourceBundleName);
return "";
}
return resourceBundle.getString(key);
}
Forked from Android Native JSON Form Library. Adapted in love by the OpenSRP Community . Apache License, Version 2.0
Introduction
Core Features
Form
Views