Search in sources :

Example 1 with Translation

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

Example 2 with 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));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Locale(java.util.Locale) Translation(org.xwiki.localization.Translation) LocalizationContext(org.xwiki.localization.LocalizationContext) WordBlock(org.xwiki.rendering.block.WordBlock) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) LocalizationManager(org.xwiki.localization.LocalizationManager) TranslationBundle(org.xwiki.localization.TranslationBundle) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block)

Example 3 with Translation

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;
}
Also used : Translation(org.xwiki.localization.Translation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Block(org.xwiki.rendering.block.Block) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 4 with Translation

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

Example 5 with 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);
    }
}
Also used : Translation(org.xwiki.localization.Translation)

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