为什么有些语言环境即使可用也不能工作?

问题描述

我正面临一个奇怪的情况:当我尝试对日期进行本地化时,即使使用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"))); // 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)

如果我尝试在Scastie游乐场进行复制,我会看到相同的奇怪行为 https://scastie.scala-lang.org/llVhFYjQSu27UMauYw2UDA

我知道本地化取决于JRE上可用的语言环境,我的应用程序当前运行在我插入此类语言环境的Docker容器中。

解决方法

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

但是在JDK 11中,它可以https://repl.it/repls/CriminalChillyHashmap

,

如果您只想用不同的语言获得月份,可以尝试:

import java.text.DateFormatSymbols

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

对于奇怪的行为,我可以肯定是因为某些语言环境不支持该文本样式。例如:

// 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")))