Search in sources :

Example 1 with TableLocaleSource

use of org.javarosa.core.services.locale.TableLocaleSource in project javarosa by opendatakit.

the class LocalizerTest method testAddLocaleWithData.

public void testAddLocaleWithData() {
    Localizer l = new Localizer();
    final String TEST_LOCALE = "test";
    TableLocaleSource localeData = new TableLocaleSource();
    localeData.setLocaleMapping("textID", "text");
    if (l.hasLocale(TEST_LOCALE)) {
        fail("Localizer reports it contains non-existent locale");
    }
    l.addAvailableLocale(TEST_LOCALE);
    l.registerLocaleResource(TEST_LOCALE, localeData);
    if (!l.hasLocale(TEST_LOCALE)) {
        fail("Localizer reports it does not contain newly added locale");
    }
    if (!localeData.getLocalizedText().get("textID").equals(l.getRawText(TEST_LOCALE, "textID"))) {
        fail("Newly stored locale does not match source");
    }
}
Also used : TableLocaleSource(org.javarosa.core.services.locale.TableLocaleSource) Localizer(org.javarosa.core.services.locale.Localizer)

Example 2 with TableLocaleSource

use of org.javarosa.core.services.locale.TableLocaleSource in project javarosa by opendatakit.

the class XFormParser method parseTranslation.

private void parseTranslation(Localizer l, Element trans) {
    // ///for warning message
    List<String> usedAtts = new ArrayList<>();
    usedAtts.add("lang");
    usedAtts.add("default");
    // ///////////////////////
    String lang = trans.getAttributeValue("", "lang");
    if (lang == null || lang.length() == 0) {
        throw new XFormParseException("no language specified for <translation>", trans);
    }
    String isDefault = trans.getAttributeValue("", "default");
    if (!l.addAvailableLocale(lang)) {
        throw new XFormParseException("duplicate <translation> for language '" + lang + "'", trans);
    }
    if (isDefault != null) {
        if (l.getDefaultLocale() != null)
            throw new XFormParseException("more than one <translation> set as default", trans);
        l.setDefaultLocale(lang);
    }
    TableLocaleSource source = new TableLocaleSource();
    Collection<Integer> removeIndexes = new HashSet<>();
    for (int j = 0; j < trans.getChildCount(); j++) {
        Element text = trans.getElement(j);
        if (text == null || !text.getName().equals("text")) {
            continue;
        }
        parseTextHandle(source, text);
        // Clayton Sims - Jun 17, 2009 - This code is used when the stinginess flag
        // is set for the build. It dynamically wipes out old model nodes once they're
        // used. This is sketchy if anything else plans on touching the nodes.
        // This code can be removed once we're pull-parsing
        // #if org.javarosa.xform.stingy
        removeIndexes.add(j);
    }
    ElementChildDeleter.delete(trans, removeIndexes);
    // print unused attribute warning message for parent element
    if (XFormUtils.showUnusedAttributeWarning(trans, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(trans, usedAtts), getVagueLocation(trans));
    }
    l.registerLocaleResource(lang, source);
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) ArrayList(java.util.ArrayList) TableLocaleSource(org.javarosa.core.services.locale.TableLocaleSource) HashSet(java.util.HashSet)

Example 3 with TableLocaleSource

use of org.javarosa.core.services.locale.TableLocaleSource in project javarosa by opendatakit.

the class LocalizerTest method testTextMapping.

public void testTextMapping() {
    Localizer l = new Localizer();
    final String TEST_LOCALE = "test";
    l.addAvailableLocale(TEST_LOCALE);
    if (l.hasMapping(TEST_LOCALE, "textID")) {
        fail("Localizer contains text mapping that was not defined");
    }
    TableLocaleSource table = new TableLocaleSource();
    table.setLocaleMapping("textID", "text");
    l.registerLocaleResource(TEST_LOCALE, table);
    if (!l.hasMapping(TEST_LOCALE, "textID")) {
        fail("Localizer does not contain newly added text mapping");
    }
    if (!"text".equals(l.getLocaleData(TEST_LOCALE).get("textID"))) {
        fail("Newly added text mapping does not match source");
    }
}
Also used : TableLocaleSource(org.javarosa.core.services.locale.TableLocaleSource) Localizer(org.javarosa.core.services.locale.Localizer)

Example 4 with TableLocaleSource

use of org.javarosa.core.services.locale.TableLocaleSource in project javarosa by opendatakit.

the class LocalizerTest method buildLocalizer.

private Localizer buildLocalizer(int i, int j, int k, String ourLocale, String otherLocale) {
    Localizer l = new Localizer(i / 2 == 0, i % 2 == 0);
    TableLocaleSource firstLocale = new TableLocaleSource();
    TableLocaleSource secondLocale = new TableLocaleSource();
    if (j / 2 == 0 || "default".equals(ourLocale))
        firstLocale.setLocaleMapping("textID", "text:" + ourLocale + ":base");
    if (j % 2 == 0 || "default".equals(ourLocale))
        firstLocale.setLocaleMapping("textID;form", "text:" + ourLocale + ":form");
    if (otherLocale != null) {
        if (k / 2 == 0 || "default".equals(otherLocale))
            secondLocale.setLocaleMapping("textID", "text:" + otherLocale + ":base");
        if (k % 2 == 0 || "default".equals(otherLocale))
            secondLocale.setLocaleMapping("textID;form", "text:" + otherLocale + ":form");
    }
    l.addAvailableLocale(ourLocale);
    l.registerLocaleResource(ourLocale, firstLocale);
    if (otherLocale != null) {
        l.addAvailableLocale(otherLocale);
        l.registerLocaleResource(otherLocale, secondLocale);
    }
    if (l.hasLocale("default")) {
        l.setDefaultLocale("default");
    }
    l.setLocale(ourLocale);
    return l;
}
Also used : TableLocaleSource(org.javarosa.core.services.locale.TableLocaleSource) Localizer(org.javarosa.core.services.locale.Localizer)

Example 5 with TableLocaleSource

use of org.javarosa.core.services.locale.TableLocaleSource in project javarosa by opendatakit.

the class LocalizerTest method testSerialization.

public void testSerialization() {
    Localizer l = new Localizer(true, true);
    TableLocaleSource firstLocale = new TableLocaleSource();
    TableLocaleSource secondLocale = new TableLocaleSource();
    TableLocaleSource finalLocale = new TableLocaleSource();
    testSerialize(l, "empty 1");
    testSerialize(new Localizer(false, false), "empty 2");
    testSerialize(new Localizer(true, false), "empty 3");
    testSerialize(new Localizer(false, true), "empty 4");
    l.addAvailableLocale("locale1");
    testSerialize(l, "one empty locale");
    l.addAvailableLocale("locale2");
    testSerialize(l, "two empty locales");
    l.setDefaultLocale("locale2");
    testSerialize(l, "two empty locales + default");
    l.setToDefault();
    testSerialize(l, "two empty locales + default/current");
    l.setLocale("locale1");
    testSerialize(l, "two empty locales + default/current 2");
    l.setDefaultLocale(null);
    testSerialize(l, "two empty locales + current");
    l.registerLocaleResource("locale1", firstLocale);
    l.registerLocaleResource("locale2", secondLocale);
    firstLocale.setLocaleMapping("id1", "text1");
    testSerialize(l, "locales with data 1");
    firstLocale.setLocaleMapping("id2", "text2");
    testSerialize(l, "locales with data 2");
    secondLocale.setLocaleMapping("id1", "text1");
    secondLocale.setLocaleMapping("id2", "text2");
    secondLocale.setLocaleMapping("id3", "text3");
    testSerialize(l, "locales with data 3");
    secondLocale.setLocaleMapping("id2", null);
    testSerialize(l, "locales with data 4");
    finalLocale.setLocaleMapping("id1", "text1");
    finalLocale.setLocaleMapping("id4", "text4");
    l.registerLocaleResource("locale3", finalLocale);
    testSerialize(l, "locales with data 5");
    l.destroyLocale("locale2");
    testSerialize(l, "locales with data 6");
}
Also used : TableLocaleSource(org.javarosa.core.services.locale.TableLocaleSource) Localizer(org.javarosa.core.services.locale.Localizer)

Aggregations

TableLocaleSource (org.javarosa.core.services.locale.TableLocaleSource)11 Localizer (org.javarosa.core.services.locale.Localizer)10 UnregisteredLocaleException (org.javarosa.core.util.UnregisteredLocaleException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IFormElement (org.javarosa.core.model.IFormElement)1 QuestionDef (org.javarosa.core.model.QuestionDef)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 DummyFormEntryPrompt (org.javarosa.core.model.test.DummyFormEntryPrompt)1 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)1 Element (org.kxml2.kdom.Element)1