Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package pl.matisoft.soy.bundle;
  
 
 import java.net.URL;
 import java.util.*;
Created with IntelliJ IDEA. User: mati Date: 20/06/2013 Time: 00:01 An implementation of SoyMsgBundleResolver that returns SoyMsgBundle based on a configurable url, which can be retrieved from classpath using Thread.getContextClassLoader(). The implementation will fallback to English translation if a language specific translation cannot be found only if fallbackToEnglish configuration option is set. Assuming defaults and locale set to pl_PL, an implementation will look for a following file in a classpath: messages_pl_PL.xlf
 
 public class DefaultSoyMsgBundleResolver implements SoyMsgBundleResolver {
 
     private static final Logger logger = LoggerFactory.getLogger(DefaultSoyMsgBundleResolver.class);

    
a cache of soy msg bundles
 
    
friendly
a path to a bundle
 
will cache msgBundles if a hotReloadMode is off, if debug is on, will compile msg bundles each time it is invoked
 
     private boolean hotReloadMode = .;

    
in case translation is missing for a passed in locale, whether the implementation should fallback to English returning an english translation if available
 
     private boolean fallbackToEnglish = true;

    
Based on a provided locale return a SoyMsgBundle file. If a passed in locale object is "Optional.absent()", the implementation will return Optional.absent() as well

Parameters:
locale - maybe locale
Returns:
maybe soy msg bundle
 
     public Optional<SoyMsgBundleresolve(final Optional<Localelocalethrows IOException {
         if (!locale.isPresent()) {
             return Optional.absent();
         }
 
         synchronized () {
             SoyMsgBundle soyMsgBundle = null;
             if (isHotReloadModeOff()) {
                 soyMsgBundle = .get(locale.get());
             }
             if (soyMsgBundle == null) {
                 soyMsgBundle = createSoyMsgBundle(locale.get());
                 if (soyMsgBundle == null) {
                     soyMsgBundle = createSoyMsgBundle(new Locale(locale.get().getLanguage()));
                 }
 
                 if (soyMsgBundle == null && ) {
                     soyMsgBundle = createSoyMsgBundle(.);
                 }
 
                 if (soyMsgBundle == null) {
                     return Optional.absent();
                 }
 
                 if (isHotReloadModeOff()) {
                     .put(locale.get(), soyMsgBundle);
                 }
             }
 
             return Optional.fromNullable(soyMsgBundle);
         }
     }

    
An implementation that using a ContextClassLoader iterates over all urls it finds based on a messagePath and locale, e.g. messages_de_DE.xlf and returns a merged SoyMsgBundle of SoyMsgBundle matching a a pattern it finds in a class path.

Parameters:
locale - locale
Returns:
SoyMsgBundle - bundle
Throws:
java.io.IOException - io error
    protected SoyMsgBundle createSoyMsgBundle(final Locale localethrows IOException {
        Preconditions.checkNotNull("messagesPath cannot be null!");
        final String path =   + "_" + locale.toString() + ".xlf";
        final Enumeration<URLe = Thread.currentThread().getContextClassLoader().getResources(path);
        final List<SoyMsgBundlemsgBundles = Lists.newArrayList();
        final SoyMsgBundleHandler msgBundleHandler = new SoyMsgBundleHandler(new XliffMsgPlugin());
        while (e.hasMoreElements()) {
            final URL msgFile = e.nextElement();
            final SoyMsgBundle soyMsgBundle = msgBundleHandler.createFromResource(msgFile);
            msgBundles.add(soyMsgBundle);
        }
        return mergeMsgBundles(localemsgBundles).orNull();
    }

    
Merge msg bundles together, creating new MsgBundle with merges msg bundles passed in as a method argument
    private Optional<? extends SoyMsgBundlemergeMsgBundles(final Locale localefinal List<SoyMsgBundlesoyMsgBundles) {
        if (soyMsgBundles.isEmpty()) {
            return Optional.absent();
        }
        final List<SoyMsgmsgs = Lists.newArrayList();
        for (final SoyMsgBundle smb : soyMsgBundles) {
            for (final Iterator<SoyMsgit = smb.iterator(); it.hasNext();) {
                msgs.add(it.next());
            }
        }
        return Optional.of(new SoyMsgBundleImpl(locale.toString(), msgs));
    }
    public void setMessagesPath(final String messagesPath) {
        this. = messagesPath;
    }
    public void setHotReloadMode(final boolean hotReloadMode) {
        this. = hotReloadMode;
    }
    public void setFallbackToEnglish(boolean fallbackToEnglish) {
        this. = fallbackToEnglish;
    }
    public String getMessagesPath() {
        return ;
    }
    public boolean isHotReloadMode() {
        return ;
    }
    private boolean isHotReloadModeOff() {
        return !;
    }
    public boolean isFallbackToEnglish() {
        return ;
    }
New to GrepCode? Check out our FAQ X