Example usage for java.util Locale forLanguageTag

List of usage examples for java.util Locale forLanguageTag

Introduction

In this page you can find the example usage for java.util Locale forLanguageTag.

Prototype

public static Locale forLanguageTag(String languageTag) 

Source Link

Document

Returns a locale for the specified IETF BCP 47 language tag string.

Usage

From source file:com.opencsv.bean.BeanFieldDate.java

/**
 * @return A {@link java.text.SimpleDateFormat} primed with the proper
 *   format string and a locale, if one has been set.
 *///from  w w  w  .j a v  a2  s. com
private SimpleDateFormat getFormat() {
    SimpleDateFormat sdf;
    if (StringUtils.isNotEmpty(locale)) {
        Locale l = Locale.forLanguageTag(locale);
        sdf = new SimpleDateFormat(formatString, l);
    } else {
        sdf = new SimpleDateFormat(formatString);
    }
    return sdf;
}

From source file:org.obiba.mica.micaConfig.rest.DataAccessResource.java

@GET
@Path("/pdf")
public FileResource getDataAccessPdf(@QueryParam("lang") String lang) {
    Optional<DataAccessForm> d = dataAccessFormService.find();

    if (!d.isPresent())
        throw NoSuchDataAccessFormException.withDefaultMessage();

    DataAccessForm dataAccessForm = d.get();
    Locale locale = Locale
            .forLanguageTag(!Strings.isNullOrEmpty(lang) ? Locale.forLanguageTag(lang).toLanguageTag()
                    : LanguageTag.UNDETERMINED);

    if (!dataAccessForm.getPdfTemplates().containsKey(locale))
        throw NoSuchDataAccessFormException.withDefaultMessage();

    fileResource.setAttachment(dataAccessForm.getPdfTemplates().get(locale));

    return fileResource;
}

From source file:uk.co.grahamcox.yui.LanguageModuleBuilder.java

/**
 * Get the module file requested// w  w  w .  j a  v a2s .co  m
 * @param group the group of the module
 * @param file the file of the module
 * @return the contents of the module
 * @throws java.io.IOException if an error occurs loading module content
 */
public String getModuleFile(String group, String file, String language) throws IOException {
    if (groups.containsKey(group)) {
        ModuleGroup moduleGroup = groups.get(group);
        Module module = moduleGroup.findModule(file);
        if (module != null) {
            StringBuilder moduleOutput = new StringBuilder();
            moduleOutput.append("YUI.add('lang/").append(module.getName());
            if (language != null && !language.isEmpty()) {
                moduleOutput.append("_").append(language);
            }
            moduleOutput.append("', function (Y) {\n");
            moduleOutput.append("Y.Intl.add(\n");
            moduleOutput.append("'").append(module.getName()).append("',\n");
            moduleOutput.append("'").append(language).append("',\n");
            moduleOutput.append("{\n");

            String resourceKey = module.getMessagesFile().toString();
            Locale locale;
            if (language == null || language.isEmpty()) {
                locale = Locale.getDefault();
            } else {
                locale = Locale.forLanguageTag(language);
            }

            ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceKey, locale,
                    new LanguageControl());
            Enumeration<String> keys = resourceBundle.getKeys();
            boolean isFirst = true;
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                String value = resourceBundle.getString(key);
                if (!isFirst) {
                    moduleOutput.append(",");
                }
                isFirst = false;
                moduleOutput.append("'").append(key).append("': '").append(value).append("'");
            }

            moduleOutput.append("});\n");
            moduleOutput.append("}, '").append(module.getVersion()).append("');");
            return moduleOutput.toString();
        } else {
            throw new UnknownModuleException(group, file);
        }
    } else {
        throw new UnknownGroupException(group);
    }

}

From source file:org.owasp.webgoat.service.LabelService.java

/**
 * Fetches labels for given language/*from  w w w .  ja v  a2s .c om*/
 * If no language is provided, the language is determined from the request headers
 * Otherwise, fall back to default language
 *
 * @param lang the language to fetch labels for (optional)
 * @return a map of labels
 * @throws Exception
 */
@GetMapping(path = URL_LABELS_MVC, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Map<String, String>> fetchLabels(
        @RequestParam(value = "lang", required = false) String lang, HttpServletRequest request) {
    Locale locale;
    if (StringUtils.isEmpty(lang)) {
        log.debug("No language provided, determining from request headers");
        locale = request.getLocale();
        if (locale != null) {
            log.debug("Locale set to {}", locale);
        }
    } else {
        locale = Locale.forLanguageTag(lang);
        log.debug("Language provided: {} leads to Locale: {}", lang, locale);
    }
    return new ResponseEntity<>(labelProvider.getLabels(locale), HttpStatus.OK);
}

From source file:com.boyuanitsm.fort.service.MailService.java

@Async
public void sendActivationEmail(User user, String baseUrl) {
    log.debug("Sending activation e-mail to '{}'", user.getEmail());
    Locale locale = Locale.forLanguageTag(user.getLangKey());
    Context context = new Context(locale);
    context.setVariable(USER, user);//from   w w  w  .jav a 2  s .c  o  m
    context.setVariable(BASE_URL, baseUrl);
    String content = templateEngine.process("activationEmail", context);
    String subject = messageSource.getMessage("email.activation.title", null, locale);
    sendEmail(user.getEmail(), subject, content, false, true);
}

From source file:org.agatom.springatom.webmvc.controllers.SVLocaleController.java

@Override
public Set<Locale> getAvailableLocales() {
    final String[] array = this.supportedLocales.split(REGEX);
    final Set<Locale> locales = Sets.newHashSet();

    for (final String locale : array) {
        final Locale loc = Locale.forLanguageTag(locale);
        locales.add(loc);//w w  w . j  a  v a  2s  .c o  m
    }
    return locales;
}

From source file:org.haiku.haikudepotserver.dataobjects.NaturalLanguage.java

public Locale toLocale() {
    return Locale.forLanguageTag(getCode());
}

From source file:nu.yona.server.ThymeleafConfigurationTest.java

@Test
public void smsTemplateEngine_processBuddyInvitationDutch_dutchTemplateFoundAndExpanded() {
    String requestingUserFirstName = "john";
    String emailAddress = "a@b.c";

    String result = buildSms(Optional.of(Locale.forLanguageTag("nl-NL")), requestingUserFirstName,
            emailAddress);// w w w . java2s  . c om

    assertThat(result, equalTo("john heeft je uitgenodigd voor Yona. Zie je mail op a@b.c!"));
}

From source file:it.cineca.pst.huborcid.service.MailService.java

@Async
public void sendActivationEmail(User user, String baseUrl) {
    log.debug("Sending activation e-mail to '{}'", user.getEmail());
    Locale locale = Locale.forLanguageTag(user.getLangKey());
    Context context = new Context(locale);
    context.setVariable("user", user);
    context.setVariable("baseUrl", baseUrl);
    String content = templateEngine.process("activationEmail", context);
    String subject = messageSource.getMessage("email.activation.title", null, locale);
    sendEmail(user.getEmail(), subject, content, false, true);
}

From source file:com.github.aynu.mosir.core.standard.util.PropertiesHelper.java

/**
 * ????/* w w  w. j  a  va  2 s  . c om*/
 * @param baseName ??
 * @return ??
 */
private PropertiesConfiguration getPropertiesConfiguration(final String baseName) {
    final String languageTag = String.format("%s-%s", SystemUtils.USER_LANGUAGE, SystemUtils.USER_COUNTRY);
    final Control control = Control.getControl(Control.FORMAT_DEFAULT);
    final Collection<Locale> locales = control.getCandidateLocales("messages",
            Locale.forLanguageTag(languageTag));
    final StringBuilder builder = new StringBuilder();
    for (final Locale locale : locales) {
        final String bundleName = control.toBundleName(baseName, locale);
        final String resourceName = control.toResourceName(bundleName, "properties");
        final URL url = getClass().getResource("/" + resourceName);
        // final URL url = ClassLoader.getSystemResource(resourceName);
        if (url != null) {
            if (builder.length() > 0) {
                LOG.debug("Resource could not be found ({}).", builder.toString());
            }
            LOG.debug("Resource could be found ({}).", resourceName);
            try {
                return new PropertiesConfiguration(url);
            } catch (final ConfigurationException e) {
                throw new RuntimeException(e);
            }
        }
        builder.append(" " + resourceName);
    }
    throw new RuntimeException(String.format("PROPERTY is NOT_FOUND. [baseName=%s]", baseName));
}