Class LocaleTransposition

java.lang.Object
org.ocpsoft.rewrite.transposition.LocaleTransposition
All Implemented Interfaces:
Constraint<String>, ParameterConfigurator, Transposition<String>

public class LocaleTransposition extends Object implements Transposition<String>, Constraint<String>
A Transposition and/or Constraint responsible for translating values from their matching translation from a ResourceBundle. The lookup in the properties file is case-sensitive. The properties file should use ISO-8859-1 encoding as defined by PropertyResourceBundle. Requires inverted properties files in the form of translated_name=name_to_bind. TODO Should we allow to load the properties file in a different encoding? TODO Allow to enable case insensitive and/or ascii-folding on property lookup?
Author:
Christian Gendreau
  • Method Details

    • bundle

      public static LocaleTransposition bundle(String bundleName, String localeParam)
      Create a Binding to a ResourceBundle, where the initial bound Parameter value is used as the bundle lookup key. The resultant value of the bundle lookup is stored as the new bound Parameter value.

      For example, consider the following URL-based rule:

       Configuration config = ConfigurationBuilder.begin()
                .addRule(Join.path("/{lang}/{path}").to("/{path}"))
                .where("path").transposedBy(LocaleTransposition.bundle("org.example.Paths", "lang"));
       

      In the above scenario, "org.example.Paths" is the resource bundle name. The value of the Parameter "lang" is extracted from the inbound request URL, and used as the bundle Locale. The initial value of the Parameter "path" is used as the lookup key, and is transposed to the value of the corresponding resource bundle entry. Once transposition has occurred, after rule evaluation, subsequent references to the "path" Parameter will return the value from the ResourceBundle entry.

      When this example is applied to a URL of: "/de/bibliotek", assuming a bundle called "org.example.Paths_de" exists and contains the an entry "bibliotek=library", the rule will forward to the new URL: "/library", because the value of "path" has been transposed by LocaleTransposition.

      Parameters:
      bundleName - Fully qualified name of the ResourceBundle
      localeParam - The name of the Parameter that contains the ISO 639-1 language code to be used with Locale.
      Returns:
      new instance of LocaleBinding
    • onTranspositionFailed

      public LocaleTransposition onTranspositionFailed(Operation onFailureOperation)
      Specify an Operation to be added as a preOperation in case the Transposition failed. Failure occurs when no ResourceBundle can be found for the requested language or when a value can not be transposed due to a missing key in the resource bundle.
      Parameters:
      onFailureOperation -
      Returns:
    • transpose

      public String transpose(Rewrite event, EvaluationContext context, String value)
      Description copied from interface: Transposition
      Perform an operation on the given value.
      Specified by:
      transpose in interface Transposition<String>
    • isSatisfiedBy

      public boolean isSatisfiedBy(Rewrite event, EvaluationContext context, String value)
      Description copied from interface: Constraint
      Return true if this Constraint is satisfied by the given value; otherwise, return false.
      Specified by:
      isSatisfiedBy in interface Constraint<String>