Example usage for java.util Locale getLanguage

List of usage examples for java.util Locale getLanguage

Introduction

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

Prototype

public String getLanguage() 

Source Link

Document

Returns the language code of this Locale.

Usage

From source file:de.iew.services.impl.MessageBundleServiceImpl.java

protected String messageBundleKey(Locale locale, String basename) {
    return messageBundleKey(locale.getLanguage(), locale.getCountry(), basename);
}

From source file:com.erudika.scoold.controllers.TranslateController.java

@PostMapping("/{locale}/{index}")
public String post(@PathVariable String locale, @PathVariable String index, @RequestParam String value,
        HttpServletRequest req, Model model) {
    Locale showLocale = utils.getLangutils().getProperLocale(locale);
    if (utils.isAuthenticated(req) && showLocale != null && !"en".equals(showLocale.getLanguage())) {
        Set<String> approved = utils.getLangutils().getApprovedTransKeys(showLocale.getLanguage());
        Profile authUser = utils.getAuthUser(req);
        String langkey = langkeys.get(getIndex(index, langkeys));
        boolean isTranslated = approved.contains(langkey);
        if (!StringUtils.isBlank(value) && (!isTranslated || utils.isAdmin(authUser))) {
            Translation trans = new Translation(showLocale.getLanguage(), langkey, StringUtils.trim(value));
            trans.setCreatorid(authUser.getId());
            trans.setAuthorName(authUser.getName());
            trans.setTimestamp(System.currentTimeMillis());
            pc.create(trans);/*  w ww .  j a va 2s  .c  o  m*/
            model.addAttribute("newtranslation", trans);
        }
        if (!utils.isAjaxRequest(req)) {
            return "redirect:" + TRANSLATELINK + "/" + showLocale.getLanguage() + "/"
                    + getNextIndex(getIndex(index, langkeys), approved, langkeys);
        }
    }

    return "base";
}

From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java

/**
 * Load the properties file from the given class object. The filename from the properties file
 * is the same as the simple name from the class object and it looks at the same path as the
 * given class object. If locale is not null than the language will be added to the filename
 * from the properties file./*from   ww w  .j  a  v  a 2s . c om*/
 * 
 * @param clazz
 *            the clazz
 * @param locale
 *            the locale
 * @return the properties
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadPropertiesFromClassObject(Class<?> clazz, Locale locale) throws IOException {
    if (null == clazz) {
        throw new IllegalArgumentException("Class object must not be null!!!");
    }
    StringBuilder propertiesName = new StringBuilder();
    Properties properties = null;
    String language = null;
    String filename = null;
    String pathAndFilename = null;
    File propertiesFile = null;
    String absoluteFilename = null;
    String packagePath = PackageUtils.getPackagePathWithSlash(clazz);
    List<String> missedFiles = new ArrayList<>();
    if (null != locale) {
        propertiesName.append(clazz.getSimpleName());
        language = locale.getLanguage();
        if (null != language && !language.isEmpty()) {
            propertiesName.append("_").append(language);
        }

        String country = locale.getCountry();
        if (null != country && !country.isEmpty()) {
            propertiesName.append("_").append(country);
        }
        propertiesName.append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        URL url = ClassUtils.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }

        if (null != propertiesFile && propertiesFile.exists()) {
            properties = PropertiesUtils.loadProperties(pathAndFilename);
        } else {
            propertiesName = new StringBuilder();
            if (null != locale) {
                propertiesName.append(clazz.getSimpleName());
                language = locale.getLanguage();
                if (null != language && !language.isEmpty()) {
                    propertiesName.append("_").append(language);
                }
                propertiesName.append(FileExtension.PROPERTIES.getExtension());
                filename = propertiesName.toString().trim();
                pathAndFilename = packagePath + filename;
                url = ClassUtils.getResource(clazz, filename);

                if (url != null) {
                    absoluteFilename = url.getFile();
                } else {
                    missedFiles.add("File with filename '" + filename + "' does not exists.");
                }
                if (null != absoluteFilename) {
                    propertiesFile = new File(absoluteFilename);
                }
                if (null != propertiesFile && propertiesFile.exists()) {
                    properties = PropertiesUtils.loadProperties(pathAndFilename);
                }
            }
        }
    }

    if (null == properties) {
        propertiesName = new StringBuilder();
        propertiesName.append(clazz.getSimpleName()).append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        URL url = ClassUtils.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            properties = PropertiesUtils.loadProperties(pathAndFilename);
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }
        if (null != propertiesFile && propertiesFile.exists()) {
            properties = PropertiesUtils.loadProperties(pathAndFilename);
        }
    }
    if (properties == null) {
        for (String string : missedFiles) {
            LOGGER.info(string);
        }
    }

    return properties;
}

From source file:org.smigo.species.vernacular.VernacularHandler.java

public CrudResult addVernacular(Vernacular vernacular, AuthenticatedUser user, Locale locale) {
    Species species = speciesHandler.getSpecies(vernacular.getSpeciesId());
    List<Vernacular> currentVernaculars = vernacularDao.getVernacularBySpecies(species.getId());
    vernacular.setLanguage(locale.getLanguage());
    vernacular.setCountry(locale.getCountry());

    boolean isCreator = species.getCreator() == user.getId();
    if (isCreator || user.isModerator()) {
        int id = vernacularDao.insertVernacular(vernacular);
        return new CrudResult(id, Review.NONE);
    }/*from  www .  j a va 2 s  . c  om*/

    mailHandler.sendReviewRequest("Add vernacular", currentVernaculars, vernacular, user);
    return new CrudResult(null, Review.MODERATOR);
}

From source file:com.github.mrstampy.gameboot.messages.context.GameBootContextLoader.java

@Override
public Map<String, Map<Integer, ResponseContext>> getErrorProperties() throws Exception {
    Map<String, Map<Integer, ResponseContext>> map = new ConcurrentHashMap<>();

    Locale[] locales = Locale.getAvailableLocales();

    for (Locale locale : locales) {
        if (isNotEmpty(locale.getCountry())) {
            String suffix = "_" + locale.getLanguage() + "_" + locale.getCountry();
            addToMap(map, suffix);/*  ww  w.  ja v  a 2s . c  om*/
        }

        String suffix = "_" + locale.getLanguage();
        addToMap(map, suffix);
    }

    additionalLocales.forEach(suffix -> {
        try {
            addToMap(map, suffix);
        } catch (Exception e) {
            log.error("Unexpected exception", e);
        }
    });

    Map<Integer, ResponseContext> root = getForLocale(GameBootContextLookup.ROOT);
    if (root == null)
        throw new IllegalStateException("No error.properties file");

    map.put(GameBootContextLookup.ROOT, root);

    return map;
}

From source file:de.iew.web.view.js.RequireJSMessageBundleView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> stringObjectMap, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    response.setContentType("text/javascript; charset=utf-8");

    PrintWriter writer = response.getWriter();

    // Ja, Javascript Hlle :-)
    writer.write("define({");
    if (isRootBundle(stringObjectMap)) {
        writer.write("'root':{");
    }/*from w  w w. ja  va 2 s. c o  m*/

    Map<String, String> bundle = getBundle(stringObjectMap);
    int itemsLeft = bundle.size();
    for (Map.Entry<String, String> item : bundle.entrySet()) {
        writer.write("'");
        writer.write(item.getKey());
        writer.write("':'");
        writer.write(item.getValue());
        writer.write("'");
        itemsLeft--;

        if (itemsLeft > 0) {
            writer.write(",");
        }
    }

    if (isRootBundle(stringObjectMap)) {
        writer.write("}");

        List<Locale> availableLocales = getAvailableLocales(stringObjectMap);
        for (Locale locale : availableLocales) {
            if (!locale.equals(this.defaultLocale)) {
                writer.write(",");
                writer.write("'");
                writer.write(locale.getLanguage().toLowerCase() + "-" + locale.getCountry().toLowerCase());
                writer.write("':");
                writer.write("true");
            }
        }
    }
    writer.write("});");
}

From source file:com.properned.model.MultiLanguageProperties.java

private String getFileName(Locale locale) {
    String fileName = baseName.get();
    if (StringUtils.isNotEmpty(locale.getLanguage())) {
        fileName += "_" + locale.getLanguage();
    }//from   w ww. java2 s  .c  o m
    if (StringUtils.isNotEmpty(locale.getCountry())) {
        fileName += "_" + locale.getCountry();
    }
    fileName += ".properties";
    return fileName;
}

From source file:org.iti.agrimarket.view.UserController.java

@RequestMapping(value = { "/uprofile.htm" }, method = RequestMethod.POST)
public String updateUserProfile(@RequestParam(value = "fullName", required = true) String fullName,
        @RequestParam(value = "mobile", required = true) String mobil,
        @RequestParam(value = "governerate", required = true) String governerate,
        @RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,
        Locale locale, Model model) {
    System.out.println("hhhhhhhhhhhhhhhhhhhhhh" + file.getName());

    String language = locale.getLanguage();
    locale = LocaleContextHolder.getLocale();
    User user = (User) request.getSession().getAttribute("user");
    if (user != null) {
        user.setFullName(fullName);/*  w  ww  .  j a va 2s.  c  om*/
        user.setGovernerate(governerate);
        if (file != null) {
            try {
                user.setImage(file.getBytes());
                byte[] image = user.getImage();

                MagicMatch match = null;
                try {
                    match = Magic.getMagicMatch(image);
                } catch (MagicParseException ex) {
                    Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MagicMatchNotFoundException ex) {
                    Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MagicException ex) {
                    Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                }
                String ext = null;
                if (match != null)
                    ext = "." + match.getExtension();

                File parentDir = new File(Constants.IMAGE_PATH + Constants.USER_PATH);
                if (!parentDir.isDirectory()) {
                    parentDir.mkdirs();
                }
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(
                        new File(Constants.IMAGE_PATH + Constants.USER_PATH + file.getOriginalFilename())));
                stream.write(image);
                stream.close();
                user.setImageUrl(
                        Constants.IMAGE_PRE_URL + Constants.USER_PATH + file.getOriginalFilename() + ext);

            } catch (IOException ex) {
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        user.setMobile(mobil);
        int res = userService.updateUser(user);
        if (res != 0) {

            request.getSession().setAttribute("user", user);
        }
        model.addAttribute("user", user);
    }
    model.addAttribute("lang", locale);

    return "profile";
}

From source file:de.alpharogroup.resourcebundle.properties.PropertiesExtensions.java

/**
 * Load the properties file from the given class object. The filename from the properties file
 * is the same as the simple name from the class object and it looks at the same path as the
 * given class object. If locale is not null than the language will be added to the filename
 * from the properties file./*from   w  ww  .  j av a  2 s.  com*/
 *
 * @param clazz
 *            the clazz
 * @param locale
 *            the locale
 * @return the properties
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadPropertiesFromClassObject(final Class<?> clazz, final Locale locale)
        throws IOException {
    if (null == clazz) {
        throw new IllegalArgumentException("Class object must not be null!!!");
    }
    StringBuilder propertiesName = new StringBuilder();
    Properties properties = null;
    String language = null;
    String filename = null;
    String pathAndFilename = null;
    File propertiesFile = null;
    String absoluteFilename = null;
    final String packagePath = PackageExtensions.getPackagePathWithSlash(clazz);
    final List<String> missedFiles = new ArrayList<>();
    if (null != locale) {
        propertiesName.append(clazz.getSimpleName());
        language = locale.getLanguage();
        if ((null != language) && !language.isEmpty()) {
            propertiesName.append("_").append(language);
        }

        final String country = locale.getCountry();
        if ((null != country) && !country.isEmpty()) {
            propertiesName.append("_").append(country);
        }
        propertiesName.append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        URL url = ClassExtensions.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }

        if ((null != propertiesFile) && propertiesFile.exists()) {
            properties = PropertiesExtensions.loadProperties(pathAndFilename);
        } else {
            propertiesName = new StringBuilder();
            if (null != locale) {
                propertiesName.append(clazz.getSimpleName());
                language = locale.getLanguage();
                if ((null != language) && !language.isEmpty()) {
                    propertiesName.append("_").append(language);
                }
                propertiesName.append(FileExtension.PROPERTIES.getExtension());
                filename = propertiesName.toString().trim();
                pathAndFilename = packagePath + filename;
                url = ClassExtensions.getResource(clazz, filename);

                if (url != null) {
                    absoluteFilename = url.getFile();
                } else {
                    missedFiles.add("File with filename '" + filename + "' does not exists.");
                }
                if (null != absoluteFilename) {
                    propertiesFile = new File(absoluteFilename);
                }
                if ((null != propertiesFile) && propertiesFile.exists()) {
                    properties = PropertiesExtensions.loadProperties(pathAndFilename);
                }
            }
        }
    }

    if (null == properties) {
        propertiesName = new StringBuilder();
        propertiesName.append(clazz.getSimpleName()).append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        final URL url = ClassExtensions.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            properties = PropertiesExtensions.loadProperties(pathAndFilename);
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }
        if ((null != propertiesFile) && propertiesFile.exists()) {
            properties = PropertiesExtensions.loadProperties(pathAndFilename);
        }
    }
    if (properties == null) {
        for (final String string : missedFiles) {
            LOGGER.info(string);
        }
    }

    return properties;
}

From source file:com.doculibre.constellio.lucene.impl.SkosIndexHelperImpl.java

@Override
protected String[] createSearchFields(Field[] indexFields) {
    List<String> fieldNames = new ArrayList<String>();
    fieldNames.add(PREF_LABEL);//from w w w.ja va2 s .  co m
    fieldNames.add(ALT_LABEL);
    for (Locale locale : ConstellioSpringUtils.getSupportedLocales()) {
        String suffix = "_" + locale.getLanguage();
        fieldNames.add(PREF_LABEL + suffix);
        fieldNames.add(ALT_LABEL + suffix);
    }
    return fieldNames.toArray(new String[0]);
}