@@ -10,17 +10,17 @@ import { BasePluralRule, Tmui4xI18nTml, DateTimeFormatReal, I18nOptions, I18nOpt
// 日期时间名称格式类型
type DateTimeNames = {
long : string [ ] ,
short : string [ ] ,
narrow : string [ ]
long : string [ ] ,
short : string [ ] ,
narrow : string [ ]
}
type BestTimeTYpe = { unit : string , value : number }
type timeUnitsTYpe = { unit : string , ms : number , threshold : number }
export function mergeI18nOpts ( args ? : I18nOptions ) : I18nOptionsReally {
type BestTimeTYpe = { unit : string , value : number }
type timeUnitsTYpe = { unit : string , ms : number , threshold : number }
export function mergeI18nOpts ( args? : I18nOptions ) : I18nOptionsReally {
let localLang = uni . getStorageSync ( "language" )
// 提供默认配置
const currentLan = localLang != null && localLang != '' && typeof localLang == 'string' ? ( localLang ! as string ) : "zh-Hans"
const defaultOptions : I18nOptionsReally = {
const defaultOptions : I18nOptionsReally = {
locale : currentLan ,
fallbackLocale : "en" ,
messages : { } ,
@@ -41,7 +41,7 @@ export function mergeI18nOpts(args ?: I18nOptions) : I18nOptionsReally {
warnHandler : ( v , e ) = > {
console . warn ( ` [tmui4x]: ${ v } ` , e || '' ) ;
} ,
pluralRule : ( choice : number , choicesLength : number , orgRule ? : BasePluralRule | null ) : number = > {
pluralRule : ( choice : number , choicesLength : number , orgRule? : BasePluralRule | null ) : number = > {
// 默认英语复数规则实现
// 参考 Vue I18n 标准实现:https://vue-i18n.intlify.dev/guide/essentials/pluralization
// 如果提供了原始规则函数,优先使用
@@ -110,37 +110,37 @@ export function mergeI18nOpts(args ?: I18nOptions) : I18nOptionsReally {
}
export class Tmui4xI18n implements Tmui4xI18nTml {
ops : I18nOptionsReally ;
private static instance : Tmui4xI18n ;
constructor ( args ? : I18nOptions ) {
ops : I18nOptionsReally ;
private static instance : Tmui4xI18n ;
constructor ( args? : I18nOptions ) {
this . ops = mergeI18nOpts ( args )
}
/ * *
* 单 例 模 式
* /
public static getInstance ( args ? : I18nOptions ) : Tmui4xI18n {
public static getInstance ( args? : I18nOptions ) : Tmui4xI18n {
if ( Tmui4xI18n . instance ) {
return Tmui4xI18n . instance
}
return new Tmui4xI18n ( args ) ;
}
// 语言
setLocale ( local : string ) {
setLocale ( local : string ) {
uni . setStorageSync ( 'language' , local )
this . ops . locale = local
}
//设置语言
getLocale ( ) : string {
getLocale ( ) : string {
return this . ops . locale
}
getFallbackLocale ( ) : string {
getFallbackLocale ( ) : string {
return this . ops . fallbackLocale
}
setFallbackLocale ( local : string ) {
setFallbackLocale ( local : string ) {
this . ops . fallbackLocale = local
}
setOptions ( args : I18nOptions ) {
setOptions ( args : I18nOptions ) {
this . ops = mergeI18nOpts ( args )
}
/ * *
@@ -150,14 +150,14 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param opts 可 选 的 第 二 个 参 数 当 args为复数数字 , key中函数 | 时 , 此 opts起效且必须为utsjsonobject格式等同原vueI18n使用方式
* @returns 翻 译 后 的 字 符 串
* /
t ( key : string , . . . argsopts : Array < any > ) : string {
let repeatCount : NumberOrNull = null ;
let values : UTSJSONObjectOrArray = { } ;
let locale : string = this . ops ? . locale ? ? 'zh-Hans' ;
t ( key : string , . . . argsopts : Array < any > ) : string {
let repeatCount : NumberOrNull = null ;
let values : UTSJSONObjectOrArray = { } ;
let locale : string = this . ops ? . locale ? ? 'zh-Hans' ;
if ( argsopts . length > 0 ) {
// 严格的类型检查和参数解析
let args : AnyOrNull = argsopts [ 0 ]
let opts : AnyOrNull = null ;
let args : AnyOrNull = argsopts [ 0 ]
let opts : AnyOrNull = null ;
if ( argsopts . length > 1 ) {
opts = argsopts [ 1 ]
}
@@ -165,33 +165,33 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
if ( args != null && opts == null ) {
if ( typeof args == 'number' ) {
// args 是数字类型
const numArgs : number = args ;
const numArgs : number = args ;
values = { count : numArgs , n : numArgs } ;
repeatCount = numArgs
} else if ( typeof args == 'string' ) {
// args 是字符串类型(locale)
const strArgs : string = args ;
const strArgs : string = args ;
locale = strArgs ;
} else if ( args instanceof Object && args != null ) {
const objArgs : Record < string , any > = args ;
const objArgs : Record < string , any > = args ;
values = objArgs ;
} else if ( Array . isArray ( args ) ) {
const arrArgs : Array < any > = args ;
const arrArgs : Array < any > = args ;
values = arrArgs ;
}
} else if ( typeof args == 'number' && opts instanceof Object && opts != null ) {
const objOpts : Record < string , any > = opts ;
const objOpts : Record < string , any > = opts ;
values = objOpts ;
const numArgs : number = args ;
const numArgs : number = args ;
repeatCount = numArgs
}
}
// 类型安全的消息获取
let message : StringOrNull = this . getMessage ( key , locale ) ;
let message : StringOrNull = this . getMessage ( key , locale ) ;
// 如果没有找到消息,尝试回退语言
if ( message == null && locale != this . ops . fallbackLocale ) {
@@ -209,7 +209,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
// 类型安全的复数处理
if ( repeatCount != null && typeof message == 'string' && message . includes ( '|' ) ) {
const safeCount : number = repeatCount ;
const safeCount : number = repeatCount ;
message = this . handlePlural ( message , safeCount , locale ) ;
}
@@ -220,7 +220,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
// 类型安全的后处理
if ( this . ops . postTranslation != null && typeof message == 'string' ) {
const processedMessage : string = this . ops . postTranslation ( message , key ) ;
const processedMessage : string = this . ops . postTranslation ( message , key ) ;
message = processedMessage ;
}
@@ -235,9 +235,9 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param opts 格 式 化 选 项
* @returns 格 式 化 后 的 字 符 串
* /
n ( val : number , formatName ? : StringOrNull , opts ? : NumberFormatOrNull ) : string {
n ( val : number , formatName? : StringOrNull , opts? : NumberFormatOrNull ) : string {
let currentLocale = ( opts ? . local ? ? this . ops . locale ) ! ;
let formatOptions : NumberFormatOpts = {
let formatOptions : NumberFormatOpts = {
style : 'decimal' ,
currency : 'CNY' ,
local : currentLocale ,
@@ -304,9 +304,9 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param opts 格 式 化 选 项
* @returns 格 式 化 后 的 字 符 串
* /
d ( val : DateOrNumberOrString , formatName ? : StringOrNull , opts ? : DateTimeFormatOrNull ) : string {
d ( val : DateOrNumberOrString , formatName? : StringOrNull , opts? : DateTimeFormatOrNull ) : string {
let currentLocale = ( opts ? . local ? ? this . ops . locale ) ! ;
let formatOptions : DateTimeFormatReal = {
let formatOptions : DateTimeFormatReal = {
localeMatcher : 'best fit' ,
local : currentLocale ,
calendar : 'gregory' ,
@@ -407,13 +407,13 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param { string } local 语 言
* @param { Record < string , any > } newMessage 键 值 及 字 段 。
* /
mergeLocaleMessage ( local : string , newMessage : Record < string , any > ) {
let nowmessage = this . ops . messages ? . [ local ] ? ? null ;
mergeLocaleMessage ( local : string , newMessage : Record < string , any > ) {
let nowmessage = this . ops . messages ? . [ local ] ? ? null ;
if ( nowmessage != null ) {
nowmessage = { . . . nowmessage , . . . newMessage }
this . ops . messages [ local ] = { . . . this . ops . messages . local , . . . newMessage }
this . ops . messages [ local ] = { . . . this . ops . messages . local , . . . newMessage }
} else {
this . ops . messages [ local ] = newMessage
this . ops . messages [ local ] = newMessage
}
}
/ * *
@@ -422,7 +422,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param { string } locale 可 选 的 语 言 代 码 , 不 提 供 则 使 用 当 前 语 言
* @returns { boolean } 存 在 返 回 true , 不 存 在 返 回 false
* /
te ( key : string , locale ? : string | null ) : boolean {
te ( key : string , locale? : string | null ) : boolean {
// 确定要检查的语言
const targetLocale = ( locale != null ? locale : this.ops.locale ) ;
@@ -449,7 +449,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param { string } locale 可 选 语 言 , 不 提 供 以 默 认 创 建 的 设 置 为 准 。
* @returns { string } 格 式 化 后 的 时 间 字 符 串
* /
rt ( timeValues ? : number | Date | string | null , units ? : string | null , locale ? : string | null ) : string {
rt ( timeValues? : number | Date | string | null , units? : string | null , locale? : string | null ) : string {
const targetLocale = ( locale != null ? locale : this.ops.locale )
const timeValue = ( timeValues != null ? new Date ( ) : timeValues ! )
const unit = ( units != null ? 'minute' : units ! )
@@ -534,7 +534,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* 获 取 可 用 语 言 列 表
* @returns { string [ ] }
* /
availableLocales ( ) : string [ ] {
availableLocales ( ) : string [ ] {
return Object . keys ( this . ops . messages )
}
/ * *
@@ -543,11 +543,11 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param preferredUnit 首 选 单 位
* @returns 包 含 最 佳 单 位 和 数 值 的 对 象
* /
private getBestTimeUnit ( timeDiff : number , preferredUnit : string ) : BestTimeTYpe {
private getBestTimeUnit ( timeDiff : number , preferredUnit : string ) : BestTimeTYpe {
const absTimeDiff = Math . abs ( timeDiff ) ;
// 定义时间单位的毫秒值和优先级
const timeUnits : timeUnitsTYpe [ ] = [
const timeUnits : timeUnitsTYpe [ ] = [
{ unit : 'second' , ms : 1000 , threshold : 60 } ,
{ unit : 'minute' , ms : 1000 * 60 , threshold : 60 } ,
{ unit : 'hour' , ms : 1000 * 60 * 60 , threshold : 24 } ,
@@ -589,7 +589,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 格 式 化 后 的 相 对 时 间 字 符 串
* /
private formatRelativeTimeFallback ( value : number , unit : string , locale : string ) : string {
private formatRelativeTimeFallback ( value : number , unit : string , locale : string ) : string {
const absValue = Math . abs ( value ) ;
const isPast = value < 0 ;
const isFuture = value > 0 ;
@@ -656,7 +656,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param langCode 语 言 代 码
* @returns 本 地 化 的 单 位 名 称
* /
private getLocalizedUnitName ( unit : string , value : number , langCode : string ) : string {
private getLocalizedUnitName ( unit : string , value : number , langCode : string ) : string {
const isPlural = value != 1 ;
switch ( langCode ) {
@@ -719,10 +719,10 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 格 式 化 后 的 字 符 串
* /
private formatDateTime ( value : DateOrNumberOrString , options : DateTimeFormatReal , locale : string ) : string {
private formatDateTime ( value : DateOrNumberOrString , options : DateTimeFormatReal , locale : string ) : string {
try {
// 转换输入值为Date对象
let dateObj : Date = new Date ( ) ;
let dateObj : Date = new Date ( ) ;
if ( value instanceof Date ) {
dateObj = value ;
} else if ( typeof value == 'number' ) {
@@ -758,7 +758,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 格 式 化 后 的 字 符 串
* /
private formatWithPredefinedStyle ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
private formatWithPredefinedStyle ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
let result = '' ;
// 处理日期样式
@@ -814,9 +814,9 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 格 式 化 后 的 字 符 串
* /
private formatWithCustomOptions ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
const dateParts : string [ ] = [ ] ;
const timeParts : string [ ] = [ ] ;
private formatWithCustomOptions ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
const dateParts : string [ ] = [ ] ;
const timeParts : string [ ] = [ ] ;
// 根据语言环境确定日期组件的排列顺序
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
@@ -900,7 +900,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
}
// 组合日期和时间
const result : string [ ] = [ ] ;
const result : string [ ] = [ ] ;
if ( dateParts . length > 0 ) {
result . push ( this . combineDateTimeParts ( dateParts , locale , options . dateSeparator ) ) ;
}
@@ -918,7 +918,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 格 式 化 后 的 字 符 串
* /
private formatNumber ( value : number , options : NumberFormatOpts , locale : string ) : string {
private formatNumber ( value : number , options : NumberFormatOpts , locale : string ) : string {
try {
// 基础格式化逻辑
let result = value . toString ( ) ;
@@ -991,7 +991,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param options 格 式 化 选 项
* @returns 格 式 化 后 的 字 符 串
* /
private formatWithSignificantDigits ( value : number , options : NumberFormatOpts ) : string {
private formatWithSignificantDigits ( value : number , options : NumberFormatOpts ) : string {
const minSigDigits = options . minimumSignificantDigits ? ? 1 ;
const maxSigDigits = options . maximumSignificantDigits ? ? 21 ;
@@ -1023,7 +1023,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param minIntegerDigits 最 小 整 数 位 数
* @returns 格 式 化 后 的 字 符 串
* /
private formatWithMinimumIntegerDigits ( numStr : string , minIntegerDigits : number ) : string {
private formatWithMinimumIntegerDigits ( numStr : string , minIntegerDigits : number ) : string {
const parts = numStr . split ( '.' ) ;
const integerPart = parts [ 0 ] ;
const decimalPart = parts . length > 1 ? parts [ 1 ] : '' ;
@@ -1039,7 +1039,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 完 整 日 期 格 式
* /
private getFullDateFormat ( date : Date , locale : string ) : string {
private getFullDateFormat ( date : Date , locale : string ) : string {
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
const isUSFormat = langCode == 'en' && ( locale . toLowerCase ( ) . includes ( 'us' ) || locale . toLowerCase ( ) == 'en' ) ;
@@ -1061,7 +1061,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 长 日 期 格 式
* /
private getLongDateFormat ( date : Date , locale : string ) : string {
private getLongDateFormat ( date : Date , locale : string ) : string {
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
const isUSFormat = langCode == 'en' && ( locale . toLowerCase ( ) . includes ( 'us' ) || locale . toLowerCase ( ) == 'en' ) ;
@@ -1083,7 +1083,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 中 等 日 期 格 式
* /
private getMediumDateFormat ( date : Date , locale : string ) : string {
private getMediumDateFormat ( date : Date , locale : string ) : string {
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
const isUSFormat = langCode == 'en' && ( locale . toLowerCase ( ) . includes ( 'us' ) || locale . toLowerCase ( ) == 'en' ) ;
@@ -1102,7 +1102,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 短 日 期 格 式
* /
private getShortDateFormat ( date : Date , locale : string ) : string {
private getShortDateFormat ( date : Date , locale : string ) : string {
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
const isUSFormat = langCode == 'en' && ( locale . toLowerCase ( ) . includes ( 'us' ) || locale . toLowerCase ( ) == 'en' ) ;
const year = date . getFullYear ( ) . toString ( ) . slice ( - 2 ) ;
@@ -1124,7 +1124,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 完 整 时 间 格 式
* /
private getFullTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
private getFullTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
const timeStr = this . getMediumTimeFormat ( date , options , locale ) ;
const timeZone = this . formatTimeZone ( date , 'long' , options . timeZone ) ;
return ` ${ timeStr } ${ timeZone } ` ;
@@ -1133,7 +1133,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 长 时 间 格 式
* /
private getLongTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
private getLongTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
const timeStr = this . getMediumTimeFormat ( date , options , locale ) ;
const timeZone = this . formatTimeZone ( date , 'short' , options . timeZone ) ;
return ` ${ timeStr } ${ timeZone } ` ;
@@ -1142,7 +1142,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 中 等 时 间 格 式
* /
private getMediumTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
private getMediumTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
const hour = this . formatHour ( date , 'numeric' , options . hour12 , options . hourCycle ) ;
const minute = this . formatMinute ( date , '2-digit' ) ;
const second = this . formatSecond ( date , '2-digit' , 0 ) ;
@@ -1153,7 +1153,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 短 时 间 格 式
* /
private getShortTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
private getShortTimeFormat ( date : Date , options : DateTimeFormatReal , locale : string ) : string {
const hour = this . formatHour ( date , 'numeric' , options . hour12 , options . hourCycle ) ;
const minute = this . formatMinute ( date , '2-digit' ) ;
const dayPeriod = options . hour12 ? ` ${ this . formatDayPeriod ( date , 'short' , locale ) } ` : '' ;
@@ -1163,7 +1163,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 星 期
* /
private formatWeekday ( date : Date , style : string , locale : string ) : string {
private formatWeekday ( date : Date , style : string , locale : string ) : string {
const weekdays = this . getWeekdayNames ( locale ) ;
const dayIndex = date . getDay ( ) ;
@@ -1182,7 +1182,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 纪 元
* /
private formatEra ( date : Date , style : string , locale : string ) : string {
private formatEra ( date : Date , style : string , locale : string ) : string {
const year = date . getFullYear ( ) ;
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
@@ -1206,7 +1206,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param numberingSystem 数 字 系 统
* @returns 转 换 后 的 数 字 字 符 串
* /
private convertNumberingSystem ( number : string , numberingSystem : string ) : string {
private convertNumberingSystem ( number : string , numberingSystem : string ) : string {
if ( numberingSystem == 'latn' || numberingSystem == '' ) {
return number ; // 拉丁数字(默认)
}
@@ -1243,7 +1243,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 年 份
* /
private formatYear ( date : Date , style : string , locale : string = '' , numberingSystem : string = 'latn' , dateSeparator : string = '' ) : string {
private formatYear ( date : Date , style : string , locale : string = '' , numberingSystem : string = 'latn' , dateSeparator : string = '' ) : string {
const year = date . getFullYear ( ) ;
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
@@ -1266,7 +1266,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 月 份
* /
private formatMonth ( date : Date , style : string , locale : string , numberingSystem : string = 'latn' , dateSeparator : string = '' ) : string {
private formatMonth ( date : Date , style : string , locale : string , numberingSystem : string = 'latn' , dateSeparator : string = '' ) : string {
const month = date . getMonth ( ) ;
const monthNames = this . getMonthNames ( locale ) ;
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
@@ -1295,7 +1295,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 日 期
* /
private formatDay ( date : Date , style : string , locale : string = '' , numberingSystem : string = 'latn' , dateSeparator : string = '' ) : string {
private formatDay ( date : Date , style : string , locale : string = '' , numberingSystem : string = 'latn' , dateSeparator : string = '' ) : string {
const day = date . getDate ( ) ;
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
@@ -1317,7 +1317,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 小 时
* /
private formatHour ( date : Date , style : string , hour12 : boolean , hourCycle : string , numberingSystem : string = 'latn' ) : string {
private formatHour ( date : Date , style : string , hour12 : boolean , hourCycle : string , numberingSystem : string = 'latn' ) : string {
let hour = date . getHours ( ) ;
if ( hour12 ) {
@@ -1349,7 +1349,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 分 钟
* /
private formatMinute ( date : Date , style : string , numberingSystem : string = 'latn' ) : string {
private formatMinute ( date : Date , style : string , numberingSystem : string = 'latn' ) : string {
const minute = date . getMinutes ( ) ;
switch ( style ) {
case 'numeric' :
@@ -1364,7 +1364,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 秒
* /
private formatSecond ( date : Date , style : string , fractionalDigits : number , numberingSystem : string = 'latn' ) : string {
private formatSecond ( date : Date , style : string , fractionalDigits : number , numberingSystem : string = 'latn' ) : string {
const second = date . getSeconds ( ) ;
const millisecond = date . getMilliseconds ( ) ;
@@ -1394,7 +1394,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 时 段 ( AM / PM )
* /
private formatDayPeriod ( date : Date , style : string , locale : string ) : string {
private formatDayPeriod ( date : Date , style : string , locale : string ) : string {
const hour = date . getHours ( ) ;
const isAM = hour < 12 ;
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
@@ -1423,7 +1423,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 格 式 化 时 区
* /
private formatTimeZone ( date : Date , style : string , timeZone : string ) : string {
private formatTimeZone ( date : Date , style : string , timeZone : string ) : string {
// 简化实现,实际应用中可能需要更复杂的时区处理
switch ( style ) {
case 'long' :
@@ -1437,7 +1437,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 组 合 日 期 时 间 各 部 分
* /
private combineDateTimeParts ( parts : string [ ] , locale : string , dateSeparator : string = '' ) : string {
private combineDateTimeParts ( parts : string [ ] , locale : string , dateSeparator : string = '' ) : string {
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
// 如果设置了自定义分隔符,使用自定义分隔符连接
@@ -1457,7 +1457,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 星 期 名 称
* /
private getWeekdayNames ( locale : string ) : DateTimeNames {
private getWeekdayNames ( locale : string ) : DateTimeNames {
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
if ( langCode == 'zh' ) {
@@ -1478,7 +1478,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
/ * *
* 获 取 月 份 名 称
* /
private getMonthNames ( locale : string ) : DateTimeNames {
private getMonthNames ( locale : string ) : DateTimeNames {
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
if ( langCode == 'zh' ) {
@@ -1502,7 +1502,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 带 千 分 位 分 隔 符 的 字 符 串
* /
private addThousandsSeparator ( numStr : string , locale : string ) : string {
private addThousandsSeparator ( numStr : string , locale : string ) : string {
const parts = numStr . split ( '.' ) ;
const integerPart = parts [ 0 ] ;
const decimalPart = parts . length > 1 ? parts [ 1 ] : '' ;
@@ -1560,7 +1560,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 分 隔 符 字 符
* /
private getThousandsSeparator ( locale : string ) : string {
private getThousandsSeparator ( locale : string ) : string {
// 提取语言代码前缀(如 'en-US' -> 'en')
const langCode = locale . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
@@ -1578,7 +1578,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 格 式 化 后 的 货 币 字 符 串
* /
private formatCurrency ( numStr : string , options : NumberFormatOpts , locale : string ) : string {
private formatCurrency ( numStr : string , options : NumberFormatOpts , locale : string ) : string {
const currency = options . currency ? ? 'USD' ;
const currencyDisplay = options . currencyDisplay ? ? 'symbol' ;
@@ -1688,7 +1688,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 格 式 化 后 的 货 币 字 符 串
* /
private getCurrencyPosition ( symbol : string , numStr : string , currency : string , locale : string ) : string {
private getCurrencyPosition ( symbol : string , numStr : string , currency : string , locale : string ) : string {
const currencyCode = currency . toUpperCase ( ) ;
// 符号在前面的货币(无空格)
@@ -1720,7 +1720,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param originalValue 原 始 数 值
* @returns 格 式 化 后 的 百 分 比 字 符 串
* /
private formatPercent ( numStr : string , originalValue : number ) : string {
private formatPercent ( numStr : string , originalValue : number ) : string {
// 将数值乘以100并添加百分号
const percentValue = originalValue * 100 ;
return ` ${ percentValue } % ` ;
@@ -1731,7 +1731,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 翻 译 消 息 或 null
* /
private getMessage ( key : string , locale : string ) : StringOrNull {
private getMessage ( key : string , locale : string ) : StringOrNull {
const messages = this . ops . messages ;
if ( messages [ locale ] == null ) {
return null ;
@@ -1747,22 +1747,22 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param path 路 径 , 如 'user.name'
* @returns 值 或 null
* /
private getNestedValue ( obj : UTSJSONObjectOrNull , path : string ) : StringOrNull {
private getNestedValue ( obj : UTSJSONObjectOrNull , path : string ) : StringOrNull {
const keys = path . split ( '.' ) ;
let getAny : GetAnyTypeOrNull = ( objdata : AnyOrNull ) : AnyOrNull = > {
let getAny : GetAnyTypeOrNull = ( objdata : AnyOrNull ) : AnyOrNull = > {
let realGetAny = getAny ! ;
let path : StringOrNull = keys . shift ( ) || null
let path : StringOrNull = keys . shift ( ) || null
if ( objdata == null ) return null ;
if (
typeof objdata == 'string' ||
typeof objdata == 'number'
) return objdata
if ( path == null ) return null ;
return realGetAny ( ( objdata ! as Record < string , any > ) ? . [ path ] ? ? null ) ;
return realGetAny ( ( objdata ! as Record < string , any > ) ? . [ path ] ? ? null ) ;
}
let current : AnyOrNull = getAny ( obj ) ;
let current : AnyOrNull = getAny ( obj ) ;
if ( typeof current == 'number' ) return current . toString ( )
return typeof current == 'string' ? current : null ;
@@ -1775,8 +1775,8 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param locale 语 言 代 码
* @returns 选 择 的 复 数 形 式
* /
private handlePlural ( message : string , count : number , locale : string ) : string {
const choices = message . split ( '|' ) . map ( ( choice : string ) : string = > choice . trim ( ) ) ;
private handlePlural ( message : string , count : number , locale : string ) : string {
const choices = message . split ( '|' ) . map ( ( choice : string ) : string = > choice . trim ( ) ) ;
const choicesLength = choices . length ;
// 获取复数规则
@@ -1811,12 +1811,12 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
* @param values 插 值 参 数
* @returns 插 值 后 的 字 符 串
* /
private interpolate ( message : string , values : UTSJSONObjectOrArray ) : string {
private interpolate ( message : string , values : UTSJSONObjectOrArray ) : string {
let msg = message ;
if ( Array . isArray ( values ) ) {
// 处理位置插值 {0}, {1}, {2}...
// #ifndef APP-ANDROID
msg = message . replace ( /\{(\d+)\}/g , ( match : string , key : string , index : number ) : string = > {
msg = message . replace ( /\{(\d+)\}/g , ( match : string , key : string , index : number ) : string = > {
const value = values [ parseInt ( key ) ] ;
if ( value != null ) {
@@ -1827,7 +1827,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
} ) ;
// #endif
// #ifdef APP-ANDROID
msg = message . replace ( /\{(\d+)\}/g , ( match : string , key : string , offset : number , str : string ) : string = > {
msg = message . replace ( /\{(\d+)\}/g , ( match : string , key : string , offset : number , str : string ) : string = > {
const value = values [ parseInt ( key ) ] ;
if ( value != null ) {
let result = typeof value == 'number' ? value . toString ( ) : value as string
@@ -1842,7 +1842,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
}
// 处理命名插值 {name}比如{count},{n}等
// #ifndef APP-ANDROID
msg = message . replace ( /\{([^}]+)\}/g , ( match : string , key : string , index : number ) : string = > {
msg = message . replace ( /\{([^}]+)\}/g , ( match : string , key : string , index : number ) : string = > {
const value = values [ key ] ;
if ( value != null ) {
return value as string ;
@@ -1851,7 +1851,7 @@ export class Tmui4xI18n implements Tmui4xI18nTml {
} ) ;
// #endif
// #ifdef APP-ANDROID
msg = message . replace ( /\{([^}]+)\}/g , ( match : string , key : string , offset : number , str : string ) : string = > {
msg = message . replace ( /\{([^}]+)\}/g , ( match : string , key : string , offset : number , str : string ) : string = > {
const value = values [ key ] ;
if ( value != null ) {
if ( typeof value == 'string' ) return value ;