Skip to content

Translation Models

mnipper edited this page Dec 12, 2013 · 1 revision

If an instrument has a translation for the current language selected for the Android device, then it will automatically be displayed by the application. This works by each component model of an instrument having a corresponding translated component. For example, Instrument has the InstrumentTranslation model.

Setting a Language

The language can be set in one of two ways. The best method is to select the desired language in the Android OS Settings under "Language and input." However, if a language is not supported by Android and must be included manually as described in Adding a Language, then another method must be available. The alternate method is in the admin settings of the application. There is a field labeled "Custom ISO-639-1." If the lowercase version of the ISO-639-1 language is put in here, then this will override the language selected on the device for translations.

This is done in the Instrument model as follows:

    public static String getDeviceLanguage() {
        if (!AdminSettings.getInstance().getCustomLocaleCode().equals("")) {
            return AdminSettings.getInstance().getCustomLocaleCode();
        }
        return Locale.getDefault().getLanguage();
    }

Loading Translations

Translations are loaded as its corresponding model is synced from a translations JSON array.

For example:

            // Generate translations
            JSONArray translationsArray = jsonObject.getJSONArray("translations");
            for(int i = 0; i < translationsArray.length(); i++) {
                JSONObject translationJSON = translationsArray.getJSONObject(i);
                InstrumentTranslation translation = instrument.getTranslationByLanguage(translationJSON.getString("language"));
                translation.setInstrument(instrument);
                translation.setAlignment(translationJSON.getString("alignment"));
                translation.setTitle(translationJSON.getString("title"));
                translation.save();
            }

The getTranslationByLanguage() is necessary so that a translation may be found again and updated if necessary. If a translation for the current model is not found for the current language, then a new Translation object is returned.

Displaying Translations

Translations are displayed for their corresponding fields in the getter methods in each model. The logic is to return the default field immediately if the instrument language is the same as the desired language. If this is not true, iterate through translations to try to find a translation (Note: this could be refactored to store translations in a hash; this reeked of premature optimization at the time, however). If no translation is found for the desired language, then fall back to the default field value for the field. This is the language that the instrument was originally created with.

    public String getTitle() {
        if (getLanguage().equals(getDeviceLanguage())) return mTitle;
        for(InstrumentTranslation translation : translations()) {
            if (translation.getLanguage().equals(getDeviceLanguage())) {
                return translation.getTitle();
            }
        }
        
        // Fall back to default
        return mTitle;
    }