er.extensions
Class ERXLocalizer
java.lang.Object
er.extensions.ERXLocalizer
- All Implemented Interfaces:
- NSKeyValueCoding, NSKeyValueCodingAdditions
- Direct Known Subclasses:
- ERXNonPluralFormLocalizer
- public class ERXLocalizer
- extends Object
- implements NSKeyValueCoding, NSKeyValueCodingAdditions
KVC access to localization.
Monitors a set of files in all frameworks and returns a string given a key for a language.
In the current state, it's more a stub for things to come.
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. 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 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)
TODO: chaining of Localizers
Nested classes inherited from class com.webobjects.foundation.NSKeyValueCoding |
NSKeyValueCoding._BooleanFieldBinding, NSKeyValueCoding._BooleanMethodBinding, NSKeyValueCoding._FieldBinding, NSKeyValueCoding._ForwardingBinding, NSKeyValueCoding._KeyBinding, NSKeyValueCoding._KeyBindingCreation, NSKeyValueCoding._MethodBinding, NSKeyValueCoding._NumberFieldBinding, NSKeyValueCoding._NumberMethodBinding, NSKeyValueCoding._ReflectionKeyBindingCreation, NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.Null, NSKeyValueCoding.UnknownKeyException, NSKeyValueCoding.Utility, NSKeyValueCoding.ValueAccessor |
Method Summary |
static NSArray |
availableLanguages()
|
NSDictionary |
createdKeys()
|
protected static ERXLocalizer |
createLocalizerForLanguage(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 String |
defaultLanguage()
|
static ERXLocalizer |
defaultLocalizer()
Gets the localizer for the default language. |
static NSDictionary |
fakeSessionForLanguage(String language)
|
static NSDictionary |
fakeSessionForSession(Object session)
|
static NSArray |
fileNamesToWatch()
|
static NSArray |
frameworkSearchPath()
|
static void |
initialize()
|
static boolean |
isLocalizationEnabled()
|
String |
language()
|
void |
load()
|
Locale |
locale()
|
Format |
localizedDateFormatForKey(String formatString)
Returns a localized date formatter for the given key. |
Format |
localizedNumberFormatForKey(String formatString)
Returns a localized number formatter for the given key.
|
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)
|
static ERXLocalizer |
localizerForLanguage(String language)
|
static ERXLocalizer |
localizerForLanguages(NSArray languages)
|
static ERXLocalizer |
localizerForRequest(WORequest request)
|
static ERXLocalizer |
localizerForSession(Object session)
|
String |
plurifiedString(String name,
int count)
|
String |
plurifiedStringWithTemplateForKey(String key,
String name,
int count,
Object helper)
|
protected NSDictionary |
readPropertyListFromFileInFramework(String fileName,
String framework,
NSArray languages)
|
static void |
resetCache()
Resets the localizer cache. |
static void |
setAvailableLanguages(NSArray value)
|
static void |
setCurrentLocalizer(ERXLocalizer currentLocalizer)
Sets a localizer for the current thread. |
static void |
setDefaultLanguage(String value)
|
static void |
setFileNamesToWatch(NSArray value)
|
static void |
setFrameworkSearchPath(NSArray value)
|
static void |
setIsLocalizationEnabled(boolean value)
|
void |
setLocale(Locale value)
|
void |
setLocalizedDateFormatForKey(NSTimestampFormatter formatter,
String pattern)
|
void |
setLocalizedNumberFormatForKey(Format formatter,
String pattern)
|
static void |
setLocalizerForLanguage(ERXLocalizer l,
String language)
|
void |
takeValueForKey(Object value,
String key)
|
void |
takeValueForKeyPath(Object value,
String key)
|
String |
toString()
|
Object |
valueForKey(String key)
Cover method that calls localizedStringForKey . |
Object |
valueForKeyPath(String key)
|
log
protected static final ERXLogger log
createdKeysLog
protected static final ERXLogger createdKeysLog
LocalizationDidResetNotification
public static final String LocalizationDidResetNotification
- See Also:
- Constant Field Values
cache
protected NSMutableDictionary cache
_dateFormatters
protected Hashtable _dateFormatters
_numberFormatters
protected Hashtable _numberFormatters
language
protected String language
locale
protected Locale locale
ERXLocalizer
public ERXLocalizer(String aLanguage)
initialize
public static void initialize()
isLocalizationEnabled
public static boolean isLocalizationEnabled()
setIsLocalizationEnabled
public static void setIsLocalizationEnabled(boolean value)
fakeSessionForLanguage
public static NSDictionary fakeSessionForLanguage(String language)
fakeSessionForSession
public static NSDictionary fakeSessionForSession(Object session)
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.
- 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.
- Returns:
- localizer for the default language.
localizerForSession
public static ERXLocalizer localizerForSession(Object session)
localizerForRequest
public static ERXLocalizer localizerForRequest(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.
localizerForLanguages
public static ERXLocalizer localizerForLanguages(NSArray languages)
localizerForLanguage
public static ERXLocalizer localizerForLanguage(String language)
defaultLanguage
public static String defaultLanguage()
setDefaultLanguage
public static void setDefaultLanguage(String value)
fileNamesToWatch
public static NSArray fileNamesToWatch()
setFileNamesToWatch
public static void setFileNamesToWatch(NSArray value)
availableLanguages
public static NSArray availableLanguages()
setAvailableLanguages
public static void setAvailableLanguages(NSArray value)
frameworkSearchPath
public static NSArray frameworkSearchPath()
setFrameworkSearchPath
public static void setFrameworkSearchPath(NSArray value)
createLocalizerForLanguage
protected static ERXLocalizer createLocalizerForLanguage(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 forpluralForm
- denotes if the language supports the plural form
- Returns:
- a localizer for the given language
setLocalizerForLanguage
public static void setLocalizerForLanguage(ERXLocalizer l,
String language)
load
public void load()
readPropertyListFromFileInFramework
protected NSDictionary readPropertyListFromFileInFramework(String fileName,
String framework,
NSArray languages)
valueForKey
public Object valueForKey(String key)
- Cover method that calls
localizedStringForKey
.
- Specified by:
valueForKey
in interface NSKeyValueCoding
- Parameters:
key
- to resolve a localized varient of
- Returns:
- localized string for the given key
valueForKeyPath
public Object valueForKeyPath(String key)
- Specified by:
valueForKeyPath
in interface NSKeyValueCodingAdditions
takeValueForKey
public void takeValueForKey(Object value,
String key)
- Specified by:
takeValueForKey
in interface NSKeyValueCoding
takeValueForKeyPath
public void takeValueForKeyPath(Object value,
String key)
- Specified by:
takeValueForKeyPath
in interface NSKeyValueCodingAdditions
language
public String language()
createdKeys
public NSDictionary createdKeys()
localizedValueForKeyWithDefault
public Object localizedValueForKeyWithDefault(String key)
localizedValueForKey
public Object localizedValueForKey(String key)
localizedStringForKeyWithDefault
public String localizedStringForKeyWithDefault(String key)
localizedStringForKey
public String localizedStringForKey(String key)
localizedTemplateStringForKeyWithObject
public String localizedTemplateStringForKeyWithObject(String key,
Object o1)
localizedTemplateStringForKeyWithObjectOtherObject
public String localizedTemplateStringForKeyWithObjectOtherObject(String key,
Object o1,
Object o2)
plurifiedStringWithTemplateForKey
public String plurifiedStringWithTemplateForKey(String key,
String name,
int count,
Object helper)
plurifiedString
public String plurifiedString(String name,
int count)
toString
public String toString()
localizedDateFormatForKey
public Format localizedDateFormatForKey(String formatString)
- Returns a localized date formatter for the given key.
- Parameters:
formatString
-
- Returns:
localizedNumberFormatForKey
public 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
setLocalizedNumberFormatForKey
public void setLocalizedNumberFormatForKey(Format formatter,
String pattern)
- Parameters:
formatter
- pattern
-
- Returns:
locale
public Locale locale()
- Returns:
setLocale
public void setLocale(Locale value)
setLocalizedDateFormatForKey
public void setLocalizedDateFormatForKey(NSTimestampFormatter formatter,
String pattern)
- Parameters:
formatter
- pattern
-
Copyright © 2002 – 2004 Project Wonder.