Class BundleFunctions

java.lang.Object
com.github.mustachejava.functions.BundleFunctions

public class BundleFunctions extends Object
Factory for Mustache.java translation functions based on localized Resource bundles. Example with a class:
public class ... {
    Function trans = BundleFunctions.newPreTranslate("com.project.locale", Locale.US);
    ...
}
Example with a map:
Map <String, Object> scopes =  new HashMap%lt;String, Object>();
scopes.put("trans", BundleFunctions.newPostTranslateNullableLabel("com.project.locale", Locale.US);
...
Usage in template: {{#trans}}TranslatedLabel1{{/trans}}
  • Constructor Details

    • BundleFunctions

      public BundleFunctions()
  • Method Details

    • newPreTranslate

      public static Function newPreTranslate(String bundle, Locale locale)
      Returns a Function that operates prior to template evaluation and returns unknown keys intact. Given the following HTML:
      {{#trans}}Label1{{/trans}}
      {{#trans}}Label.unknown{{/trans}}
      {{#trans}}Label.{{replaceMe}}{{/trans}}
      
      and the following properties in the provided bundle:
      Label1=hello
      Label.replaced=world
      
      and a mapping from replaceMe to the value replaced, the following output will be generated:
      hello
      Label.unknown
      Label.replaced
      
      Parameters:
      bundle - name of the resource bundle
      locale - translation locale
      Returns:
      Function that operates prior to template evaluation and returns unknown keys intact
    • newPreTranslateNullableLabel

      public static Function newPreTranslateNullableLabel(String bundle, Locale locale)
      Returns a Function that operates prior to template evaluation and returns nulls for unknown keys. Given the following HTML:
      {{#trans}}Label1{{/trans}}
      {{#trans}}Label.unknown{{/trans}}
      {{#trans}}Label.{{replaceMe}}{{/trans}}
      
      and the following properties in the provided bundle:
      Label1=hello
      Label.replaced=world
      
      and a mapping from replaceMe to the value replaced, the following output will be generated:
      hello
      
      Parameters:
      bundle - name of the resource bundle
      locale - translation locale
      Returns:
      Function that operates prior to template evaluation and returns nulls for unknown keys
    • newPostTranslate

      public static Function newPostTranslate(String bundle, Locale locale)
      Returns a Function that operates after template evaluation and returns unknown keys intact. Given the following HTML:
      {{#trans}}Label1{{/trans}}
      {{#trans}}Label.unknown{{/trans}}
      {{#trans}}Label.{{replaceMe}}{{/trans}}
      
      and the following properties in the provided bundle:
      Label1=hello
      Label.replaced=world
      
      and a mapping from replaceMe to the value replaced, the following output will be generated:
      hello
      Label.unknown
      world
      
      Parameters:
      bundle - name of the resource bundle
      locale - translation locale
      Returns:
      Function that operates after template evaluation and returns unknown keys intact
    • newPostTranslateNullableLabel

      public static Function newPostTranslateNullableLabel(String bundle, Locale locale)
      Returns a Function that operates after template evaluation and returns nulls for unknown keys. Given the following HTML:
      {{#trans}}Label1{{/trans}}
      {{#trans}}Label.unknown{{/trans}}
      {{#trans}}Label.{{replaceMe}}{{/trans}}
      
      and the following properties in the provided bundle:
      Label1=hello
      Label.replaced=world
      
      and a mapping from replaceMe to the value replaced, the following output will be generated:
      hello
      world
      
      Parameters:
      bundle - name of the resource bundle
      locale - translation locale
      Returns:
      Function that operates after template evaluation and returns nulls for unknown keys