java.lang.Object
er.extensions.ERXLocalizer
- Direct Known Subclasses:
- ERXNonPluralFormLocalizer
- public class ERXLocalizer
- extends java.lang.Object
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
|
Method Summary |
private java.lang.String |
_plurify(java.lang.String s,
int howMany)
|
private java.lang.String |
_singularify(java.lang.String value)
|
static NSArray |
availableLanguages()
|
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()
|
static ERXLocalizer |
defaultLocalizer()
Gets the localizer for the default language. |
static NSDictionary |
fakeSessionForLanguage(java.lang.String language)
|
static NSDictionary |
fakeSessionForSession(java.lang.Object session)
|
static NSArray |
fileNamesToWatch()
|
static NSArray |
frameworkSearchPath()
|
static void |
initialize()
|
static boolean |
isLocalizationEnabled()
|
java.lang.String |
language()
|
void |
load()
|
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)
|
java.lang.Object |
localizedValueForKeyWithDefault(java.lang.String key)
|
static ERXLocalizer |
localizerForLanguage(java.lang.String language)
|
static ERXLocalizer |
localizerForLanguages(NSArray languages)
|
static ERXLocalizer |
localizerForSession(java.lang.Object session)
|
java.lang.String |
plurifiedString(java.lang.String name,
int count)
|
java.lang.String |
plurifiedStringWithTemplateForKey(java.lang.String key,
java.lang.String name,
int count,
java.lang.Object helper)
|
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(java.lang.String value)
|
static void |
setFileNamesToWatch(NSArray value)
|
static void |
setFrameworkSearchPath(NSArray value)
|
static void |
setIsLocalizationEnabled(boolean value)
|
static void |
setLocalizerForLanguage(ERXLocalizer l,
java.lang.String 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()
Convert this Object to a human-readable String. |
java.lang.Object |
valueForKey(java.lang.String key)
Cover method that calls localizedStringForKey. |
java.lang.Object |
valueForKeyPath(java.lang.String key)
|
log
protected static final ERXLogger log
createdKeysLog
protected static final ERXLogger createdKeysLog
isLocalizationEnabled
private static boolean isLocalizationEnabled
isInitialized
private static boolean isInitialized
LocalizationDidResetNotification
public static final java.lang.String LocalizationDidResetNotification
- See Also:
- Constant Field Values
observer
private static ERXLocalizer.Observer observer
monitoredFiles
private static NSMutableArray monitoredFiles
fileNamesToWatch
static NSArray fileNamesToWatch
frameworkSearchPath
static NSArray frameworkSearchPath
availableLanguages
static NSArray availableLanguages
defaultLanguage
static java.lang.String defaultLanguage
localizers
static NSMutableDictionary localizers
cache
protected NSMutableDictionary cache
createdKeys
private NSMutableDictionary createdKeys
NOT_FOUND
private java.lang.String NOT_FOUND
_languagesWithoutPluralForm
private static NSArray _languagesWithoutPluralForm
language
java.lang.String language
ERXLocalizer
public ERXLocalizer(java.lang.String aLanguage)
initialize
public static void initialize()
isLocalizationEnabled
public static boolean isLocalizationEnabled()
setIsLocalizationEnabled
public static void setIsLocalizationEnabled(boolean value)
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(java.lang.String language)
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.
setLocalizerForLanguage
public static void setLocalizerForLanguage(ERXLocalizer l,
java.lang.String language)
valueForKey
public java.lang.Object valueForKey(java.lang.String key)
- Cover method that calls
localizedStringForKey.
valueForKeyPath
public java.lang.Object valueForKeyPath(java.lang.String key)
takeValueForKey
public void takeValueForKey(java.lang.Object value,
java.lang.String key)
takeValueForKeyPath
public void takeValueForKeyPath(java.lang.Object value,
java.lang.String key)
load
public void load()
fakeSessionForLanguage
public static NSDictionary fakeSessionForLanguage(java.lang.String language)
fakeSessionForSession
public static NSDictionary fakeSessionForSession(java.lang.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.
setCurrentLocalizer
public static void setCurrentLocalizer(ERXLocalizer currentLocalizer)
- Sets a localizer for the current thread. This is accomplished
by using the object ERXThreadStorage
defaultLocalizer
public static ERXLocalizer defaultLocalizer()
- Gets the localizer for the default language.
localizerForSession
public static ERXLocalizer localizerForSession(java.lang.Object session)
language
public java.lang.String language()
createdKeys
public NSDictionary createdKeys()
localizedValueForKeyWithDefault
public java.lang.Object localizedValueForKeyWithDefault(java.lang.String key)
localizedValueForKey
public java.lang.Object localizedValueForKey(java.lang.String key)
localizedStringForKeyWithDefault
public java.lang.String localizedStringForKeyWithDefault(java.lang.String key)
localizedStringForKey
public java.lang.String localizedStringForKey(java.lang.String 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
private java.lang.String _plurify(java.lang.String s,
int howMany)
_singularify
private java.lang.String _singularify(java.lang.String value)
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)
toString
public java.lang.String toString()
- Description copied from class:
java.lang.Object
- Convert this Object to a human-readable String.
There are no limits placed on how long this String
should be or what it should contain. We suggest you
make it as intuitive as possible to be able to place
it into System.out.println() 55
and such.
It is typical, but not required, to ensure that this method
never completes abruptly with a java.lang.RuntimeException.
This method will be called when performing string
concatenation with this object. If the result is
null, string concatenation will instead
use "null".
The default implementation returns
getClass().getName() + "@" +
Integer.toHexString(hashCode()).
defaultLanguage
public static java.lang.String defaultLanguage()
setDefaultLanguage
public static void setDefaultLanguage(java.lang.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)