ERXLocalizer Class Reference

Inherits com::webobjects::foundation::NSKeyValueCoding, and com::webobjects::foundation::NSKeyValueCodingAdditions.

Inherited by ERXItalianLocalizer, and ERXNonPluralFormLocalizer.

Collaboration diagram for ERXLocalizer:

Collaboration graph
[legend]

List of all members.

Classes

class  Observer

Public Member Functions

NSDictionary cache ()
NSDictionary createdKeys ()
void dumpCreatedKeys ()
 ERXLocalizer (String aLanguage)
String language ()
String languageCode ()
void load ()
Locale locale ()
Format localizedDateFormatForKey (String formatString)
String localizedDisplayNameForKey (String prefix, String key)
Format localizedNumberFormatForKey (String formatString)
String localizedStringForKey (String key)
String localizedStringForKeyWithDefault (String key)
String localizedTemplateStringForKeyWithObject (String key, Object o1)
String localizedTemplateStringForKeyWithObjectOtherObject (String key, Object o1, Object o2)
Object localizedValueForKey (String key)
Object localizedValueForKeyWithDefault (String key)
String plurifiedString (String name, int count)
String plurifiedStringWithTemplateForKey (String key, String name, int count, Object helper)
void setLocale (Locale value)
void setLocalizedDateFormatForKey (NSTimestampFormatter formatter, String pattern)
void setLocalizedNumberFormatForKey (Format formatter, String pattern)
String singularifiedString (String value)
void takeValueForKey (Object value, String key)
void takeValueForKeyPath (Object value, String key)
String toString ()
Object valueForKey (String key)
Object valueForKeyPath (String key)

Static Public Member Functions

static NSArray< StringavailableLanguages ()
static ERXLocalizer currentLocalizer ()
static String defaultLanguage ()
static ERXLocalizer defaultLocalizer ()
static ERXLocalizer englishLocalizer ()
static boolean fallbackToDefaultLanguage ()
static NSArray fileNamesToWatch ()
static NSArray< StringframeworkSearchPath ()
static void initialize ()
static boolean isLocalizationEnabled ()
static ERXLocalizer localizerForLanguage (String language)
static ERXLocalizer localizerForLanguages (NSArray languages)
static ERXLocalizer localizerForRequest (WORequest request)
static void resetCache ()
static void setAvailableLanguages (NSArray< String > value)
static void setCurrentLocalizer (ERXLocalizer currentLocalizer)
static void setDefaultLanguage (String value)
static void setFileNamesToWatch (NSArray value)
static void setFrameworkSearchPath (NSArray value)
static void setIsLocalizationEnabled (boolean value)
static void setLocalizerForLanguage (ERXLocalizer l, String language)
static boolean useLocalizedFormatters ()

Static Public Attributes

static final String KEY_LOCALIZER_EXCEPTIONS = "localizerExceptions"
static final String LocalizationDidResetNotification = "LocalizationDidReset"

Protected Member Functions

void addEntriesToCache (NSDictionary dict)
void addToCreatedKeys (Object value, String key)
String applyRules (String str, Map rules)
Map defaultPlurifyRules ()
Map defaultSingularifyRules ()
String plurify (String str, int howMany)
Map plurifyRules ()
NSDictionary readPropertyListFromFileInFramework (String fileName, String framework, NSArray languages)
void setCacheValueForKey (Object value, String key)
String singularify (String str)
Map singularifyRules ()

Static Protected Member Functions

static ERXLocalizer createLocalizerForLanguage (String language, boolean pluralForm)

Protected Attributes

Hashtable _dateFormatters = new Hashtable()
Hashtable _numberFormatters = new Hashtable()
NSMutableDictionary cache
String language
Locale locale

Static Protected Attributes

static final Logger createdKeysLog = Logger.getLogger(ERXLocalizer.class.getName() + ".createdKeys")
static final Logger log = Logger.getLogger(ERXLocalizer.class)

Static Package Attributes

static NSArray< StringavailableLanguages
static String defaultLanguage
static NSArray< StringfileNamesToWatch
static NSArray< StringframeworkSearchPath
static NSMutableDictionary localizers = new NSMutableDictionary()

Private Member Functions

String displayNameForKey (String key)

Private Attributes

Map _plurifyRules
Map _singularifyRules
NSMutableDictionary createdKeys
String NOT_FOUND = "**NOT_FOUND**"

Static Private Attributes

static Boolean _fallbackToDefaultLanguage
static NSArray _languagesWithoutPluralForm = new NSArray(new Object[] { "Japanese" })
static final char _localizerMethodIndicatorCharacter = '@'
static Boolean _useLocalizedFormatters
static boolean isInitialized = false
static boolean isLocalizationEnabled = true
static NSMutableArray monitoredFiles = new NSMutableArray()
static Observer observer = new Observer()


Detailed Description

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.


Constructor & Destructor Documentation

ERXLocalizer ( String  aLanguage  ) 


Member Function Documentation

void addEntriesToCache ( NSDictionary  dict  )  [protected]

void addToCreatedKeys ( Object  value,
String  key 
) [protected]

String applyRules ( String  str,
Map  rules 
) [protected]

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

static NSArray<String> availableLanguages (  )  [static]

NSDictionary cache (  ) 

NSDictionary createdKeys (  ) 

static ERXLocalizer createLocalizerForLanguage ( String  language,
boolean  pluralForm 
) [static, protected]

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

static ERXLocalizer currentLocalizer (  )  [static]

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.

static String defaultLanguage (  )  [static]

Returns the default language (English) or the contents of the er.extensions.ERXLocalizer.defaultLanguage property.

static ERXLocalizer defaultLocalizer (  )  [static]

Gets the localizer for the default language.

Map defaultPlurifyRules (  )  [protected]

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

Map defaultSingularifyRules (  )  [protected]

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

String displayNameForKey ( String  key  )  [private]

void dumpCreatedKeys (  ) 

static ERXLocalizer englishLocalizer (  )  [static]

static boolean fallbackToDefaultLanguage (  )  [static]

static NSArray fileNamesToWatch (  )  [static]

static NSArray<String> frameworkSearchPath (  )  [static]

static void initialize (  )  [static]

static boolean isLocalizationEnabled (  )  [static]

String language (  ) 

String languageCode (  ) 

void load (  ) 

Locale locale (  ) 

Format localizedDateFormatForKey ( String  formatString  ) 

Returns a localized date formatter for the given key.

Parameters:
formatString 

String localizedDisplayNameForKey ( String  prefix,
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 

Format localizedNumberFormatForKey ( 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

String localizedStringForKey ( String  key  ) 

String localizedStringForKeyWithDefault ( String  key  ) 

String localizedTemplateStringForKeyWithObject ( String  key,
Object  o1 
)

String localizedTemplateStringForKeyWithObjectOtherObject ( String  key,
Object  o1,
Object  o2 
)

Object localizedValueForKey ( 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 '.getLanguage' key.

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

Object localizedValueForKeyWithDefault ( String  key  ) 

static ERXLocalizer localizerForLanguage ( String  language  )  [static]

static ERXLocalizer localizerForLanguages ( NSArray  languages  )  [static]

Gets the best localizer for a set of languages.

Parameters:
languages 

static ERXLocalizer localizerForRequest ( WORequest  request  )  [static]

String plurifiedString ( String  name,
int  count 
)

Returns a plurified string

Parameters:
name 
count 

Reimplemented in ERXNonPluralFormLocalizer.

String plurifiedStringWithTemplateForKey ( String  key,
String  name,
int  count,
Object  helper 
)

String plurify ( String  str,
int  howMany 
) [protected]

Reimplemented in ERXItalianLocalizer.

Map plurifyRules (  )  [protected]

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

NSDictionary readPropertyListFromFileInFramework ( String  fileName,
String  framework,
NSArray  languages 
) [protected]

static void resetCache (  )  [static]

Resets the localizer cache. If WOCaching is enabled then after being reinitialize all of the localizers will be reloaded.

static void setAvailableLanguages ( NSArray< String value  )  [static]

void setCacheValueForKey ( Object  value,
String  key 
) [protected]

static void setCurrentLocalizer ( ERXLocalizer  currentLocalizer  )  [static]

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.

static void setDefaultLanguage ( String  value  )  [static]

Sets the default language.

Parameters:
value 

static void setFileNamesToWatch ( NSArray  value  )  [static]

static void setFrameworkSearchPath ( NSArray  value  )  [static]

static void setIsLocalizationEnabled ( boolean  value  )  [static]

void setLocale ( Locale  value  ) 

void setLocalizedDateFormatForKey ( NSTimestampFormatter  formatter,
String  pattern 
)

Parameters:
formatter 
pattern 

void setLocalizedNumberFormatForKey ( Format  formatter,
String  pattern 
)

Parameters:
formatter 
pattern 

static void setLocalizerForLanguage ( ERXLocalizer  l,
String  language 
) [static]

String singularifiedString ( String  value  ) 

Returns a singularified string

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

String singularify ( String  str  )  [protected]

Map singularifyRules (  )  [protected]

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

void takeValueForKey ( Object  value,
String  key 
)

void takeValueForKeyPath ( Object  value,
String  key 
)

String toString (  ) 

Reimplemented in ERXNonPluralFormLocalizer.

static boolean useLocalizedFormatters (  )  [static]

Object valueForKey ( String  key  ) 

Cover method that calls localizedStringForKey.

Parameters:
key to resolve a localized varient of
Returns:
localized string for the given key

Object valueForKeyPath ( String  key  ) 


Member Data Documentation

Hashtable _dateFormatters = new Hashtable() [protected]

Boolean _fallbackToDefaultLanguage [static, private]

NSArray _languagesWithoutPluralForm = new NSArray(new Object[] { "Japanese" }) [static, private]

final char _localizerMethodIndicatorCharacter = '@' [static, private]

Hashtable _numberFormatters = new Hashtable() [protected]

Map _plurifyRules [private]

Map _singularifyRules [private]

Boolean _useLocalizedFormatters [static, private]

NSArray<String> availableLanguages [static, package]

final Logger createdKeysLog = Logger.getLogger(ERXLocalizer.class.getName() + ".createdKeys") [static, protected]

String defaultLanguage [static, package]

NSArray<String> fileNamesToWatch [static, package]

NSArray<String> frameworkSearchPath [static, package]

boolean isInitialized = false [static, private]

boolean isLocalizationEnabled = true [static, private]

final String KEY_LOCALIZER_EXCEPTIONS = "localizerExceptions" [static]

String language [protected]

Locale locale [protected]

final String LocalizationDidResetNotification = "LocalizationDidReset" [static]

final Logger log = Logger.getLogger(ERXLocalizer.class) [static, protected]

NSMutableArray monitoredFiles = new NSMutableArray() [static, private]

String NOT_FOUND = "**NOT_FOUND**" [private]

Observer observer = new Observer() [static, private]


The documentation for this class was generated from the following file:

Generated on Sat May 26 06:43:16 2012 for Project Wonder by  doxygen 1.5.8