Search in sources :

Example 1 with TranslationMessage

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

the class RootClassLoaderTranslationBundle method createBundle.

@Override
protected LocalizedTranslationBundle createBundle(Locale locale) {
    Properties properties = getResourceProperties(locale);
    if (properties == null) {
        return LocalizedTranslationBundle.EMPTY;
    }
    // Convert to LocalBundle
    DefaultLocalizedTranslationBundle localeBundle = new DefaultLocalizedTranslationBundle(this, locale);
    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 = this.parser.parse(message);
            localeBundle.addTranslation(new DefaultTranslation(this.bundleContext, localeBundle, key, translationMessage));
        }
    }
    return localeBundle;
}
Also used : DefaultLocalizedTranslationBundle(org.xwiki.localization.internal.DefaultLocalizedTranslationBundle) Properties(java.util.Properties) DefaultTranslation(org.xwiki.localization.internal.DefaultTranslation) Map(java.util.Map) TranslationMessage(org.xwiki.localization.message.TranslationMessage)

Example 2 with TranslationMessage

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

the class AbstractDocumentTranslationBundle method loadDocumentLocaleBundle.

protected LocalizedTranslationBundle loadDocumentLocaleBundle(Locale locale) throws Exception {
    XWikiContext context = this.contextProvider.get();
    if (context == null) {
        // No context for some reason, lets try later
        return null;
    }
    XWiki xwiki = context.getWiki();
    if (xwiki == null) {
        // No XWiki instance ready, lets try later
        return null;
    }
    XWikiDocument document = xwiki.getDocument(this.documentReference, context);
    if (locale != null && !locale.equals(Locale.ROOT) && !locale.equals(document.getDefaultLocale())) {
        document = xwiki.getDocument(new DocumentReference(document.getDocumentReference(), locale), context);
        if (document.isNew()) {
            // No document found for this locale
            return LocalizedTranslationBundle.EMPTY;
        }
    }
    String content = document.getContent();
    Properties properties = new Properties();
    properties.load(new StringReader(content));
    // 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 : XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Properties(java.util.Properties) DefaultTranslation(org.xwiki.localization.internal.DefaultTranslation) TranslationMessageParser(org.xwiki.localization.message.TranslationMessageParser) TranslationMessage(org.xwiki.localization.message.TranslationMessage) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultLocalizedTranslationBundle(org.xwiki.localization.internal.DefaultLocalizedTranslationBundle) StringReader(java.io.StringReader) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with TranslationMessage

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

the class MessageToolTranslationMessageParserTest method messageWithChoiceSyntax.

@Test
public void messageWithChoiceSyntax() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("{0,choice,0#choice1|0<choice2}");
    Assert.assertEquals("{0,choice,0#choice1|0<choice2}", translationMessage.getRawSource());
    Assert.assertEquals(new WordBlock("choice1"), translationMessage.render(null, null, 0));
    Assert.assertEquals(new WordBlock("choice2"), translationMessage.render(null, null, 42));
}
Also used : WordBlock(org.xwiki.rendering.block.WordBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 4 with TranslationMessage

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

the class MessageToolTranslationMessageParserTest method messageWithOneParameter.

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

Example 5 with TranslationMessage

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

the class MessageToolTranslationMessageParserTest method messageWithApostrophe.

@Test
public void messageWithApostrophe() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("'");
    Assert.assertEquals("'", translationMessage.getRawSource());
    Assert.assertEquals(new SpecialSymbolBlock('\''), translationMessage.render(null, null));
}
Also used : 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