use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class TranslationMacro method execute.
@Override
public List<Block> execute(TranslationMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
Locale locale = parameters.getLocale() != null ? parameters.getLocale() : this.localizationContext.getCurrentLocale();
Translation translation = this.localization.getTranslation(parameters.getKey(), locale);
List<Block> blocks;
if (translation != null) {
Block block = parameters.getParameters() != null ? translation.render(locale, (Object[]) parameters.getParameters()) : translation.render(locale);
if (block instanceof CompositeBlock) {
blocks = block.getChildren();
} else {
blocks = Arrays.asList(block);
}
if (!context.getCurrentMacroBlock().isInline()) {
// Make the content standalone
blocks = Arrays.<Block>asList(new GroupBlock(blocks));
}
} else if (content != null) {
blocks = this.macroContentParser.parse(content, context, false, context.getCurrentMacroBlock().isInline()).getChildren();
} else {
try {
blocks = this.plainParser.parse(new StringReader(parameters.getKey())).getChildren();
if (context.getCurrentMacroBlock().isInline()) {
PARSERUTILS.removeTopLevelParagraph(blocks);
}
} catch (ParseException e) {
throw new MacroExecutionException("Failed to parse key [" + parameters.getKey() + "]", e);
}
}
return blocks;
}
use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class LocalizationScriptServiceTest method setUp.
@Before
public void setUp() throws Exception {
componentManager = mock(ComponentManager.class);
Provider<ComponentManager> componentManagerProvider = mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
when(componentManagerProvider.get()).thenReturn(componentManager);
renderer = mock(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
when(componentManager.getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString())).thenReturn(renderer);
localizationContext = mocker.getInstance(LocalizationContext.class);
localizationManager = mocker.getInstance(LocalizationManager.class);
localizationScriptService = (LocalizationScriptService) mocker.getComponentUnderTest();
translation = mock(Translation.class);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
WikiPrinter printer = (WikiPrinter) invocation.getArguments()[1];
printer.print("print result");
return null;
}
}).when(renderer).render(eq(new WordBlock("message")), any(WikiPrinter.class));
when(translation.render(Locale.ROOT, ArrayUtils.EMPTY_OBJECT_ARRAY)).thenReturn(new WordBlock("message"));
when(localizationManager.getTranslation("key", Locale.ROOT)).thenReturn(translation);
when(localizationContext.getCurrentLocale()).thenReturn(Locale.ROOT);
environment = mocker.getInstance(Environment.class);
}
use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class AbstractCachedTranslationBundle method getTranslation.
@Override
public Translation getTranslation(String key, Locale locale) {
Translation translation;
LocalizedTranslationBundle bundle = getLocalizedBundle(locale);
if (bundle != null) {
translation = bundle.getTranslation(key);
if (translation == null) {
Locale parentLocale = LocaleUtils.getParentLocale(locale);
if (parentLocale != null) {
translation = getTranslation(key, parentLocale);
}
}
} else {
translation = null;
}
return translation;
}
use of org.xwiki.localization.Translation in project xwiki-platform by xwiki.
the class DefaultContextualLocalizationManager method getTranslationPlain.
@Override
public String getTranslationPlain(String key, Object... parameters) {
Translation translation = getTranslation(key);
if (translation == null) {
return null;
}
Block block = translation.render(parameters);
DefaultWikiPrinter wikiPrinter = new DefaultWikiPrinter();
this.plainRenderer.render(block, wikiPrinter);
return wikiPrinter.toString();
}
Aggregations