play.i18n.Lang Java Examples

The following examples show how to use play.i18n.Lang. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CalendarBinder.java    From restcommander with Apache License 2.0 6 votes vote down vote up
public Calendar bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception {
    if (value == null || value.trim().length() == 0) {
        return null;
    }
    Calendar cal = Calendar.getInstance(Lang.getLocale());
    try {
        Date date = AnnotationHelper.getDateAs(annotations, value);
        if (date != null) {
            cal.setTime(date);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(I18N.getDateFormat());
            sdf.setLenient(false);
            cal.setTime(sdf.parse(value));
        }
    } catch (ParseException e) {
        throw new IllegalArgumentException("Cannot convert [" + value + "] to a Calendar: " + e.toString());
    }

    return cal;
}
 
Example #2
Source File: AnnotationHelper.java    From restcommander with Apache License 2.0 6 votes vote down vote up
public static Tuple getLocale(String[] langs) {
    int i = 0;
    for (String l : langs) {
        String[] commaSeparatedLang = l.split(",");
        for (String lang : commaSeparatedLang) {
            if (Lang.get().equals(lang) || "*".equals(lang)) {
                Locale locale = null;
                if ("*".equals(lang)) {
                    locale = Lang.getLocale();
                }
                if (locale == null) {
                    locale = Lang.getLocale(lang);
                }
                if (locale != null) {
                    return new Tuple(i, locale);
                }
            }
        }
        i++;
    }
    return null;
}
 
Example #3
Source File: GSNUsernamePasswordAuthProvider.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Body getVerifyEmailMailingBody(final String token,
		final GSNUsernamePasswordAuthUser user, final Context ctx) {

	final boolean isSecure = getConfiguration().getBoolean(
			SETTING_KEY_VERIFICATION_LINK_SECURE);
	final String url = controllers.gsn.auth.routes.Signup.verify(token).absoluteURL(
			ctx.request(), isSecure);

	final Lang lang = Lang.preferred(ctx.request().acceptLanguages());
	final String langCode = lang.code();

	final String html = getEmailTemplate(
			"views.html.account.signup.email.verify_email", langCode, url,
			token, user.getName(), user.getEmail());
	final String text = getEmailTemplate(
			"views.txt.account.signup.email.verify_email", langCode, url,
			token, user.getName(), user.getEmail());

	return new Body(text, html);
}
 
Example #4
Source File: GSNUsernamePasswordAuthProvider.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
protected Body getPasswordResetMailingBody(final String token,
		final User user, final Context ctx) {

	final boolean isSecure = getConfiguration().getBoolean(
			SETTING_KEY_PASSWORD_RESET_LINK_SECURE);
	final String url = controllers.gsn.auth.routes.Signup.resetPassword(token).absoluteURL(
			ctx.request(), isSecure);

	final Lang lang = Lang.preferred(ctx.request().acceptLanguages());
	final String langCode = lang.code();

	final String html = getEmailTemplate(
			"views.html.account.email.password_reset", langCode, url,
			token, user.name, user.email);
	final String text = getEmailTemplate(
			"views.txt.account.email.password_reset", langCode, url, token,
			user.name, user.email);

	return new Body(text, html);
}
 
Example #5
Source File: GSNUsernamePasswordAuthProvider.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
protected Body getVerifyEmailMailingBodyAfterSignup(final String token,
		final User user, final Context ctx) {

	final boolean isSecure = getConfiguration().getBoolean(
			SETTING_KEY_VERIFICATION_LINK_SECURE);
	final String url = controllers.gsn.auth.routes.Signup.verify(token).absoluteURL(
			ctx.request(), isSecure);

	final Lang lang = Lang.preferred(ctx.request().acceptLanguages());
	final String langCode = lang.code();

	final String html = getEmailTemplate(
			"views.html.account.email.verify_email", langCode, url, token,
			user.name, user.email);
	final String text = getEmailTemplate(
			"views.txt.account.email.verify_email", langCode, url, token,
			user.name, user.email);

	return new Body(text, html);
}
 
Example #6
Source File: AnnotationHelper.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * It can be something like As(lang={"fr,de","*"}, value={"dd-MM-yyyy","MM-dd-yyyy"})
 *
 * @param annotations
 * @param value
 * @return null if it cannot be converted because there is no annotation.
 * @throws ParseException
 *
 */
public static Date getDateAs(Annotation[] annotations, String value) throws ParseException {
    // Look up for the BindAs annotation
    if (annotations == null) {
        return null;
    }
    for (Annotation annotation : annotations) {
        if (annotation.annotationType().equals(As.class)) {
            As as = (As) annotation;
            Locale locale = Lang.getLocale();
            String format = as.value()[0];
            if (!StringUtils.isEmpty(format)) {
                // This can be comma separated
                Tuple tuple = getLocale(as.lang());
                if (tuple != null) {
                    // Avoid NPE and get the last value if not specified
                    format = as.value()[tuple.index < as.value().length ? tuple.index : as.value().length - 1];
                    locale = tuple.locale;
                }
            }
            if (StringUtils.isEmpty(format)) {
                format = I18N.getDateFormat();
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
            sdf.setLenient(false);
            return sdf.parse(value);

        }
    }
    return null;
}
 
Example #7
Source File: JavaExtensions.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public static String formatCurrency(Number number, String currencyCode) {
    Currency currency = Currency.getInstance(currencyCode);
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(new Locale(Lang.get()));
    numberFormat.setCurrency(currency);
    numberFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
    String s = numberFormat.format(number);
    s = s.replace(currencyCode, I18N.getCurrencySymbol(currencyCode));
    return s;
}
 
Example #8
Source File: I18N.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public static String getDateFormat() {
    final String localizedDateFormat = Play.configuration.getProperty("date.format." + Lang.get());
    if (!StringUtils.isEmpty(localizedDateFormat)) {
        return localizedDateFormat;
    }
    final String globalDateFormat = Play.configuration.getProperty("date.format");
    if (!StringUtils.isEmpty(globalDateFormat)) {
        return globalDateFormat;
    }
    // Default value. It's completely arbitrary.
    return "yyyy-MM-dd";
}
 
Example #9
Source File: TracingFilterTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public List<Lang> acceptLanguages() {
    return null;
}
 
Example #10
Source File: JavaExtensions.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public static String format(Number number, String pattern) {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale(Lang.get()));
    return new DecimalFormat(pattern, symbols).format(number);
}
 
Example #11
Source File: JavaExtensions.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public static String format(Date date, String pattern) {
    return format(date, pattern, Lang.get());
}
 
Example #12
Source File: JavaExtensions.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public static String asdate(Long timestamp, String pattern) {
    return asdate(timestamp, pattern, Lang.get());
}
 
Example #13
Source File: Invoker.java    From restcommander with Apache License 2.0 2 votes vote down vote up
/**
 * Needs this method to do stuff *before* init() is executed.
 * The different Invocation-implementations does a lot of stuff in init()
 * and they might do it before calling super.init()
 */
protected void preInit() {
    // clear language for this request - we're resolving it later when it is needed
    Lang.clear();
}