为什么有些地区即使可用也无法正常工作?

huangapple go评论89阅读模式
英文:

Why some locales are not working even if available?

问题

我遇到了一个奇怪的情况当我尝试对日期进行本地化时有些语言可以正常工作而有些语言不行即使当我使用 `Locale.getAvailableLocales()` 获取所有可用语言环境的列表时它们都会显示出来例如

import java.time.Month
import java.time.format.TextStyle
import java.util.Locale

println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("it")));
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("en"))); // 不起作用
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("fr"))); // 不起作用
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("es"))); // 不起作用
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("fi")));
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("de"))); // 不起作用

Locale.getAvailableLocales().foreach(println)

如果我在 Scastie playground 上尝试复现,我会看到相同奇怪的行为
https://scastie.scala-lang.org/llVhFYjQSu27UMauYw2UDA

我知道本地化取决于JRE上可用的语言环境,我的应用程序目前在一个我插入了这些语言环境的Docker容器中运行。
英文:

I'm facing a strange situation: when I try to localize a date some languages are working, some other languages are not, even if all of them are displayed when I use Locale.getAvailableLocales() to get the list of all available locales. For example:

import java.time.Month
import java.time.format.TextStyle
import java.util.Locale

println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("it")));
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("en"))); // doesn't work
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("fr"))); // doesn't work
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("es"))); // doesn't work
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("fi")));
println(Month.of(3).getDisplayName(TextStyle.FULL_STANDALONE, new Locale("de"))); // doesn't work

Locale.getAvailableLocales().foreach(println)

I see the same strange behaviour if I try to reproduce on Scastie playground
https://scastie.scala-lang.org/llVhFYjQSu27UMauYw2UDA

I know that the localization depends on the locales available on the JRE, my app is currently running in a Docker container where I inserted such locales.

答案1

得分: 3

JDK 8很奇怪 http://tpcg.io/kMvv4qd7

但在JDK 11中可以正常工作 https://repl.it/repls/CriminalChillyHashmap

英文:

JDK 8 is weird http://tpcg.io/kMvv4qd7

But in JDK 11 it works https://repl.it/repls/CriminalChillyHashmap

答案2

得分: 0

如果您想要在不同语言中获取月份您可以尝试

    import java.text.DateFormatSymbols
    
    val frenchMonths: Array[String] = new DateFormatSymbols(Locale.FRENCH).getMonths
    val april = frenchMonths(3)

至于奇怪的行为我很确定是因为某些区域设置不支持该文本样式例如

    // 使用TextSyle.FULL可以正常工作
    println(Month.of(3).getDisplayName(TextStyle.FULL, new Locale("fr"))); 
    // 避免创建新的区域设置    
    println(Month.of(3).getDisplayName(TextStyle.FULL, Locale.forLanguageTag("es")))
英文:

If you want to get just the months in different languages you can try:

import java.text.DateFormatSymbols

val frenchMonths: Array[String] = new DateFormatSymbols(Locale.FRENCH).getMonths
val april = frenchMonths(3)

As for the strange behavior, I am pretty sure it is because that text style is not supported for some locales. For example:

// works with TextSyle.FULL
println(Month.of(3).getDisplayName(TextStyle.FULL, new Locale("fr"))); 
// avoids creating a new locale    
println(Month.of(3).getDisplayName(TextStyle.FULL, Locale.forLanguageTag("es")))

huangapple
  • 本文由 发表于 2020年8月14日 22:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63415047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定