java.util.Locale.getLanguage()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(304)

本文整理了Java中java.util.Locale.getLanguage()方法的一些代码示例,展示了Locale.getLanguage()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Locale.getLanguage()方法的具体详情如下:
包路径:java.util.Locale
类名称:Locale
方法名:getLanguage

Locale.getLanguage介绍

[英]Returns the language code for this Locale or the empty string if no language was set.
[中]返回此区域设置的语言代码,如果未设置语言,则返回空字符串。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Determine the RFC 3066 compliant language tag,
 * as used for the HTTP "Accept-Language" header.
 * @param locale the Locale to transform to a language tag
 * @return the RFC 3066 compliant language tag as {@code String}
 * @deprecated as of 5.0.4, in favor of {@link Locale#toLanguageTag()}
 */
@Deprecated
public static String toLanguageTag(Locale locale) {
  return locale.getLanguage() + (hasText(locale.getCountry()) ? "-" + locale.getCountry() : "");
}

代码示例来源:origin: stackoverflow.com

public class MyApplication extends Application {

  public static String sDefSystemLanguage;

  @Override
  public void onCreate() {
    super.onCreate();

    sDefSystemLanguage = Locale.getDefault().getLanguage();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    sDefSystemLanguage = newConfig.locale.getLanguage();
  }

}

代码示例来源:origin: redisson/redisson

/**
 * Resolves locale code from locale.
 */
public static String resolveLocaleCode(Locale locale) {
  return resolveLocaleCode(locale.getLanguage(), locale.getCountry(), locale.getVariant());
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Only the given locale is searched. Contrary to java.util.ResourceBundle, no strategy for locating the bundle is implemented in
 * this method.
 */
String messageFromFile(Locale locale, String filename, String relatedProperty) {
 String result = null;
 String bundleBase = propertyToBundles.get(relatedProperty);
 if (bundleBase == null) {
  // this property has no translation
  return null;
 }
 String filePath = bundleBase.replace('.', '/');
 if (!"en".equals(locale.getLanguage())) {
  filePath += "_" + locale.getLanguage();
 }
 filePath += "/" + filename;
 InputStream input = classloader.getResourceAsStream(filePath);
 if (input != null) {
  result = readInputStream(filePath, input);
 }
 return result;
}

代码示例来源:origin: marytts/marytts

/**
 * Get the feature processor manager associated with the given locale, if any.
 * 
 * @param locale
 *            locale
 * @return the feature processor manager, or null if there is no locale-specific feature processor manager.
 */
public static FeatureProcessorManager getFeatureProcessorManager(Locale locale) {
  FeatureProcessorManager m = managersByLocale.get(locale);
  if (m != null)
    return m;
  // Maybe locale is language_COUNTRY, so look up by language also:
  Locale lang = new Locale(locale.getLanguage());
  return managersByLocale.get(lang);
}

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

for (String namespace : config.getLanguageNamespaces()) {
      String filename = locale.getLanguage() + "_" + locale.getCountry() + File.separator + namespace + ".json";
        filename = locale.getLanguage() + File.separator + namespace + ".json";
        r = getBaseDirectory().createRelative(filename);
return languageMaps.get(locale);

代码示例来源:origin: org.freemarker/freemarker

/**
 * Returns a locale that's one less specific, or {@code null} if there's no less specific locale.
 */
public static Locale getLessSpecificLocale(Locale locale) {
  String country = locale.getCountry();
  if (locale.getVariant().length() != 0) {
    String language = locale.getLanguage();
    return country != null ? new Locale(language, country) : new Locale(language);
  }
  if (country.length() != 0) {
    return new Locale(locale.getLanguage());
  }
  return null;
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

/**
 * Accept-Language: zh-CN,zh;q=0.8
 */
public static String getAcceptLanguage() {
  if (TextUtils.isEmpty(acceptLanguage)) {
    Locale locale = Locale.getDefault();
    String language = locale.getLanguage();
    String country = locale.getCountry();
    StringBuilder acceptLanguageBuilder = new StringBuilder(language);
    if (!TextUtils.isEmpty(country)) acceptLanguageBuilder.append('-').append(country).append(',').append(language).append(";q=0.8");
    acceptLanguage = acceptLanguageBuilder.toString();
    return acceptLanguage;
  }
  return acceptLanguage;
}

代码示例来源:origin: stackoverflow.com

// may contain simple syntax error, i dont have java rite now to test..
// but this is a bigger picture for ur algo...
public String localeToString(Locale l) {
  return l.getLanguage() + "," + l.getCountry();
}

public Locale stringToLocale(String s) {
  StringTokenizer tempStringTokenizer = new StringTokenizer(s,",");
  if(tempStringTokenizer.hasMoreTokens())
  String l = tempStringTokenizer.nextElement();
  if(tempStringTokenizer.hasMoreTokens())
  String c = tempStringTokenizer.nextElement();
  return new Locale(l,c);
}

代码示例来源:origin: torakiki/pdfsam

Locale getBestLocale() {
  if (SUPPORTED_LOCALES.contains(Locale.getDefault())) {
    return Locale.getDefault();
  }
  Locale onlyLanguage = new Locale(Locale.getDefault().getLanguage());
  if (SUPPORTED_LOCALES.contains(onlyLanguage)) {
    LOG.trace("Using supported locale closest to default {}", onlyLanguage);
    return onlyLanguage;
  }
  LOG.trace("Using fallback locale");
  return Locale.UK;
}

代码示例来源:origin: stackoverflow.com

@ManagedBean
@SessionScoped
public class LocaleManager {

  private Locale locale;

  @PostConstruct
  public void init() {
    locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
  }

  public Locale getLocale() {
    return locale;
  }

  public String getLanguage() {
    return locale.getLanguage();
  }

  public void setLanguage(String language) {
    locale = new Locale(language);
    FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
  }

}

代码示例来源:origin: shopizer-ecommerce/shopizer

@Override
public Language toLanguage(Locale locale) {
  
  try {
    Language lang = getLanguagesMap().get(locale.getLanguage());
    return lang;
  } catch (Exception e) {
    LOGGER.error("Cannot convert locale " + locale.getLanguage() + " to language");
  }
  
  return new Language(Constants.DEFAULT_LANGUAGE);
}

代码示例来源:origin: marytts/marytts

/**
 * Get the feature processor manager associated with the given locale, if any.
 * 
 * @param locale
 *            locale
 * @return the feature processor manager, or null if there is no locale-specific feature processor manager.
 */
public static FeatureProcessorManager getFeatureProcessorManager(Locale locale) {
  FeatureProcessorManager m = managersByLocale.get(locale);
  if (m != null)
    return m;
  // Maybe locale is language_COUNTRY, so look up by language also:
  Locale lang = new Locale(locale.getLanguage());
  return managersByLocale.get(lang);
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

public static String[] getDays(Locale locale, String pattern) {
  if (Validator.isNull(pattern)) {
    pattern = "EEEE";
  }
  StringBundler sb = new StringBundler(6);
  sb.append("days_");
  sb.append(pattern);
  sb.append("_");
  sb.append(locale.getLanguage());
  sb.append("_");
  sb.append(locale.getCountry());
  String key = sb.toString();
  String[] days = _calendarPool.get(key);
  if (days == null) {
    days = new String[7];
    Format dayFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
      pattern, locale);
    Calendar cal = CalendarFactoryUtil.getCalendar();
    cal.set(Calendar.DATE, 1);
    for (int i = 0; i < 7; i++) {
      cal.set(Calendar.DAY_OF_WEEK, i + 1);
      days[i] = dayFormat.format(cal.getTime());
    }
    _calendarPool.put(key, days);
  }
  return days;
}

代码示例来源:origin: org.apache.commons/commons-lang3

if (locale != null) {
  list.add(locale);
  if (!locale.getVariant().isEmpty()) {
    list.add(new Locale(locale.getLanguage(), locale.getCountry()));
  if (!locale.getCountry().isEmpty()) {
    list.add(new Locale(locale.getLanguage(), StringUtils.EMPTY));

代码示例来源:origin: spotbugs/spotbugs

private  static  List<String> getPotentialMessageFiles() {
  // Load the message collections
  Locale locale = Locale.getDefault();
  String language = locale.getLanguage();
  String country = locale.getCountry();
  List<String> potential = new ArrayList<>(3);
  if (country != null) {
    potential.add("messages_" + language + "_" + country + ".xml");
  }
  potential.add("messages_" + language + ".xml");
  potential.add("messages.xml");
  return potential;
}

代码示例来源:origin: floragunncom/search-guard

private static Locale forEN()
{
  if ("en".equalsIgnoreCase(Locale.getDefault().getLanguage()))
  {
    return Locale.getDefault();
  }
  Locale[] locales = Locale.getAvailableLocales();
  for (int i = 0; i != locales.length; i++)
  {
    if ("en".equalsIgnoreCase(locales[i].getLanguage()))
    {
      return locales[i];
    }
  }
  return Locale.getDefault();
}

代码示例来源:origin: spring-projects/spring-framework

String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
StringBuilder temp = new StringBuilder(basename);

代码示例来源:origin: lets-blade/blade

public static Tuple2<String,Locale> toLocaleModel(String baseName,Locale locale) {
  if (StringKit.isBlank(baseName)) {
    return new Tuple2<>("i18n_"+locale.getLanguage()+"_"+locale.getCountry(),locale);
  }else {
    String[] baseNames = pattern.split(baseName);
    if (baseNames != null && baseNames.length == 3) {
      return new Tuple2<>(baseName,new Locale(baseNames[1],baseNames[2]));
    }
    throw new IllegalArgumentException("baseName illegal,name format is :i18n_{language}_{country}.properties," +
        "for example:i18n_zh_CN.properties");
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private Locale findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales) {
  Enumeration<Locale> requestLocales = request.getLocales();
  Locale languageMatch = null;
  while (requestLocales.hasMoreElements()) {
    Locale locale = requestLocales.nextElement();
    if (supportedLocales.contains(locale)) {
      if (languageMatch == null || languageMatch.getLanguage().equals(locale.getLanguage())) {
        // Full match: language + country, possibly narrowed from earlier language-only match
        return locale;
      }
    }
    else if (languageMatch == null) {
      // Let's try to find a language-only match as a fallback
      for (Locale candidate : supportedLocales) {
        if (!StringUtils.hasLength(candidate.getCountry()) &&
            candidate.getLanguage().equals(locale.getLanguage())) {
          languageMatch = candidate;
          break;
        }
      }
    }
  }
  return languageMatch;
}

相关文章