Search in sources :

Example 6 with Translation

use of org.hisp.dhis.translation.Translation in project dhis2-core by dhis2.

the class TranslationsCheck method run.

private <T extends IdentifiableObject> void run(IdentifiableObject object, Class<T> klass, Consumer<ObjectReport> addReports, Schema schema, int index) {
    Set<Translation> translations = object.getTranslations();
    if (CollectionUtils.isEmpty(translations)) {
        return;
    }
    ObjectReport objectReport = new ObjectReport(klass, index);
    if (!schema.isTranslatable()) {
        objectReport.addErrorReport(new ErrorReport(Translation.class, ErrorCode.E1107, klass.getSimpleName()).setErrorKlass(klass));
        addReports.accept(objectReport);
        return;
    }
    Set<String> setPropertyLocales = new HashSet<>();
    for (Translation translation : translations) {
        String key = String.join("_", translation.getProperty(), translation.getLocale());
        if (setPropertyLocales.contains(key)) {
            objectReport.addErrorReport(new ErrorReport(Translation.class, ErrorCode.E1106, translation.getProperty(), translation.getLocale()).setErrorKlass(klass));
        } else {
            setPropertyLocales.add(key);
        }
        if (translation.getLocale() == null) {
            objectReport.addErrorReport(new ErrorReport(Translation.class, ErrorCode.E4000, "locale").setErrorKlass(klass));
        }
        if (translation.getProperty() == null) {
            objectReport.addErrorReport(new ErrorReport(Translation.class, ErrorCode.E4000, "property").setErrorKlass(klass));
        }
        if (translation.getValue() == null) {
            objectReport.addErrorReport(new ErrorReport(Translation.class, ErrorCode.E4000, "value").setErrorKlass(klass));
        }
    }
    if (objectReport.hasErrorReports()) {
        addReports.accept(objectReport);
    }
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) Translation(org.hisp.dhis.translation.Translation) ObjectReport(org.hisp.dhis.feedback.ObjectReport) HashSet(java.util.HashSet)

Example 7 with Translation

use of org.hisp.dhis.translation.Translation in project dhis2-core by dhis2.

the class GistBuilder method translate.

private Object translate(Object value, String property, Object translations) {
    @SuppressWarnings("unchecked") Set<Translation> list = (Set<Translation>) translations;
    if (list == null || list.isEmpty()) {
        return value;
    }
    String locale = query.getTranslationLocale().toString();
    for (Translation t : list) {
        if (t.getLocale().equalsIgnoreCase(locale) && t.getProperty().equalsIgnoreCase(property) && !t.getValue().isEmpty())
            return t.getValue();
    }
    String lang = query.getTranslationLocale().getLanguage();
    for (Translation t : list) {
        if (t.getLocale().startsWith(lang) && t.getProperty().equalsIgnoreCase(property) && !t.getValue().isEmpty())
            return t.getValue();
    }
    return value;
}
Also used : Translation(org.hisp.dhis.translation.Translation) Set(java.util.Set)

Example 8 with Translation

use of org.hisp.dhis.translation.Translation in project dhis2-core by dhis2.

the class JsonListBinaryTypeTest method setUp.

@BeforeEach
void setUp() {
    translation1 = new Translation();
    translation1.setLocale("en");
    translation1.setValue("English Test 1");
    translation2 = new Translation();
    translation2.setLocale("no");
    translation2.setValue("Norwegian Test 1");
    translations = new ArrayList<>();
    translations.add(translation1);
    translations.add(translation2);
    jsonBinaryType = new JsonListBinaryType();
    jsonBinaryType.init(Translation.class);
}
Also used : Translation(org.hisp.dhis.translation.Translation) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with Translation

use of org.hisp.dhis.translation.Translation in project dhis2-core by dhis2.

the class JsonBinaryTypeTest method deepCopy.

@Test
void deepCopy() {
    final Translation result = (Translation) jsonBinaryType.deepCopy(translation1);
    Assertions.assertNotSame(translation1, result);
    Assertions.assertEquals(translation1, result);
}
Also used : Translation(org.hisp.dhis.translation.Translation) Test(org.junit.jupiter.api.Test)

Example 10 with Translation

use of org.hisp.dhis.translation.Translation in project dhis2-core by dhis2.

the class TranslationWebApiTest method testOK.

@Test
@Disabled
void testOK() throws Exception {
    final Locale locale = Locale.FRENCH;
    final CategoryCombo categoryCombo = createCategoryCombo('C');
    final DataElement dataElementA = createDataElement('A', categoryCombo);
    final String valueToCheck = "frenchTranslated";
    final MockHttpSession session = getSession("ALL");
    transactionTemplate.execute(status -> {
        identifiableObjectManager.save(categoryCombo);
        identifiableObjectManager.save(dataElementA);
        dataElementA.getTranslations().add(new Translation(locale.getLanguage(), "NAME", valueToCheck));
        try {
            mvc.perform(put("/dataElements/" + dataElementA.getUid() + "/translations").session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(TestUtils.convertObjectToJsonBytes(dataElementA))).andExpect(status().is(HttpStatus.SC_NO_CONTENT));
        } catch (Exception e) {
            log.error("Failed:" + e.getMessage(), e);
        }
        dbmsManager.clearSession();
        return null;
    });
    MvcResult result = mvc.perform(get("/dataElements/" + dataElementA.getUid() + "?locale=" + locale.getLanguage()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andReturn();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(result.getResponse().getContentAsString());
    assertEquals(valueToCheck, node.get("displayName").asText());
}
Also used : Locale(java.util.Locale) DataElement(org.hisp.dhis.dataelement.DataElement) Translation(org.hisp.dhis.translation.Translation) CategoryCombo(org.hisp.dhis.category.CategoryCombo) MockHttpSession(org.springframework.mock.web.MockHttpSession) JsonNode(com.fasterxml.jackson.databind.JsonNode) MvcResult(org.springframework.test.web.servlet.MvcResult) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

Translation (org.hisp.dhis.translation.Translation)11 HashSet (java.util.HashSet)5 Test (org.junit.jupiter.api.Test)4 Locale (java.util.Locale)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Set (java.util.Set)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DhisSpringTest (org.hisp.dhis.DhisSpringTest)1 DhisTest (org.hisp.dhis.DhisTest)1 CategoryCombo (org.hisp.dhis.category.CategoryCombo)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)1 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)1 Expression (org.hisp.dhis.expression.Expression)1 ErrorReport (org.hisp.dhis.feedback.ErrorReport)1 ObjectReport (org.hisp.dhis.feedback.ObjectReport)1