Package com.ilib

Class IResourceBundle


  • public class IResourceBundle
    extends java.lang.Object
    A class that represents a resource bundle for Android with methods that can look up translated strings by the source string or by unique key. The Java ResourceBundle class does not offer those methods and is not subclassable because many of its methods are final. So, this class is a wrapper around ResourceBundle which delegates much of its functionality to ResourceBundle and adds the look-up by source string functionality.

    The class ResBundle looks up strings in json files. This class uses the regular Java properties files (in properties or xml format) so it can be used as a replacement for regular ResourceBundles and does not require rewriting existing resources as json.

    • Constructor Summary

      Constructors 
      Constructor Description
      IResourceBundle()
      Construct a new IResourceBundle and load in the Java ResourceBundle for delegation.
      IResourceBundle​(java.lang.String baseName)
      Construct a new IResourceBundle and load in the Java ResourceBundle for delegation.
      IResourceBundle​(java.lang.String baseName, IlibLocale locale)
      Construct a new IResourceBundle and load in the Java ResourceBundle for delegation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean containsKey​(java.lang.String sourceOrKey)
      Examines if given unique key or source string has already been translated or not.
      boolean containsSource​(java.lang.String source)
      Examines if source string has already been translated or not
      protected java.lang.String escape​(java.lang.String str)
      Replaces all xml and html tags occurences with corresponding replacements
      java.lang.String getBaseBundleName()
      Returns the base name of this bundle, if known, or null if unknown.
      java.util.Enumeration<java.lang.String> getKeys()
      Delegated
      IlibLocale getLocale()
      Returns target locale object as an IlibLocale object
      IResourceBundle.MissingType getMissing()
      Returns missing option - that option specifies behavior in case when translation is not found by given key or value.
      protected java.lang.String getPseudoCharacter​(java.lang.String character)
      Returns string with pseudo character that corresponds to the input one
      java.lang.String getString​(java.lang.String sourceOrKey)
      Retrieve a string from the resources based on the source string or the key.
      java.lang.String getString​(java.lang.String source, java.lang.String key)
      Returns translation for given source and key strings.
      java.lang.String[] getStringArray​(java.lang.String key)
      Gets a string array for the given key from this resource bundle or one of its parents.
      java.lang.String getStringPseudo​(java.lang.String source)
      Return the pseudo-translated version of the source string.
      protected java.lang.String getTranslation​(java.lang.String source, java.lang.String key)
      Get the translation of the given source string and key combination.
      java.lang.String getType()
      Returns type of text information which ResBundle is being operating with (default type: raw)
      protected void initPseudoMap()  
      protected boolean isHTML_XML_Type()
      Return true if the output file type is HTML or XML.
      boolean isLengthen()
      Returns lengthen option that allows to lengthen potential length for translated string (for UI/UX issues) using pseudo-localization.
      protected java.lang.String makeKey​(java.lang.String source)
      Creates a unique key from given source string
      protected java.lang.String pseudo​(java.lang.String source)
      Returns pseudo string from input one using appropriate pseudomap
      void setLengthen​(boolean lengthen)
      Specifies lengthen option to lengthen potential length for translated string (for UI/UX issues) using pseudo-localization.
      void setMissingType​(IResourceBundle.MissingType type)
      Specifies missing option - option, that specifies behavior in case when translation is not found by given key or value.
      void setType​(java.lang.String type)
      Specifies type of text information which ResBundle is being operating with
      protected java.lang.String unescape​(java.lang.String str)
      Invokes retroaction for escape(String)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • sourceLocale

        protected static volatile IlibLocale sourceLocale
      • rb

        protected java.util.ResourceBundle rb
      • name

        protected java.lang.String name
      • type

        protected java.lang.String type
      • pseudoCharacters

        protected java.util.Map<java.lang.String,​java.lang.String> pseudoCharacters
      • lengthen

        protected boolean lengthen
    • Constructor Detail

      • IResourceBundle

        public IResourceBundle​(java.lang.String baseName,
                               IlibLocale locale)
        Construct a new IResourceBundle and load in the Java ResourceBundle for delegation.
        Parameters:
        baseName - the baseName for this bundle, or null to use the default
        locale - the ilib locale for this bundle, or null to use the current locale
      • IResourceBundle

        public IResourceBundle​(java.lang.String baseName)
        Construct a new IResourceBundle and load in the Java ResourceBundle for delegation. This uses the given base name but the default locale.
        Parameters:
        baseName - the baseName for this bundle, or null to use the default
      • IResourceBundle

        public IResourceBundle()
        Construct a new IResourceBundle and load in the Java ResourceBundle for delegation. This method uses the default locale and the default base name.
    • Method Detail

      • getKeys

        public java.util.Enumeration<java.lang.String> getKeys()
        Delegated
        Returns:
      • initPseudoMap

        protected void initPseudoMap()
      • getLocale

        public IlibLocale getLocale()
        Returns target locale object as an IlibLocale object
        Returns:
        target locale instance
      • containsSource

        public boolean containsSource​(java.lang.String source)
        Examines if source string has already been translated or not
        Parameters:
        source - source string to be examined
        Returns:
        true if source string is found in translations, otherwise - false
      • containsKey

        public boolean containsKey​(java.lang.String sourceOrKey)
        Examines if given unique key or source string has already been translated or not.
        Parameters:
        sourceOrKey - unique key or source string to be examined
        Returns:
        true if source string or unique key are found in translations. False otherwise.
      • getStringArray

        public java.lang.String[] getStringArray​(java.lang.String key)
        Gets a string array for the given key from this resource bundle or one of its parents.
        Parameters:
        key - unique key of the string array
        Returns:
        an array of string for the unique key
      • getBaseBundleName

        public java.lang.String getBaseBundleName()
        Returns the base name of this bundle, if known, or null if unknown.
        Returns:
        base name of this bundle
      • getType

        public java.lang.String getType()
        Returns type of text information which ResBundle is being operating with (default type: raw)
        Returns:
        input text type: html, xml, raw
      • setType

        public void setType​(java.lang.String type)
        Specifies type of text information which ResBundle is being operating with
        Parameters:
        type - input text specified type (default type: raw).
      • isLengthen

        public boolean isLengthen()
        Returns lengthen option that allows to lengthen potential length for translated string (for UI/UX issues) using pseudo-localization.
        Returns:
        true if lengthen option is specified, false otherwise
      • setLengthen

        public void setLengthen​(boolean lengthen)
        Specifies lengthen option to lengthen potential length for translated string (for UI/UX issues) using pseudo-localization.
        Parameters:
        lengthen - option allows to lengthen potential translated string length using pseudo string as returned instead.
      • getMissing

        public IResourceBundle.MissingType getMissing()
        Returns missing option - that option specifies behavior in case when translation is not found by given key or value. Possible values: SOURCE - return source string if translation is not found; PSEUDO - return pseudo localized string from source string if translation is not found; EMPTY - return empty string if translation is not found.
        Returns:
        missing option value
      • setMissingType

        public void setMissingType​(IResourceBundle.MissingType type)
        Specifies missing option - option, that specifies behavior in case when translation is not found by given key or value. Possible values: SOURCE - return source string if translation is not found; PSEUDO - return pseudo localized string from source string if translation is not found; EMPTY - return empty string if translation is not found.
        Parameters:
        type -
      • pseudo

        protected java.lang.String pseudo​(java.lang.String source)
        Returns pseudo string from input one using appropriate pseudomap
        Parameters:
        source - input string needed to be pseudo localized
        Returns:
        pseudo localized string
      • getPseudoCharacter

        protected java.lang.String getPseudoCharacter​(java.lang.String character)
        Returns string with pseudo character that corresponds to the input one
        Parameters:
        character - input character string
        Returns:
        string with pseudo character if input character is found in appropriate pseudoMap, otherwise - input character
      • escape

        protected java.lang.String escape​(java.lang.String str)
        Replaces all xml and html tags occurences with corresponding replacements
        Parameters:
        str - input string
        Returns:
        string with no html- and xml-style tags
      • unescape

        protected java.lang.String unescape​(java.lang.String str)
        Invokes retroaction for escape(String)
        Parameters:
        str - input string
        Returns:
        string with html- and xml-style tags
        See Also:
        escape(String str);
      • makeKey

        protected java.lang.String makeKey​(java.lang.String source)
        Creates a unique key from given source string
        Parameters:
        source - input source string
        Returns:
        unique key that contains no spaces, and modified equals and colon signs
      • getString

        public java.lang.String getString​(java.lang.String source,
                                          java.lang.String key)
        Returns translation for given source and key strings.
        Parameters:
        source - source string to look up
        key - unique key for the string. If null - then unique key will be generated based on the source string
        Returns:
        translation for target locale if it is exists, otherwise the source string
        See Also:
        makeKey(String), IResourceBundle.MissingType
      • getTranslation

        protected java.lang.String getTranslation​(java.lang.String source,
                                                  java.lang.String key)
        Get the translation of the given source string and key combination. At least one of the source or the key must be specified.
        Parameters:
        source - The source string written in the source language, or null if no source string is available
        key - The unique key being sought, or null if no key is available
        Returns:
        the translation for the given source string/key combination
      • isHTML_XML_Type

        protected boolean isHTML_XML_Type()
        Return true if the output file type is HTML or XML.
        Returns:
        true if the output file type is HTML or XML.
      • getString

        public java.lang.String getString​(java.lang.String sourceOrKey)
        Retrieve a string from the resources based on the source string or the key. If the source string is given, a key will be made out of it and the key will be looked up in the resources. If a key is given, the key will be looked up directly in the resources. If the string cannot be looked up by either the source or the key, this method will use the current missing string strategy.
        Parameters:
        sourceOrKey - A source string or unique key to look up in the resources
        Returns:
        the translation for the given source string or key string
      • getStringPseudo

        public java.lang.String getStringPseudo​(java.lang.String source)
        Return the pseudo-translated version of the source string.
        Parameters:
        source - the source string to translate.
        Returns:
        the pseudo-translated version of the source string