英文:
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")))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论