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);
}
}
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;
}
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);
}
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);
}
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());
}
Aggregations