Search in sources :

Example 1 with DefaultTranslation

use of org.xwiki.localization.internal.DefaultTranslation 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 DefaultTranslation

use of org.xwiki.localization.internal.DefaultTranslation 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)

Aggregations

Map (java.util.Map)2 Properties (java.util.Properties)2 DefaultLocalizedTranslationBundle (org.xwiki.localization.internal.DefaultLocalizedTranslationBundle)2 DefaultTranslation (org.xwiki.localization.internal.DefaultTranslation)2 TranslationMessage (org.xwiki.localization.message.TranslationMessage)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 StringReader (java.io.StringReader)1 TranslationMessageParser (org.xwiki.localization.message.TranslationMessageParser)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1