Project Wonder 5.0

er.extensions.localization
Class ERXLocalizer

java.lang.Object
  extended by er.extensions.localization.ERXLocalizer
All Implemented Interfaces:
com.webobjects.foundation.NSKeyValueCoding, com.webobjects.foundation.NSKeyValueCodingAdditions
Direct Known Subclasses:
ERXItalianLocalizer, ERXNonPluralFormLocalizer

public class ERXLocalizer
extends java.lang.Object
implements com.webobjects.foundation.NSKeyValueCoding, com.webobjects.foundation.NSKeyValueCodingAdditions

Provides KVC access to localization. Monitors a set of files in all loaded frameworks and returns a string given a key for a language. These types of keys are acceptable in the monitored files:


   "this is a test" = "some test";
   "unittest.key.path.as.string" = "some test";
   "unittest" = {
      "key" = { 
          "path" = { 
              "as" = {
                  "dict"="some test";
               };
          };
      };
   };
 
Note that if you only call for unittest, you'll get a dictionary not a string. So you can localize more complex objects than strings.
If you set the base class of your session to ERXSession, you can then use this code in your components:

  valueForKeyPath("session.localizer.this is a test")
  valueForKeyPath("session.localizer.unittest.key.path.as.string")
  valueForKeyPath("session.localizer.unittest.key.path.as.dict")
 
For sessionless Apps, you must use another method to get at the requested language and then call the localizer via:

  ERXLocalizer l = ERXLocalizer.localizerForLanguages(languagesThisUserCanHandle) or
  ERXLocalizer l = ERXLocalizer.localizerForLanguage("German")
 
These are the defaults can be set (listed with their current defaults):

  er.extensions.ERXLocalizer.defaultLanguage=English
  er.extensions.ERXLocalizer.fileNamesToWatch=("Localizable.strings","ValidationTemplate.strings")
  er.extensions.ERXLocalizer.availableLanguages=(English,German)
  er.extensions.ERXLocalizer.frameworkSearchPath=(app,ERDirectToWeb,ERExtensions)
 
There are also methods that pluralize using normal english pluralizing rules (y->ies, x -> xes etc). You can provide your own plural strings by using a dict entry:

  localizerExceptions = {
      "Table.0" = "Table"; 
      "Table" = "Tables";
      ...
  };
 
in your Localizable.strings. Table.0 meaning no "Table", Table.1 one table and Table any other number. Note: unlike all other keys, you need to give the translated value ("Tisch" for "Table" in German) as the key, not the untranslated one. This is because this method is mainly called via d2wContext.displayNameForProperty which is already localized.


Nested Class Summary
static class ERXLocalizer.Observer
           
 
Nested classes/interfaces inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions
com.webobjects.foundation.NSKeyValueCodingAdditions.DefaultImplementation, com.webobjects.foundation.NSKeyValueCodingAdditions.Utility
 
Nested classes/interfaces inherited from interface com.webobjects.foundation.NSKeyValueCoding
com.webobjects.foundation.NSKeyValueCoding._BooleanFieldBinding, com.webobjects.foundation.NSKeyValueCoding._BooleanMethodBinding, com.webobjects.foundation.NSKeyValueCoding._FieldBinding, com.webobjects.foundation.NSKeyValueCoding._ForwardingBinding, com.webobjects.foundation.NSKeyValueCoding._KeyBinding, com.webobjects.foundation.NSKeyValueCoding._KeyBindingCreation, com.webobjects.foundation.NSKeyValueCoding._MethodBinding, com.webobjects.foundation.NSKeyValueCoding._NumberFieldBinding, com.webobjects.foundation.NSKeyValueCoding._NumberMethodBinding, com.webobjects.foundation.NSKeyValueCoding._ReflectionKeyBindingCreation, com.webobjects.foundation.NSKeyValueCoding.ErrorHandling, com.webobjects.foundation.NSKeyValueCoding.Null, com.webobjects.foundation.NSKeyValueCoding.UnknownKeyException, com.webobjects.foundation.NSKeyValueCoding.ValueAccessor
 
Field Summary
protected  java.util.Hashtable _dateFormatters
           
protected  java.util.Hashtable _numberFormatters
           
protected  com.webobjects.foundation.NSMutableDictionary cache
           
protected static org.apache.log4j.Logger createdKeysLog
           
static java.lang.String KEY_LOCALIZER_EXCEPTIONS
           
protected  java.lang.String language
           
protected  java.util.Locale locale
           
static java.lang.String LocalizationDidResetNotification
           
protected static org.apache.log4j.Logger log
           
 
Fields inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions
_CLASS, _KeyPathSeparatorChar, KeyPathSeparator
 
Fields inherited from interface com.webobjects.foundation.NSKeyValueCoding
NullValue
 
Constructor Summary
ERXLocalizer(java.lang.String aLanguage)
           
 
Method Summary
protected  void addEntriesToCache(com.webobjects.foundation.NSDictionary dict)
           
protected  void addToCreatedKeys(java.lang.Object value, java.lang.String key)
           
protected  java.lang.String applyRules(java.lang.String str, java.util.Map rules)
          Apply the set of rules in the given Map to the input String and return a modified string that matches the case of the input string.
static com.webobjects.foundation.NSArray<java.lang.String> availableLanguages()
           
 com.webobjects.foundation.NSDictionary cache()
           
 com.webobjects.foundation.NSDictionary createdKeys()
           
protected static ERXLocalizer createLocalizerForLanguage(java.lang.String language, boolean pluralForm)
          Creates a localizer for a given language and with an indication if the language supports plural forms.
static ERXLocalizer currentLocalizer()
          Returns the current localizer for the current thread.
static java.lang.String defaultLanguage()
          Returns the default language (English) or the contents of the er.extensions.ERXLocalizer.defaultLanguage property.
static ERXLocalizer defaultLocalizer()
          Gets the localizer for the default language.
protected  java.util.Map defaultPlurifyRules()
          Returns the default plurify rules for this language.
protected  java.util.Map defaultSingularifyRules()
          Returns the default singularify rules for this language.
 void dumpCreatedKeys()
           
static ERXLocalizer englishLocalizer()
           
static boolean fallbackToDefaultLanguage()
           
static com.webobjects.foundation.NSArray fileNamesToWatch()
           
static com.webobjects.foundation.NSArray<java.lang.String> frameworkSearchPath()
           
static void initialize()
           
static boolean isLocalizationEnabled()
           
 java.lang.String language()
           
 java.lang.String languageCode()
           
 void load()
           
 java.util.Locale locale()
           
 java.text.Format localizedDateFormatForKey(java.lang.String formatString)
          Returns a localized date formatter for the given key.
 java.lang.String localizedDisplayNameForKey(java.lang.String prefix, java.lang.String key)
          Returns a localized string for the given prefix and keyPath, inserting it "prefix.keyPath" = "Key Path"; Also tries to find "Key Path"
 java.text.Format localizedNumberFormatForKey(java.lang.String formatString)
          Returns a localized number formatter for the given key.
 java.lang.String localizedStringForKey(java.lang.String key)
           
 java.lang.String localizedStringForKeyWithDefault(java.lang.String key)
           
 java.lang.String localizedTemplateStringForKeyWithObject(java.lang.String key, java.lang.Object o1)
           
 java.lang.String localizedTemplateStringForKeyWithObjectOtherObject(java.lang.String key, java.lang.Object o1, java.lang.Object o2)
           
 java.lang.Object localizedValueForKey(java.lang.String key)
          Returns the localized value for a key.
 java.lang.Object localizedValueForKeyWithDefault(java.lang.String key)
           
static ERXLocalizer localizerForLanguage(java.lang.String language)
           
static ERXLocalizer localizerForLanguages(com.webobjects.foundation.NSArray languages)
          Gets the best localizer for a set of languages.
static ERXLocalizer localizerForRequest(com.webobjects.appserver.WORequest request)
           
 java.lang.String plurifiedString(java.lang.String name, int count)
          Returns a plurified string
 java.lang.String plurifiedStringWithTemplateForKey(java.lang.String key, java.lang.String name, int count, java.lang.Object helper)
           
protected  java.lang.String plurify(java.lang.String str, int howMany)
           
protected  java.util.Map plurifyRules()
          Returns the plurify rules for the current language.
protected  com.webobjects.foundation.NSDictionary readPropertyListFromFileInFramework(java.lang.String fileName, java.lang.String framework, com.webobjects.foundation.NSArray languages)
           
static void resetCache()
          Resets the localizer cache.
static void setAvailableLanguages(com.webobjects.foundation.NSArray<java.lang.String> value)
           
protected  void setCacheValueForKey(java.lang.Object value, java.lang.String key)
           
static void setCurrentLocalizer(ERXLocalizer currentLocalizer)
          Sets a localizer for the current thread.
static void setDefaultLanguage(java.lang.String value)
          Sets the default language.
static void setFileNamesToWatch(com.webobjects.foundation.NSArray value)
           
static void setFrameworkSearchPath(com.webobjects.foundation.NSArray value)
           
static void setIsLocalizationEnabled(boolean value)
           
 void setLocale(java.util.Locale value)
           
 void setLocalizedDateFormatForKey(com.webobjects.foundation.NSTimestampFormatter formatter, java.lang.String pattern)
           
 void setLocalizedNumberFormatForKey(java.text.Format formatter, java.lang.String pattern)
           
static void setLocalizerForLanguage(ERXLocalizer l, java.lang.String language)
           
 java.lang.String singularifiedString(java.lang.String value)
          Returns a singularified string
protected  java.lang.String singularify(java.lang.String str)
           
protected  java.util.Map singularifyRules()
          Returns the singularify rules for the current language.
 void takeValueForKey(java.lang.Object value, java.lang.String key)
           
 void takeValueForKeyPath(java.lang.Object value, java.lang.String key)
           
 java.lang.String toString()
           
static boolean useLocalizedFormatters()
           
 java.lang.Object valueForKey(java.lang.String key)
          Cover method that calls localizedStringForKey.
 java.lang.Object valueForKeyPath(java.lang.String key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

KEY_LOCALIZER_EXCEPTIONS

public static final java.lang.String KEY_LOCALIZER_EXCEPTIONS
See Also:
Constant Field Values

log

protected static final org.apache.log4j.Logger log

createdKeysLog

protected static final org.apache.log4j.Logger createdKeysLog

LocalizationDidResetNotification

public static final java.lang.String LocalizationDidResetNotification
See Also:
Constant Field Values

cache

protected com.webobjects.foundation.NSMutableDictionary cache

_dateFormatters

protected java.util.Hashtable _dateFormatters

_numberFormatters

protected java.util.Hashtable _numberFormatters

language

protected java.lang.String language

locale

protected java.util.Locale locale
Constructor Detail

ERXLocalizer

public ERXLocalizer(java.lang.String aLanguage)
Method Detail

initialize

public static void initialize()

isLocalizationEnabled

public static boolean isLocalizationEnabled()

setIsLocalizationEnabled

public static void setIsLocalizationEnabled(boolean value)

currentLocalizer

public static ERXLocalizer currentLocalizer()
Returns the current localizer for the current thread. Note that the localizer for a given session is pushed onto the thread when a session awakes and is nulled out when a session sleeps. In case there is no localizer set, it tries to pull it from the current WOContext or the default language.

Returns:
the current localizer that has been pushed into thread storage.

setCurrentLocalizer

public static void setCurrentLocalizer(ERXLocalizer currentLocalizer)
Sets a localizer for the current thread. This is accomplished by using the object ERXThreadStorage

Parameters:
currentLocalizer - to set in thread storage for the current thread.

defaultLocalizer

public static ERXLocalizer defaultLocalizer()
Gets the localizer for the default language.


englishLocalizer

public static ERXLocalizer englishLocalizer()

localizerForRequest

public static ERXLocalizer localizerForRequest(com.webobjects.appserver.WORequest request)

resetCache

public static void resetCache()
Resets the localizer cache. If WOCaching is enabled then after being reinitialize all of the localizers will be reloaded.


addToCreatedKeys

protected void addToCreatedKeys(java.lang.Object value,
                                java.lang.String key)

localizerForLanguages

public static ERXLocalizer localizerForLanguages(com.webobjects.foundation.NSArray languages)
Gets the best localizer for a set of languages.

Parameters:
languages -

localizerForLanguage

public static ERXLocalizer localizerForLanguage(java.lang.String language)

defaultLanguage

public static java.lang.String defaultLanguage()
Returns the default language (English) or the contents of the er.extensions.ERXLocalizer.defaultLanguage property.


setDefaultLanguage

public static void setDefaultLanguage(java.lang.String value)
Sets the default language.

Parameters:
value -

fileNamesToWatch

public static com.webobjects.foundation.NSArray fileNamesToWatch()

setFileNamesToWatch

public static void setFileNamesToWatch(com.webobjects.foundation.NSArray value)

availableLanguages

public static com.webobjects.foundation.NSArray<java.lang.String> availableLanguages()

setAvailableLanguages

public static void setAvailableLanguages(com.webobjects.foundation.NSArray<java.lang.String> value)

frameworkSearchPath

public static com.webobjects.foundation.NSArray<java.lang.String> frameworkSearchPath()

setFrameworkSearchPath

public static void setFrameworkSearchPath(com.webobjects.foundation.NSArray value)

createLocalizerForLanguage

protected static ERXLocalizer createLocalizerForLanguage(java.lang.String language,
                                                         boolean pluralForm)
Creates a localizer for a given language and with an indication if the language supports plural forms. To provide your own subclass of an ERXLocalizer you can set the system property er.extensions.ERXLocalizer.pluralFormClassName or er.extensions.ERXLocalizer.nonPluralFormClassName.

Parameters:
language - name to construct the localizer for
pluralForm - denotes if the language supports the plural form
Returns:
a localizer for the given language

setLocalizerForLanguage

public static void setLocalizerForLanguage(ERXLocalizer l,
                                           java.lang.String language)

cache

public com.webobjects.foundation.NSDictionary cache()

load

public void load()

plurifyRules

protected java.util.Map plurifyRules()
Returns the plurify rules for the current language. This first checks for a property of the form: er.extensions.ERXLocalizer.en.plurifyRules=(.*)person$=$1people:(.*)man$=$1men which is er.extensions.ERXLocalizer.en.plurifyRules=pattern1=replacement1:pattern2=replacement2:etc In the absence of a rule set for a particular language, the default rules are English and ported from the pluralizer in Rails.

Returns:
the plurify rules for the current language

defaultPlurifyRules

protected java.util.Map defaultPlurifyRules()
Returns the default plurify rules for this language. The default implementation is English and ported from the plurify code in Ruby on Rails. The returned Map should have regex Pattern objects as keys mapping to the replacement String to apply to that pattern.

Returns:
the default plurify rules

singularifyRules

protected java.util.Map singularifyRules()
Returns the singularify rules for the current language. This first checks for a property of the form: er.extensions.ERXLocalizer.en.singularifyRules=(.*)person$=$1people:(.*)man$=$1men which is er.extensions.ERXLocalizer.en.singularifyRules=pattern1=replacement1:pattern2=replacement2:etc In the absence of a rule set for a particular language, the default rules are English and ported from the singularizer in Rails.

Returns:
the singularify rules for the current language

defaultSingularifyRules

protected java.util.Map defaultSingularifyRules()
Returns the default singularify rules for this language. The default implementation is English and ported from the singularize code in Ruby on Rails. The returned Map should have regex Pattern objects as keys mapping to the replacement String to apply to that pattern.

Returns:
the default singularify rules

addEntriesToCache

protected void addEntriesToCache(com.webobjects.foundation.NSDictionary dict)

readPropertyListFromFileInFramework

protected com.webobjects.foundation.NSDictionary readPropertyListFromFileInFramework(java.lang.String fileName,
                                                                                     java.lang.String framework,
                                                                                     com.webobjects.foundation.NSArray languages)

valueForKey

public java.lang.Object valueForKey(java.lang.String key)
Cover method that calls localizedStringForKey.

Specified by:
valueForKey in interface com.webobjects.foundation.NSKeyValueCoding
Parameters:
key - to resolve a localized varient of
Returns:
localized string for the given key

setCacheValueForKey

protected void setCacheValueForKey(java.lang.Object value,
                                   java.lang.String key)

valueForKeyPath

public java.lang.Object valueForKeyPath(java.lang.String key)
Specified by:
valueForKeyPath in interface com.webobjects.foundation.NSKeyValueCodingAdditions

takeValueForKey

public void takeValueForKey(java.lang.Object value,
                            java.lang.String key)
Specified by:
takeValueForKey in interface com.webobjects.foundation.NSKeyValueCoding

takeValueForKeyPath

public void takeValueForKeyPath(java.lang.Object value,
                                java.lang.String key)
Specified by:
takeValueForKeyPath in interface com.webobjects.foundation.NSKeyValueCodingAdditions

language

public java.lang.String language()

createdKeys

public com.webobjects.foundation.NSDictionary createdKeys()

dumpCreatedKeys

public void dumpCreatedKeys()

localizedValueForKeyWithDefault

public java.lang.Object localizedValueForKeyWithDefault(java.lang.String key)

localizedValueForKey

public java.lang.Object localizedValueForKey(java.lang.String key)
Returns the localized value for a key. An @ keypath such as session.localizer.@locale.getLanguage indicates that the methods on ERXLocalizer itself should be called instead of searching the strings file for a '@locale.getLanguage' key.

Parameters:
key - the keyPath string
Returns:
a localized string value or the object value of the @ keyPath

localizedStringForKeyWithDefault

public java.lang.String localizedStringForKeyWithDefault(java.lang.String key)

localizedStringForKey

public java.lang.String localizedStringForKey(java.lang.String key)

localizedDisplayNameForKey

public java.lang.String localizedDisplayNameForKey(java.lang.String prefix,
                                                   java.lang.String key)
Returns a localized string for the given prefix and keyPath, inserting it "prefix.keyPath" = "Key Path"; Also tries to find "Key Path"

Parameters:
prefix -
key -

localizedTemplateStringForKeyWithObject

public java.lang.String localizedTemplateStringForKeyWithObject(java.lang.String key,
                                                                java.lang.Object o1)

localizedTemplateStringForKeyWithObjectOtherObject

public java.lang.String localizedTemplateStringForKeyWithObjectOtherObject(java.lang.String key,
                                                                           java.lang.Object o1,
                                                                           java.lang.Object o2)

plurify

protected java.lang.String plurify(java.lang.String str,
                                   int howMany)

singularify

protected java.lang.String singularify(java.lang.String str)

applyRules

protected java.lang.String applyRules(java.lang.String str,
                                      java.util.Map rules)
Apply the set of rules in the given Map to the input String and return a modified string that matches the case of the input string. For instance, if the input string is "Person" and the rules are _plurifyRules, then this would return "People".

Parameters:
str - the input string
rules - the rules to apply
Returns:
a case-matched string converted according to the rules

plurifiedStringWithTemplateForKey

public java.lang.String plurifiedStringWithTemplateForKey(java.lang.String key,
                                                          java.lang.String name,
                                                          int count,
                                                          java.lang.Object helper)

plurifiedString

public java.lang.String plurifiedString(java.lang.String name,
                                        int count)
Returns a plurified string

Parameters:
name -
count -

singularifiedString

public java.lang.String singularifiedString(java.lang.String value)
Returns a singularified string

Parameters:
value - the value to singularify
Returns:
a singularified string

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

localizedDateFormatForKey

public java.text.Format localizedDateFormatForKey(java.lang.String formatString)
Returns a localized date formatter for the given key.

Parameters:
formatString -

localizedNumberFormatForKey

public java.text.Format localizedNumberFormatForKey(java.lang.String formatString)
Returns a localized number formatter for the given key. Also, can localize units to, just define in your Localizable.strings a suitable key, with the appropriate pattern.

Parameters:
formatString -
Returns:
the formatter object

setLocalizedNumberFormatForKey

public void setLocalizedNumberFormatForKey(java.text.Format formatter,
                                           java.lang.String pattern)
Parameters:
formatter -
pattern -

locale

public java.util.Locale locale()

setLocale

public void setLocale(java.util.Locale value)

setLocalizedDateFormatForKey

public void setLocalizedDateFormatForKey(com.webobjects.foundation.NSTimestampFormatter formatter,
                                         java.lang.String pattern)
Parameters:
formatter -
pattern -

useLocalizedFormatters

public static boolean useLocalizedFormatters()

languageCode

public java.lang.String languageCode()

fallbackToDefaultLanguage

public static boolean fallbackToDefaultLanguage()

Last updated: Tue, Feb 21, 2017 • 05:45 PM CET

Copyright © 2002 – 2007 Project Wonder.