Skip to content

Commit

Permalink
Merge branch 'tmp/fix-6881'
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Mar 8, 2021
2 parents f3abaea + 99df3ba commit 0b77155
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 15 deletions.
@@ -1,9 +1,10 @@
#
# Copyright (c) 2010-2017 Evolveum and contributors
#
# This work is dual-licensed under the Apache License 2.0
# This work is dual-licensed under the Apache License 2.0
# and European Union Public License. See LICENSE file for details.
#

customMidpointKey=customMidpointValue
standardKey=standardKeyCustomValue
AssignmentFailed=Assignment of {0} to {1} failed.
@@ -1,9 +1,10 @@
#
# Copyright (c) 2010-2017 Evolveum and contributors
#
# This work is dual-licensed under the Apache License 2.0
# This work is dual-licensed under the Apache License 2.0
# and European Union Public License. See LICENSE file for details.
#

customSchemaKey=customSchemaValue
ObjectType.name=Nameee
ObjectType.description=Popis
Expand Up @@ -90,10 +90,7 @@ public String translate(String key, Object[] params, Locale locale, String defau
try {
String value = source.getMessage(key, translated, locale);
if (StringUtils.isNotEmpty(value)) {
if (LOG.isTraceEnabled()) {
LOG.trace("Resolved key {} to value {} using message source {}", key, value, source);
}

LOG.trace("Resolved key {} to value {} using message source {}", key, value, source);
return value;
}
} catch (NoSuchMessageException ex) {
Expand Down Expand Up @@ -246,16 +243,34 @@ public String translate(PolyString polyString, Locale locale, boolean allowOrig)
}
}

private String translate(PolyStringTranslationType polyStringTranslation, Locale locale) {
String key = polyStringTranslation.getKey();
private String translate(PolyStringTranslationType translation, Locale locale) {
String key = translation.getKey();

if (StringUtils.isEmpty(key)) {
return key;
return translateFromFallback(translation, locale, key);
}
List<PolyStringTranslationArgumentType> arguments = polyStringTranslation.getArgument();

String result;
List<PolyStringTranslationArgumentType> arguments = translation.getArgument();
if (arguments == null) {
return translate(key, null, locale, polyStringTranslation.getFallback());
result = translate(key, null, locale, null);
} else {
result = translate(key, arguments.toArray(), locale, null);
}
if (result != null) {
return result;
} else {
return translateFromFallback(translation, locale, null);
}
}

private String translateFromFallback(PolyStringTranslationType translation, Locale locale, String defaultValue) {
if (translation.getFallbackTranslation() != null) {
return translate(translation.getFallbackTranslation(), locale);
} else if (translation.getFallback() != null) {
return translation.getFallback();
} else {
return translate(key, arguments.toArray(), locale, polyStringTranslation.getFallback());
return defaultValue;
}
}

Expand Down
Expand Up @@ -8,28 +8,52 @@
package com.evolveum.midpoint.common;

import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.tools.testng.AbstractUnitTest;
import com.evolveum.midpoint.tools.testng.UnusedTestElement;
import com.evolveum.midpoint.util.LocalizableMessage;
import com.evolveum.midpoint.util.LocalizableMessageBuilder;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;

import com.evolveum.midpoint.xml.ns._public.common.common_3.CaseType;

import org.testng.AssertJUnit;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import static com.evolveum.midpoint.prism.util.PrismTestUtil.getPrismContext;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Created by Viliam Repan (lazyman).
*/
@UnusedTestElement("1 test failing, not in suite")
public class LocalizationTest extends AbstractUnitTest {

private static String midpointHome;

private static LocalizationServiceImpl service;

private static final File TEST_DIR = new File("src/test/resources/localization");
private static final File CASE_WITH_LOCALIZED_NAME_FILE = new File(TEST_DIR, "case-with-localized-name.xml");

@BeforeSuite
public void setup() throws SchemaException, SAXException, IOException {
PrettyPrinter.setDefaultNamespacePrefix(MidPointConstants.NS_MIDPOINT_PUBLIC_PREFIX);
PrismTestUtil.resetPrismContext(MidPointPrismContextFactory.FACTORY);
}

@BeforeClass
public static void beforeClass() {
midpointHome = System.getProperty(MidpointConfiguration.MIDPOINT_HOME_PROPERTY);
Expand Down Expand Up @@ -88,6 +112,15 @@ public void localizationParams() {
AssertJUnit.assertEquals(expected, real);
}

@Test
public void translationWithArguments() throws SchemaException, IOException {
PrismObject<CaseType> object = getPrismContext().parserFor(CASE_WITH_LOCALIZED_NAME_FILE).parse();
PolyString name = object.getName();

String translated = service.translate(name, new Locale("sk"), false);
assertThat(translated).as("translated value").isEqualTo("Assignment of Role to User failed.");
}

private void assertTranslation(LocalizationService service, String key, String expectedValue) {
assertTranslation(service, key, null, expectedValue);
}
Expand Down
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2010-2021 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->

<!-- MID-6881 -->
<case xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
oid="f7c00237-262e-49cd-a1e4-3dd79c6ea46d">
<name>
<t:translation>
<t:key>AssignmentFailed</t:key>
<t:argument>
<t:translation>
<t:fallback>Role</t:fallback>
</t:translation>
</t:argument>
<t:argument>
<t:translation>
<t:fallback>User</t:fallback>
</t:translation>
</t:argument>
</t:translation>
</name>
</case>
5 changes: 5 additions & 0 deletions infra/common/testng-unit.xml
Expand Up @@ -26,4 +26,9 @@
<class name="com.evolveum.midpoint.common.refinery.TestRefinedSchema" />
</classes>
</test>
<test name="Localization" preserve-order="false">
<classes>
<class name="com.evolveum.midpoint.common.LocalizationTest" />
</classes>
</test>
</suite>
Expand Up @@ -123,7 +123,7 @@
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="key" type="xsd:string">
<xsd:element name="key" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Key used to look up the localized message format in the localization catalogs.
Expand Down

0 comments on commit 0b77155

Please sign in to comment.