org.springframework.util.StringUtils#parseLocaleString ( )源码实例Demo

下面列出了org.springframework.util.StringUtils#parseLocaleString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public void setAsText(String text) throws IllegalArgumentException {
	Assert.hasText(text, "'text' must not be empty");
	String name = text.trim();

	int separator = name.indexOf(BASE_NAME_SEPARATOR);
	if (separator == -1) {
		setValue(ResourceBundle.getBundle(name));
	}
	else {
		// The name potentially contains locale information
		String baseName = name.substring(0, separator);
		if (!StringUtils.hasText(baseName)) {
			throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'");
		}
		String localeString = name.substring(separator + 1);
		Locale locale = StringUtils.parseLocaleString(localeString);
		setValue(locale != null ? ResourceBundle.getBundle(baseName, locale) : ResourceBundle.getBundle(baseName));
	}
}
 
源代码2 项目: lams   文件: ResourceBundleEditor.java
@Override
public void setAsText(String text) throws IllegalArgumentException {
	Assert.hasText(text, "'text' must not be empty");
	String name = text.trim();

	int separator = name.indexOf(BASE_NAME_SEPARATOR);
	if (separator == -1) {
		setValue(ResourceBundle.getBundle(name));
	}
	else {
		// The name potentially contains locale information
		String baseName = name.substring(0, separator);
		if (!StringUtils.hasText(baseName)) {
			throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'");
		}
		String localeString = name.substring(separator + 1);
		Locale locale = StringUtils.parseLocaleString(localeString);
		setValue(locale != null ? ResourceBundle.getBundle(baseName, locale) : ResourceBundle.getBundle(baseName));
	}
}
 
源代码3 项目: blog_demos   文件: ResourceBundleEditor.java
@Override
public void setAsText(String text) throws IllegalArgumentException {
	Assert.hasText(text, "'text' must not be empty");
	ResourceBundle bundle;
	String rawBaseName = text.trim();
	int indexOfBaseNameSeparator = rawBaseName.indexOf(BASE_NAME_SEPARATOR);
	if (indexOfBaseNameSeparator == -1) {
		bundle = ResourceBundle.getBundle(rawBaseName);
	} else {
		// it potentially has locale information
		String baseName = rawBaseName.substring(0, indexOfBaseNameSeparator);
		if (!StringUtils.hasText(baseName)) {
			throw new IllegalArgumentException("Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'.");
		}
		String localeString = rawBaseName.substring(indexOfBaseNameSeparator + 1);
		Locale locale = StringUtils.parseLocaleString(localeString);
		bundle = (StringUtils.hasText(localeString))
				? ResourceBundle.getBundle(baseName, locale)
				: ResourceBundle.getBundle(baseName);
	}
	setValue(bundle);
}
 
@Override
public void setAsText(String text) throws IllegalArgumentException {
	Assert.hasText(text, "'text' must not be empty");
	ResourceBundle bundle;
	String rawBaseName = text.trim();
	int indexOfBaseNameSeparator = rawBaseName.indexOf(BASE_NAME_SEPARATOR);
	if (indexOfBaseNameSeparator == -1) {
		bundle = ResourceBundle.getBundle(rawBaseName);
	}
	else {
		// it potentially has locale information
		String baseName = rawBaseName.substring(0, indexOfBaseNameSeparator);
		if (!StringUtils.hasText(baseName)) {
			throw new IllegalArgumentException("Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'.");
		}
		String localeString = rawBaseName.substring(indexOfBaseNameSeparator + 1);
		Locale locale = StringUtils.parseLocaleString(localeString);
		bundle = (StringUtils.hasText(localeString))
				? ResourceBundle.getBundle(baseName, locale)
				: ResourceBundle.getBundle(baseName);
	}
	setValue(bundle);
}
 
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                        "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
                (locale != null ? locale: determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
                (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
源代码6 项目: stategen   文件: LocaleUtil.java
public static Locale getSupportLocale(String locale){
    Locale destLocale=null;
    if (StringUtil.isNotBlank(locale) ){
        destLocale=StringUtils.parseLocaleString(locale);
    }
    return getSupportLocale(destLocale); 
}
 
源代码7 项目: klask-io   文件: AngularCookieLocaleResolver.java
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            (locale != null ? locale: determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
源代码8 项目: expper   文件: AngularCookieLocaleResolver.java
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            (locale != null ? locale: determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
源代码9 项目: jhipster   文件: AngularCookieLocaleResolver.java
private void parseAngularCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, QUOTE, "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = !"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null;
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            locale != null ? locale : determineDefaultLocale(request));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            timeZone != null ? timeZone : determineDefaultTimeZone(request));
    }
}
 
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            (locale != null ? locale: determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            (locale != null ? locale: determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            (locale != null ? locale: determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            (locale != null ? locale: determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
源代码14 项目: gpmr   文件: AngularCookieLocaleResolver.java
private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = (!"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale +
                    "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
            (locale != null ? locale : determineDefaultLocale(request)));

        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
            (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
}
 
源代码15 项目: alfresco-repository   文件: MailActionExecuter.java
/**
 * Gets the specified user's preferred locale, if available.
 * 
 * @param user the username of the user whose locale is sought.
 * @return the preferred locale for that user, if available, else <tt>null</tt>. The result would be <tt>null</tt>
 *         e.g. if the user does not exist in the system.
 */
private Locale getLocaleForUser(final String user)
{
    Locale locale = null;
    String localeString = null;
    
    // get primary tenant for the specified user.
    //
    // This can have one of (at least) 3 values currently:
    // 1. In single-tenant (community/enterprise) this will be the empty string.
    // 2. In the cloud, for a username such as this: [email protected]:
    //    2A. If the acme.com tenant exists in the system, the primary domain is "acme.com"
    //    2B. Id the acme.xom tenant does not exist in the system, the primary domain is null.
    String domain = tenantService.getPrimaryDomain(user);
    
    if (domain != null) 
    { 
        // If the domain is not null, then the user exists in the system and we may get a preferred locale.
        localeString = TenantUtil.runAsSystemTenant(new TenantRunAsWork<String>()
        {
            public String doWork() throws Exception
            {
                return (String) preferenceService.getPreference(user, "locale");
            }
        }, domain);
    }
    else
    {
        // If the domain is null, then the beahviour here varies depending on whether it's a single tenant or multi-tenant cloud.
        if (personExists(user))
        {
            localeString = AuthenticationUtil.runAsSystem(new RunAsWork<String>()
            {
                public String doWork() throws Exception 
                {
                    return (String) preferenceService.getPreference(user, "locale");
                };
            }); 
        }
        // else leave it as null - there's no tenant, no user for that username, so we can't get a preferred locale.
    }
    
    if (localeString != null)
    {
        locale = StringUtils.parseLocaleString(localeString);
    }

    return locale;
}
 
源代码16 项目: lams   文件: StringToLocaleConverter.java
@Override
public Locale convert(String source) {
	return StringUtils.parseLocaleString(source);
}
 
@Override
public Locale convert(String source) {
	return StringUtils.parseLocaleString(source);
}
 
源代码18 项目: lams   文件: CookieLocaleResolver.java
/**
 * Parse the given locale value coming from an incoming cookie.
 * <p>The default implementation calls {@link StringUtils#parseLocaleString(String)}
 * or JDK 7's {@link Locale#forLanguageTag(String)}, depending on the
 * {@link #setLanguageTagCompliant "languageTagCompliant"} configuration property.
 * @param locale the locale value to parse
 * @return the corresponding {@code Locale} instance
 * @since 4.3
 */
@UsesJava7
protected Locale parseLocaleValue(String locale) {
	return (isLanguageTagCompliant() ? Locale.forLanguageTag(locale) : StringUtils.parseLocaleString(locale));
}
 
源代码19 项目: lams   文件: LocaleChangeInterceptor.java
/**
 * Parse the given locale value as coming from a request parameter.
 * <p>The default implementation calls {@link StringUtils#parseLocaleString(String)}
 * or JDK 7's {@link Locale#forLanguageTag(String)}, depending on the
 * {@link #setLanguageTagCompliant "languageTagCompliant"} configuration property.
 * @param locale the locale value to parse
 * @return the corresponding {@code Locale} instance
 * @since 4.3
 */
@UsesJava7
protected Locale parseLocaleValue(String locale) {
	return (isLanguageTagCompliant() ? Locale.forLanguageTag(locale) : StringUtils.parseLocaleString(locale));
}
 
源代码20 项目: lams   文件: Jackson2ObjectMapperBuilder.java
/**
 * Override the default {@link Locale} to use for formatting.
 * Default value used is {@link Locale#getDefault()}.
 * @param localeString the locale ID as a String representation
 * @since 4.1.5
 */
public Jackson2ObjectMapperBuilder locale(String localeString) {
	this.locale = StringUtils.parseLocaleString(localeString);
	return this;
}