DateTimeFormatter在阿拉伯语和孟加拉语中未使用本地数字。

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

DateTimeFormatter not using native numerals in Arabic nor Bengali

问题

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val localizedTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)

        val timeOpen = LocalTime.of(9, 30)
        val timeClose = LocalTime.of(17, 15)

        tv_time.text = (timeOpen.format(localizedTimeFormatter) +
                "\n\n" + timeClose.format(localizedTimeFormatter))
    }
}

时钟应用程序(阿拉伯语)

DateTimeFormatter在阿拉伯语和孟加拉语中未使用本地数字。

我的应用程序(阿拉伯语)

DateTimeFormatter在阿拉伯语和孟加拉语中未使用本地数字。

时钟应用程序(孟加拉语)

DateTimeFormatter在阿拉伯语和孟加拉语中未使用本地数字。

我的应用程序(孟加拉语)

DateTimeFormatter在阿拉伯语和孟加拉语中未使用本地数字。


<details>
<summary>英文:</summary>

I&#39;m trying to use the `DateTimeFormatter` API to show a specific time in a `TextView` but whenever I run my app whilst my device is set to either the Arabic or Bengali language, the time always seems to show western numerals for some reason. Is this intentional, or is there something that I need to do to fix this?

**Kotlin**

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val localizedTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
    
            val timeOpen = LocalTime.of(9, 30)
            val timeClose = LocalTime.of(17, 15)
    
            tv_time.text = (timeOpen.format(localizedTimeFormatter) +
                    &quot;\n\n&quot; + timeClose.format(localizedTimeFormatter))
        }
    }

**Clock app (Arabic)**

[![enter image description here][1]][1]

**My app (Arabic)**

[![enter image description here][2]][2]

**Clock app (Bengali)**

[![enter image description here][3]][3]

**My app (Bengali)**

[![enter image description here][4]][4]


  [1]: https://i.stack.imgur.com/vDTFx.png
  [2]: https://i.stack.imgur.com/ljnhT.png
  [3]: https://i.stack.imgur.com/G90ri.png
  [4]: https://i.stack.imgur.com/0eLYH.png

</details>


# 答案1
**得分**: 3

这是按设计进行的。`DateTimeFormatter` 的实例,即使是本地化的实例,除非明确另有指示,否则都使用西方/ASCII 数字。当您知道如何操作时,指示并不难:

```java
DateTimeFormatter localizedTimeFormatter = DateTimeFormatter
        .ofLocalizedTime(FormatStyle.SHORT)
        .withDecimalStyle(DecimalStyle.ofDefaultLocale());

LocalTime timeOpen = LocalTime.of(9, 30);
LocalTime timeClose = LocalTime.of(17, 15);

String text = timeOpen.format(localizedTimeFormatter)
        + '\n' + timeClose.format(localizedTimeFormatter);
System.out.println(text);

孟加拉语区域 (bn-BD) 的输出:

৯:৩০ AM
৫:১৫ PM

而在阿拉伯语区域 (ar) 中:

٩:٣٠ ص
٥:١٥ م

我希望您能够自行从我的 Java 代码中进行翻译。我相信这不应该很难。

英文:

This is as designed. Instances of DateTimeFormatter, even the localized ones, use Western/ASCII digits unless explicitly instructed otherwise. The instruction is not hard when you know how:

	DateTimeFormatter localizedTimeFormatter = DateTimeFormatter
			.ofLocalizedTime(FormatStyle.SHORT)
			.withDecimalStyle(DecimalStyle.ofDefaultLocale());
	
	LocalTime timeOpen = LocalTime.of(9, 30);
	LocalTime timeClose = LocalTime.of(17, 15);
	
	String text = timeOpen.format(localizedTimeFormatter)
			+ &#39;\n&#39; + timeClose.format(localizedTimeFormatter);
	System.out.println(text);

Output in Bengali locale (bn-BD):

> ৯:৩০ AM
> ৫:১৫ PM

And in Arabic (ar):

> ٩:٣٠ ص
> ٥:١٥ م

I hope you can translate from my Java code yourself. I believe that it shouldn’t be hard.

huangapple
  • 本文由 发表于 2020年9月23日 02:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64015639.html
匿名

发表评论

匿名网友

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

确定