Java File Name Get getFilename(Locale locale, String fileName, String fileType)

Here you can find the source of getFilename(Locale locale, String fileName, String fileType)

Description

Get the appropriate localized version of a file according to language settings e.

License

BSD License

Parameter

Parameter Description
locale Locale to get the file for
fileName String fileName, to get the localized file for
fileType String file extension

Return

localizedFileName String - localized filename

Declaration

private static String getFilename(Locale locale, String fileName, String fileType) 

Method Source Code

//package com.java2s;
/**/*  www  . ja v a  2  s  . c  o m*/
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */

import java.io.File;
import java.util.Locale;

public class Main {
    /**
     * Get the appropriate localized version of a file according to language settings
     * e. g. help files in jsp/help/
     *
     * @param locale
     *        Locale to get the file for
     * @param fileName
     *        String fileName, to get the localized file for
     * @param fileType
     *        String file extension
     * @return localizedFileName
     *          String - localized filename
     */
    private static String getFilename(Locale locale, String fileName, String fileType) {
        String localizedFileName = null;
        boolean fileFound = false;
        // with Language, Country, Variant
        String fileNameLCV = null;
        // with Language, Country
        String fileNameLC = null;
        // with Language
        String fileNameL = null;
        fileNameL = fileName + "_" + locale.getLanguage();

        if (fileType == null) {
            fileType = "";
        }

        if (!("".equals(locale.getCountry()))) {
            fileNameLC = fileName + "_" + locale.getLanguage() + "_" + locale.getCountry();

            if (!("".equals(locale.getVariant()))) {
                fileNameLCV = fileName + "_" + locale.getLanguage() + "_" + locale.getCountry() + "_"
                        + locale.getVariant();
            }
        }

        if (fileNameLCV != null && !fileFound) {
            File fileTmp = new File(fileNameLCV + fileType);
            if (fileTmp.exists()) {
                fileFound = true;
                localizedFileName = fileNameLCV + fileType;
            }
        }

        if (fileNameLC != null && !fileFound) {
            File fileTmp = new File(fileNameLC + fileType);
            if (fileTmp.exists()) {
                fileFound = true;
                localizedFileName = fileNameLC + fileType;
            }
        }

        if (fileNameL != null && !fileFound) {
            File fileTmp = new File(fileNameL + fileType);
            if (fileTmp.exists()) {
                fileFound = true;
                localizedFileName = fileNameL + fileType;
            }
        }
        if (!fileFound) {
            localizedFileName = fileName + fileType;
        }
        return localizedFileName;
    }
}

Related

  1. getFileName(final File file)
  2. getFilename(final String filenamePath)
  3. getFileName(final String p)
  4. getFilename(final String pPath)
  5. getFileName(final String subdirectory)
  6. getFileName(String absolutePath)
  7. getFileName(String completePath)
  8. getFileName(String dir, String name)
  9. getFileName(String f)