Java 法语的本地化持续时间格式

Java 法语的本地化持续时间格式,java,python,datetime,localization,Java,Python,Datetime,Localization,Python的类似物是什么: //以秒为单位的持续时间标准化为小时、分钟和秒 Duration dur=持续时间(337540,时钟单位秒),带持续时间(标准时钟周期); //自定义持续时间格式=>hh:mm:ss 字符串s1=模式的Duration.Formatter.of(“hh:mm:ss”).format(dur); 系统输出打印LN(s1);//输出:93:45:40 //法语的本地化持续时间格式 字符串s2=PrettyTime.of(Locale.FRANCE).print(dur

Python的类似物是什么:

//以秒为单位的持续时间标准化为小时、分钟和秒
Duration dur=持续时间(337540,时钟单位秒),带持续时间(标准时钟周期);
//自定义持续时间格式=>hh:mm:ss
字符串s1=模式的Duration.Formatter.of(“hh:mm:ss”).format(dur);
系统输出打印LN(s1);//输出:93:45:40
//法语的本地化持续时间格式
字符串s2=PrettyTime.of(Locale.FRANCE).print(dur,TextWidth.WIDE);
系统输出打印LN(s2);//输出:93小时,45分钟et 40秒
很容易获得
93:45:40

#/usr/bin/env蟒蛇3
从日期时间导入时间增量
dur=时间增量(秒=337540)
打印(dur)#->3天,21:45:40
字段={}
字段['hours',seconds=divmod(dur//timedelta(seconds=1),3600)
字段['minutes'],字段['seconds']=divmod(秒,60)
打印(“%(小时)02d:%(分钟)02d:%(秒)02d”%fields)#->93:45:40
但是我如何模拟Python中的Java代码(不需要硬编码单位)

这个包可能会有所帮助。它有一个法语本地化,或者你可以添加自己的。对于python 2.7和3.3。

允许接近所需的输出:

from babel.dates import format_timedelta # $ pip install babel

print(", ".join(format_timedelta(timedelta(**{unit: fields[unit]}),
                                 granularity=unit.rstrip('s'),
                                 threshold=fields[unit] + 1,
                                 locale='fr')
                for unit in "hours minutes seconds".split()))
# -> 93 heures, 45 minutes, 40 secondes
它自动处理区域设置和复数形式,例如,对于
dur=timedelta(seconds=1)
它生成:

0 heure, 0 minute, 1 seconde
也许更好的解决方案是使用标准工具(如
gettext
)手动翻译格式字符串

使用:


如果您使用的是Kotlin,我刚刚遇到了Kotlin
Duration
type和本地化格式的类似问题,因为找不到好的解决方案,我自己写了一个。它基于Android 9中开始提供的API(用于本地化单元),但对于较低的Android版本,它可以回退到英语单元,因此可以与较低的目标应用程序一起使用

下面是它在使用方面的外观(请参阅Kotlin type以了解第1行):

val duration=5天加3小时加2分钟加214毫秒
DurationFormat().格式(持续时间)/“5天3小时2分钟”
持续时间格式(Locale.GERMANY)。格式(持续时间)/“5T 3Std.2Min。”
DurationFormat(Locale.forLanguageTag(“ar”).format(duration)/“duration”
DurationFormat().format(duration,smallestUnit=DurationFormat.Unit.HOUR)/“5天3小时”
DurationFormat().格式(15分钟)/“15分钟”
DurationFormat().格式(0.hours)/“0秒”
如您所见,您可以为
DurationFormat
类型指定自定义
locale
。默认情况下,它使用
locale.getDefault()
。还支持数字符号与罗马语符号不同的语言(通过
NumberFormat
)。此外,您可以指定自定义的
smallestUnit
,默认情况下它设置为
SECOND
,因此不会显示毫秒。请注意,任何值为
0
的单位都将被忽略,如果整数为
0
,则最小的单位将与值
0
一起使用

这是完整
持续时间格式
类型
,请随意复制(也可作为包括单元测试在内的组件提供):

导入android.icu.text.MeasureFormat
导入android.icu.text.NumberFormat
导入android.icu.util.MeasureUnit
导入android.os.Build
导入java.util.Locale
导入kotlin.time.Duration
导入kotlin.time.ExperimentalTime
导入kotlin.time.days
导入kotlin.time.hours
导入kotlin.time.ms
导入kotlin.time.minutes
导入kotlin.time.seconds
@实验时间
数据类DurationFormat(val locale:locale=locale.getDefault()){
枚举类单元{
天、小时、分钟、秒、毫秒
}
乐趣格式(持续时间:kotlin.time.duration,最小单位:Unit=Unit.SECOND):字符串{
var formattedStringComponents=mutableListOf()
var剩余=持续时间
for(单位为unit.values()){
val组件=计算组件(单位,余数)
余数=时间(单位){
Unit.DAY->余数-component.days
Unit.HOUR->余数-component.hours
Unit.MINUTE->余数-component.minutes
单位秒->余数-分量秒
单位毫秒->余数-分量毫秒
}
val unitDisplayName=unitDisplayName(单位)
如果(组件>0){
val formattedComponent=NumberFormat.getInstance(区域设置)。format(组件)
formattedStringComponents.add(“$formattedComponent$unitDisplayName”)
}
如果(单位==最小单位){
val formattedZero=NumberFormat.getInstance(区域设置).format(0)
if(formattedStringComponents.isEmpty())formattedStringComponents.add(“$formattedZero$unitDisplayName”)
打破
}
}
返回格式化的StringComponents.joinToString(“”)
}
private fun calculateComponent(单位:单位,余数:持续时间)=时间(单位){
Unit.DAY->余数.inDays.toLong()
Unit.HOUR->restinutes.inhour.toLong()
Unit.MINUTE->restinutes.inMinutes.toLong()
Unit.SECOND->rements.unseconds.toLong()
Unit.millis秒->余数.inmillises.toLong()
}
private fun unitDisplayName(unit:unit)=if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.P){
val measureFormat=measureFormat.getInstance(区域设置,measureFormat.FormatWidth.窄)
时间(单位){
DurationFormat.Unit.DAY->measureFormat.getUnitDisplayName(MeasureUnit.DAY)
DurationFormat.Unit.HOUR->measureFormat.getUnitDisplayName(MeasureUnit.HOUR)
DurationFormat.Unit.MINUTE->measureFormat.getUnitDisplayName(MeasureUnit.MINUTE)
DurationFormat.Unit.SECOND->measureFormat.getUnitDisplayName(MeasureUnit.SECOND)
迪拉
>>> import pendulum
>>> it = pendulum.interval(seconds=337540)
>>> it.in_words(locale='fr_FR')
'3 jours 21 heures 45 minutes 40 secondes'