Java源码示例:org.apache.wicket.Localizer
示例1
public static String getString(String key, final Locale loc, String... _params) {
if (!exists()) {
ThreadContext.setApplication(org.apache.wicket.Application.get(appName));
}
String[] params = _params;
if ((params == null || params.length == 0) && STRINGS_WITH_APP.contains(key)) {
params = new String[]{getApplicationName()};
}
Localizer l = get().getResourceSettings().getLocalizer();
String value = l.getStringIgnoreSettings(key, null, null, loc, null, "[Missing]");
if (params != null && params.length > 0) {
final MessageFormat format = new MessageFormat(value, loc);
value = format.format(params);
}
if (RuntimeConfigurationType.DEVELOPMENT == get().getConfigurationType()) {
value += String.format(" [%s]", key);
}
return value;
}
示例2
/**
*
* @param localizer
* @param key
* @param component
* @return string
*/
private String getString(Localizer localizer, String key, Component component)
{
triedKeys.add(key);
// Note: It is important that the default value of "" is
// provided to getString() not to throw a MissingResourceException or to
// return a default string like "[Warning: String ..."
return localizer.getString(key, component, "");
}
示例3
/**
* @see org.apache.wicket.validation.IErrorMessageSource#getMessage(String, java.util.Map)
*/
@Override
public String getMessage(String key, Map<String, Object> vars)
{
final FormComponent<T> formComponent = FormComponent.this;
// Use the following log4j config for detailed logging on the property resolution
// process
// log4j.logger.org.apache.wicket.resource.loader=DEBUG
// log4j.logger.org.apache.wicket.Localizer=DEBUG
final Localizer localizer = formComponent.getLocalizer();
// retrieve prefix that will be used to construct message keys
String prefix = formComponent.getValidatorKeyPrefix();
String message;
// first try the full form of key [form-component-id].[prefix].[key]
String resource = getId() + "." + prefix(prefix, key);
message = getString(localizer, resource, formComponent);
// if not found, try a more general form (without prefix)
// [form-component-id].[key]
if (Strings.isEmpty(message) && Strings.isEmpty(prefix))
{
resource = getId() + "." + key;
message = getString(localizer, resource, formComponent);
}
// If not found try a more general form [prefix].[key]
if (Strings.isEmpty(message))
{
resource = prefix(prefix, key);
message = getString(localizer, resource, formComponent);
}
// If not found try the most general form [key]
if (Strings.isEmpty(message))
{
// Try a variation of the resource key
message = getString(localizer, key, formComponent);
}
// convert empty string to null in case our default value of "" was
// returned from localizer
if (Strings.isEmpty(message))
{
message = null;
}
else
{
message = substitute(message, addDefaultVars(vars));
}
return message;
}
示例4
@Provides
public Localizer getLocalizer(WebApplication application)
{
return application.getResourceSettings().getLocalizer();
}