英文:
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))
}
}
时钟应用程序(阿拉伯语)
我的应用程序(阿拉伯语)
时钟应用程序(孟加拉语)
我的应用程序(孟加拉语)
<details>
<summary>英文:</summary>
I'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) +
"\n\n" + 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)
+ '\n' + 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论