Example usage for java.util Locale toLanguageTag

List of usage examples for java.util Locale toLanguageTag

Introduction

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

Prototype

public String toLanguageTag() 

Source Link

Document

Returns a well-formed IETF BCP 47 language tag representing this locale.

Usage

From source file:org.diorite.cfg.messages.MessageLoader.java

private static InputStreamReader getInputStreamReader(final String prefix, final Class<?> clazz,
        final String resourcesFolder, final Locale locale) {
    final InputStreamReader inputStreamReader = inputStreamToReader(
            clazz.getResourceAsStream(resourcesFolder + prefix + locale.toLanguageTag() + ".yml"));
    if (inputStreamReader == null) {
        return inputStreamToReader(
                clazz.getResourceAsStream(resourcesFolder + locale.toLanguageTag() + ".yml"));
    }/*from   w w w.j  a  v  a2  s . co  m*/
    return inputStreamReader;
}

From source file:password.pwm.util.LocaleHelper.java

public static String debugLabel(final Locale locale) {
    if (locale == null || PwmConstants.DEFAULT_LOCALE.equals(locale)) {
        return "default";
    }// w w  w  . j av a2  s  .  co m
    return locale.toLanguageTag();
}

From source file:org.daxplore.presenter.server.servlets.AdminUploadServlet.java

private static void unzipAll(PersistenceManager pm, String prefix, byte[] fileData)
        throws BadRequestException, InternalServerException {
    LinkedHashMap<String, byte[]> fileMap = new LinkedHashMap<>();

    try (ZipInputStream zipIn = ServerTools.getAsZipInputStream(fileData)) {
        // Unzip all the files and put them in a map
        try {//from   w  w w .j  ava2  s . co  m
            ZipEntry entry;
            while ((entry = zipIn.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    byte[] data = IOUtils.toByteArray(zipIn);
                    fileMap.put(entry.getName(), data);
                }
            }
        } catch (IOException e) {
            throw new BadRequestException("Error when reading uploaded file (invalid file?)", e);
        }

        // Read the file manifest to get metadata about the file
        if (!fileMap.containsKey("manifest.xml")) {
            throw new BadRequestException("No manifest.xml found in uploaded file");
        }

        try (InputStream manifestStream = new ByteArrayInputStream(fileMap.get("manifest.xml"))) {
            UploadFileManifest manifest = new UploadFileManifest(manifestStream);

            // Check manifest content and make sure that the file is in proper order

            if (!ServerTools.isSupportedUploadFileVersion(manifest.getVersionMajor(),
                    manifest.getVersionMinor())) {
                throw new BadRequestException("Unsupported file version");
            }

            Locale unsupportedLocale = null;
            for (Locale locale : manifest.getSupportedLocales()) {
                if (!ServerTools.isSupportedLocale(locale)) {
                    unsupportedLocale = locale;
                    break;
                }
            }
            if (unsupportedLocale != null) {
                //TODO move exception into the for-loop above if Eclipse/Java stops warning about it
                throw new BadRequestException("Unsupported language: " + unsupportedLocale.toLanguageTag());
            }
            Set<String> missingUploadFiles = SharedResourceTools.findMissingUploadFiles(fileMap.keySet(),
                    manifest.getSupportedLocales());
            if (!missingUploadFiles.isEmpty()) {
                throw new BadRequestException("Uploaded doesn't contain required files: "
                        + SharedTools.join(missingUploadFiles, ", "));
            }

            Set<String> unwantedUploadFiles = SharedResourceTools.findUnwantedUploadFiles(fileMap.keySet(),
                    manifest.getSupportedLocales());
            if (!unwantedUploadFiles.isEmpty()) {
                throw new BadRequestException(
                        "Uploaded file contains extra files: " + SharedTools.join(unwantedUploadFiles, ", "));
            }

            // Purge all existing data that uses this prefix, but save gaID
            String gaID = SettingItemStore.getProperty(pm, prefix, "adminpanel", "gaID");
            String statStoreKey = prefix + "#adminpanel/gaID";
            String deleteResult = DeleteData.deleteForPrefix(pm, prefix);
            pm.makePersistent(new SettingItemStore(statStoreKey, gaID));
            logger.log(Level.INFO, deleteResult);

            // Since we just deleted the prefix and all it's data, we have to add it
            // again
            pm.makePersistent(new PrefixStore(prefix));
            logger.log(Level.INFO, "Added prefix to system: '" + prefix + "'");

            LocaleStore localeStore = new LocaleStore(prefix, manifest.getSupportedLocales(),
                    manifest.getDefaultLocale());
            pm.makePersistent(localeStore);
            logger.log(Level.INFO, "Added locale settings for prefix '" + prefix + "'");

            for (String fileName : fileMap.keySet()) {
                String storeName = prefix + "#" + fileName;
                if (fileName.startsWith("properties/")) {
                    unpackPropertyFile(pm, storeName, fileMap.get(fileName));
                } else if (fileName.startsWith("data/")) {
                    unpackStatisticalDataFile(pm, prefix, fileMap.get(fileName));
                } else if (fileName.startsWith("meta/")) {
                    unpackStaticFile(pm, storeName, fileMap.get(fileName));
                }
            }
        } catch (BadRequestException e) {
            throw e;
        }
    } catch (IOException e) {
        throw new InternalServerException("Failed to close unzip file", e);
    }
}

From source file:org.wso2.carbon.uuf.api.config.I18nResources.java

/**
 * Returns the i18n resource for the given language.
 *
 * @param locale Locale of the i18n resource
 * @return i18n resource/*from   www . j a  v  a  2s  .  co m*/
 */
public Properties getI18nResource(Locale locale) {
    return i18nResources.get(locale.toLanguageTag());
}

From source file:org.efaps.esjp.common.format.LeftFormatFcty.java

@Override
public Format getFormat(final String _name, final String _arguments, final Locale _locale) {
    Format ret;//from w  w w  .  j  av  a2  s .co m
    final String key = _locale.toLanguageTag() + "_" + _arguments;
    if (LeftFormatFcty.FORMATS.containsKey(key)) {
        ret = LeftFormatFcty.FORMATS.get(key);
    } else {
        ret = new LeftFormat(_arguments);
        LeftFormatFcty.FORMATS.put(key, ret);
    }
    return ret;
}

From source file:com.btmatthews.atlas.core.domain.i18n.Localized.java

/**
 * Convert the localized value to a string.
 *
 * @return The string.//from w  ww  .  ja  v a2 s . co  m
 */
@Override
public String toString() {
    final StringBuilder builder = new StringBuilder("Localized[{");
    boolean first = true;
    final List<Locale> keys = natural().onResultOf(new Function<Locale, Comparable>() {
        @Override
        public Comparable apply(Locale o) {
            return o.toLanguageTag();
        }
    }).sortedCopy(values.keySet());
    for (final Locale locale : keys) {
        if (first) {
            first = false;
        } else {
            builder.append(", ");
        }
        builder.append(locale.toLanguageTag());
        builder.append('=');
        builder.append(values.get(locale));
    }
    builder.append("}]");
    return builder.toString();
}

From source file:org.smigo.message.MessageHandler.java

public int addMessage(MessageAdd message, AuthenticatedUser user, Locale locale) {
    message.setSubmitterUserId(user.getId());
    message.setLocale(locale.toLanguageTag());
    return messageDao.addMessage(message);
}

From source file:ru.jts_dev.gameserver.parser.html.HtmlRepository.java

private String readHtml(Locale language, String htmlName) {
    Path htmlPath = htmlDir.resolve(language.toLanguageTag()).resolve(htmlName);
    if (!Files.exists(htmlPath)) {
        throw new IllegalArgumentException("Can't find html: " + htmlPath);
    }/*  w w w .  j av a2 s .  c  o  m*/
    try {
        byte[] content = Files.readAllBytes(htmlPath);
        return new String(content, UTF_8);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.efaps.esjp.common.format.DateTimeFormatFcty.java

@Override
public Format getFormat(final String _name, final String _arguments, final Locale _locale) {
    Format ret;//from   www .j a  va  2 s. co  m
    final String key = _locale.toLanguageTag() + "_" + _arguments;
    if (DateTimeFormatFcty.FORMATS.containsKey(key)) {
        ret = DateTimeFormatFcty.FORMATS.get(key);
    } else {
        ret = new DateTimeFormat(_arguments, _locale);
        DateTimeFormatFcty.FORMATS.put(key, ret);
    }
    return ret;
}

From source file:com.ibm.watson.WatsonTranslate.java

public WatsonTranslate(Locale locale) {
    String bcp47Tag = locale.toLanguageTag();
    String isoLang = locale.getLanguage();

    logger.debug("BCP 47 language tag {}", bcp47Tag);
    logger.debug("ISO language tag {}", isoLang);
    // see if this is a supported language for Watson translate

    if (isoLang.equalsIgnoreCase("es")) {
        watsonLangPair = "mt-enus-eses";
    } else if (isoLang.equalsIgnoreCase("fr")) {
        watsonLangPair = "mt-enus-frfr";
    } else if (bcp47Tag.equalsIgnoreCase("pt-BR")) {
        watsonLangPair = "mt-enus-ptbr";
    }// w ww  . jav  a  2 s.  c  o  m
}