use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class DocumentTranslationBundleFactoryTest method assertTranslation.
private void assertTranslation(String key, String message, Locale locale) {
Translation translation = this.localization.getTranslation(key, locale);
if (message != null) {
Assert.assertNotNull("No translation could be found for key [" + key + "]", translation);
Assert.assertEquals(message, translation.getRawSource());
} else {
Assert.assertNull(translation);
}
}
use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class IntegrationTests method initialize.
@RenderingTestSuite.Initialized
public void initialize(MockingComponentManager componentManager) throws Exception {
Mockery mockery = new JUnit4Mockery();
final LocalizationManager localizationManager = componentManager.registerMockComponent(mockery, LocalizationManager.class);
final LocalizationContext localizationContext = componentManager.registerMockComponent(mockery, LocalizationContext.class);
mockery.checking(new Expectations() {
{
allowing(localizationManager).getTranslation("some.translation", Locale.ENGLISH);
will(returnValue(new Translation() {
@Override
public Block render(Locale locale, Object... parameters) {
return parameters.length > 0 ? new WordBlock("entranslationmessage" + Arrays.toString(parameters)) : new WordBlock("entranslationmessage");
}
@Override
public Block render(Object... parameters) {
return render(null, parameters);
}
@Override
public String getRawSource() {
return "entranslationmessagesource";
}
@Override
public Locale getLocale() {
return Locale.ENGLISH;
}
@Override
public String getKey() {
return "some.translation";
}
@Override
public TranslationBundle getBundle() {
return null;
}
}));
allowing(localizationManager).getTranslation("some.translation", Locale.FRENCH);
will(returnValue(new Translation() {
@Override
public Block render(Locale locale, Object... parameters) {
return parameters.length > 0 ? new WordBlock("frtranslationmessage" + Arrays.toString(parameters)) : new WordBlock("frtranslationmessage");
}
@Override
public Block render(Object... parameters) {
return render(null, parameters);
}
@Override
public String getRawSource() {
return "frtranslationmessagesource";
}
@Override
public Locale getLocale() {
return Locale.FRENCH;
}
@Override
public String getKey() {
return "some.translation";
}
@Override
public TranslationBundle getBundle() {
return null;
}
}));
allowing(localizationManager).getTranslation("unexisting.translation", Locale.ENGLISH);
will(returnValue(null));
allowing(localizationContext).getCurrentLocale();
will(returnValue(Locale.ENGLISH));
}
});
}
use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class LocalizationScriptService method render.
/**
* @param keys the translations keys to try one by one
* @param syntax the syntax in which to render the translation message
* @param parameters the translation parameters
* @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
* different {@link Locale} (for example getting the {@code fr} translation when asking for the
* {@code fr_FR} one).
* @return the rendered translation message, the key if no translation can be found and null if the rendering failed
* @since 10.2
*/
public String render(Collection<String> keys, Syntax syntax, Collection<?> parameters, Locale locale) {
if (CollectionUtils.isEmpty(keys)) {
return null;
}
Translation translation = null;
for (String key : keys) {
if (key != null) {
translation = this.localization.getTranslation(key, locale);
if (translation != null) {
break;
}
}
}
String result;
if (translation != null) {
Block block = parameters != null ? translation.render(locale, parameters.toArray()) : translation.render(locale);
try {
BlockRenderer renderer = this.componentManager.get().getInstance(BlockRenderer.class, syntax.toIdString());
DefaultWikiPrinter wikiPrinter = new DefaultWikiPrinter();
renderer.render(block, wikiPrinter);
result = wikiPrinter.toString();
} catch (ComponentLookupException e) {
// TODO set current error
result = null;
}
} else {
result = null;
for (String key : keys) {
if (key != null) {
result = key;
}
}
}
return result;
}
use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class JARTranslationBundleFactoryTest method assertTranslation.
private void assertTranslation(String key, String message, Locale locale) {
Translation translation = this.localizationManager.getTranslation(key, locale);
if (message != null) {
Assert.assertNotNull("Could not find translation for key [" + key + "] and locale [" + locale + "]", translation);
Assert.assertEquals(message, translation.getRawSource());
} else {
Assert.assertNull("Found translation for key [" + key + "] and locale [" + locale + "]", translation);
}
}
use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class RootClassLoaderTranslationBundleTest method assertTranslation.
private void assertTranslation(String key, String message, Locale locale) {
Translation translation = this.localizationManager.getTranslation(key, locale);
if (message != null) {
Assert.assertNotNull("Could not find translation for key [" + key + "] and locale [" + locale + "]", translation);
Assert.assertEquals(message, translation.getRawSource());
} else {
Assert.assertNull("Found translation for key [" + key + "] and locale [" + locale + "]", translation);
}
}
Aggregations