Search in sources :

Example 6 with Translation

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;
}
Also used : Locale(java.util.Locale) Translation(org.xwiki.localization.Translation) GroupBlock(org.xwiki.rendering.block.GroupBlock) StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) Block(org.xwiki.rendering.block.Block) GroupBlock(org.xwiki.rendering.block.GroupBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) ParseException(org.xwiki.rendering.parser.ParseException)

Example 7 with Translation

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);
}
Also used : Translation(org.xwiki.localization.Translation) LocalizationContext(org.xwiki.localization.LocalizationContext) WordBlock(org.xwiki.rendering.block.WordBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Provider(javax.inject.Provider) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) LocalizationManager(org.xwiki.localization.LocalizationManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ComponentManager(org.xwiki.component.manager.ComponentManager) Environment(org.xwiki.environment.Environment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Before(org.junit.Before)

Example 8 with Translation

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;
}
Also used : Locale(java.util.Locale) Translation(org.xwiki.localization.Translation)

Example 9 with 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();
}
Also used : Translation(org.xwiki.localization.Translation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Block(org.xwiki.rendering.block.Block)

Aggregations

Translation (org.xwiki.localization.Translation)9 Block (org.xwiki.rendering.block.Block)4 Locale (java.util.Locale)3 LocalizationContext (org.xwiki.localization.LocalizationContext)2 LocalizationManager (org.xwiki.localization.LocalizationManager)2 WordBlock (org.xwiki.rendering.block.WordBlock)2 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)2 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)2 StringReader (java.io.StringReader)1 Provider (javax.inject.Provider)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 Environment (org.xwiki.environment.Environment)1 TranslationBundle (org.xwiki.localization.TranslationBundle)1