Search in sources :

Example 1 with NoLocalizedTextException

use of org.javarosa.core.util.NoLocalizedTextException in project javarosa by opendatakit.

the class LocalizerTest method testGetText.

public void testGetText(int i, int j, int k, String ourLocale, String otherLocale, String textID, int localeCase, int formCase) {
    // System.out.println("testing getText: "+localeCase+","+formCase+","+i+","+j+","+k);
    Localizer l = buildLocalizer(i, j, k, ourLocale, otherLocale);
    String expected = expectedText(textID, l);
    String text, text2;
    text = l.getText(textID, ourLocale);
    if (expected == null ? text != null : !expected.equals(text)) {
        fail("Did not retrieve expected text from localizer [" + localeCase + "," + formCase + "," + i + "," + j + "," + k + "]");
    }
    try {
        text2 = l.getLocalizedText(textID);
        if (expected == null) {
            fail("Should have gotten exception");
        } else if (!expected.equals(text2)) {
            fail("Did not retrieve expected text");
        }
    } catch (NoLocalizedTextException nsee) {
        if (expected != null) {
            fail("Got unexpected exception");
        }
    }
}
Also used : Localizer(org.javarosa.core.services.locale.Localizer) NoLocalizedTextException(org.javarosa.core.util.NoLocalizedTextException)

Example 2 with NoLocalizedTextException

use of org.javarosa.core.util.NoLocalizedTextException in project javarosa by opendatakit.

the class Localizer method getLocaleData.

/**
 * Get the set of mappings for a locale.
 *
 * @param locale Locale
 * @returns HashMap representing text mappings for this locale. Returns null if locale not defined or null.
 */
public OrderedMap<String, String> getLocaleData(String locale) {
    final CodeTimer codeTimer = new CodeTimer("getLocaleData");
    if (locale == null || !this.locales.contains(locale)) {
        return null;
    }
    // It's very important that any default locale contain the appropriate strings to localize the interface
    // for any possible language. As such, we'll keep around a table with only the default locale keys to
    // ensure that there are no localizations which are only present in another locale, which causes ugly
    // and difficult to trace errors.
    final OrderedMap<String, Boolean> defaultLocaleKeys = new OrderedMap<>();
    // This table will be loaded with the default values first (when applicable), and then with any
    // language specific translations overwriting the existing values.
    final OrderedMap<String, String> data = new OrderedMap<>();
    // the current locale to overwrite any differences between the two.
    if (fallbackDefaultLocale && defaultLocale != null) {
        for (LocaleDataSource defaultResource : localeResources.get(defaultLocale)) {
            loadTable(data, defaultResource.getLocalizedText());
        }
        for (String key : data.keySet()) {
            defaultLocaleKeys.put(key, Boolean.TRUE);
        }
    }
    for (LocaleDataSource resource : localeResources.get(locale)) {
        loadTable(data, resource.getLocalizedText());
    }
    // a locale that doesn't contain the key.
    if (fallbackDefaultLocale && defaultLocale != null) {
        StringBuilder missingKeys = new StringBuilder();
        int keysMissing = 0;
        for (String key : data.keySet()) {
            if (!defaultLocaleKeys.containsKey(key)) {
                missingKeys.append(key).append(",");
                keysMissing++;
            }
        }
        if (keysMissing > 0) {
            // Is there a good way to localize these exceptions?
            throw new NoLocalizedTextException("Error loading locale " + locale + ". There were " + keysMissing + " keys which were contained in this locale, but were not " + "properly registered in the default Locale. Any keys which are added to a locale should always " + "be added to the default locale to ensure appropriate functioning.\n" + "The missing translations were for the keys: " + missingKeys, missingKeys.toString(), defaultLocale);
        }
    }
    codeTimer.logDone();
    return data;
}
Also used : OrderedMap(org.javarosa.core.util.OrderedMap) CodeTimer(org.javarosa.core.util.CodeTimer) NoLocalizedTextException(org.javarosa.core.util.NoLocalizedTextException)

Aggregations

NoLocalizedTextException (org.javarosa.core.util.NoLocalizedTextException)2 Localizer (org.javarosa.core.services.locale.Localizer)1 CodeTimer (org.javarosa.core.util.CodeTimer)1 OrderedMap (org.javarosa.core.util.OrderedMap)1