Search in sources :

Example 1 with TranslationMessageParser

use of org.xwiki.localization.message.TranslationMessageParser 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 2 with TranslationMessageParser

use of org.xwiki.localization.message.TranslationMessageParser 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)

Aggregations

Map (java.util.Map)2 Properties (java.util.Properties)2 TranslationMessage (org.xwiki.localization.message.TranslationMessage)2 TranslationMessageParser (org.xwiki.localization.message.TranslationMessageParser)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 DefaultLocalizedTranslationBundle (org.xwiki.localization.internal.DefaultLocalizedTranslationBundle)1 DefaultTranslation (org.xwiki.localization.internal.DefaultTranslation)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1