Search in sources :

Example 6 with TranslationMessage

use of org.xwiki.localization.message.TranslationMessage in project xwiki-platform by xwiki.

the class MessageToolTranslationMessageParserTest method messageWithEscapedParameter.

@Test
public void messageWithEscapedParameter() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("'{0}");
    Assert.assertEquals("'{0}", translationMessage.getRawSource());
    Assert.assertEquals(new CompositeBlock(Arrays.<Block>asList(new SpecialSymbolBlock('{'), new WordBlock("0"), new SpecialSymbolBlock('}'))), translationMessage.render(null, null, "word"));
}
Also used : SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 7 with TranslationMessage

use of org.xwiki.localization.message.TranslationMessage in project xwiki-platform by xwiki.

the class MessageToolTranslationMessageParserTest method messageEmpty.

@Test
public void messageEmpty() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("");
    Assert.assertEquals("", translationMessage.getRawSource());
    Assert.assertEquals(new CompositeBlock(), translationMessage.render(null, null));
}
Also used : CompositeBlock(org.xwiki.rendering.block.CompositeBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 8 with TranslationMessage

use of org.xwiki.localization.message.TranslationMessage in project xwiki-platform by xwiki.

the class AbstractURLResourceTranslationBundle method loadResourceLocaleBundle.

/**
 * @param locale the locale
 * @return the {@link LocalizedTranslationBundle} corresponding to the passed {@link Locale}, null if none could be
 *         found
 */
protected LocalizedTranslationBundle loadResourceLocaleBundle(Locale locale) {
    // Find resource
    URL localeURL = getLocaleURL(locale);
    if (localeURL == null) {
        return LocalizedTranslationBundle.EMPTY;
    }
    // Parse resource
    Properties properties = new Properties();
    try (InputStream componentListStream = localeURL.openStream()) {
        properties.load(componentListStream);
    } catch (FileNotFoundException e) {
        // No translation files for the passed locale
        return LocalizedTranslationBundle.EMPTY;
    } catch (IOException e) {
        this.logger.error("Failed to parse resource [{}] as translation bundle", localeURL, e);
    }
    // Convert to LocalBundle
    DefaultLocalizedTranslationBundle localeBundle = new DefaultLocalizedTranslationBundle(this, locale);
    TranslationMessageParser parser = getTranslationMessageParser();
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
            String key = (String) entry.getKey();
            String message = (String) entry.getValue();
            TranslationMessage translationMessage = parser.parse(message);
            localeBundle.addTranslation(new DefaultTranslation(this.bundleContext, localeBundle, key, translationMessage));
        }
    }
    return localeBundle;
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL) TranslationMessageParser(org.xwiki.localization.message.TranslationMessageParser) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Map(java.util.Map)

Example 9 with TranslationMessage

use of org.xwiki.localization.message.TranslationMessage in project xwiki-platform by xwiki.

the class MessageToolTranslationMessageParserTest method messageSimple.

@Test
public void messageSimple() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("word");
    Assert.assertEquals("word", translationMessage.getRawSource());
    Assert.assertEquals(new WordBlock("word"), translationMessage.render(null, null));
}
Also used : WordBlock(org.xwiki.rendering.block.WordBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 10 with TranslationMessage

use of org.xwiki.localization.message.TranslationMessage in project xwiki-platform by xwiki.

the class MessageToolTranslationMessageParserTest method messageWithExpectedParameter.

@Test
public void messageWithExpectedParameter() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("{0}");
    Assert.assertEquals("{0}", translationMessage.getRawSource());
    Assert.assertEquals(new CompositeBlock(Arrays.<Block>asList(new SpecialSymbolBlock('{'), new WordBlock("0"), new SpecialSymbolBlock('}'))), translationMessage.render(null, null));
}
Also used : SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Aggregations

TranslationMessage (org.xwiki.localization.message.TranslationMessage)10 Test (org.junit.Test)7 WordBlock (org.xwiki.rendering.block.WordBlock)5 Map (java.util.Map)3 Properties (java.util.Properties)3 CompositeBlock (org.xwiki.rendering.block.CompositeBlock)3 SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)3 DefaultLocalizedTranslationBundle (org.xwiki.localization.internal.DefaultLocalizedTranslationBundle)2 DefaultTranslation (org.xwiki.localization.internal.DefaultTranslation)2 TranslationMessageParser (org.xwiki.localization.message.TranslationMessageParser)2 Block (org.xwiki.rendering.block.Block)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1