Locale QML Type
提供本地特定属性和格式化数据。更多
| Import Statement: | import QtQml |
属性
- amText : string
- decimalPoint : string
- exponential : string
- firstDayOfWeek : enumeration
- groupSeparator : string
- measurementSystem : enumeration
- name : string
- nativeLanguageName : string
- nativeTerritoryName : string
- negativeSign : string
- numberOptions : enumeration
- percent : string
- pmText : string
- positiveSign : string
- textDirection : enumeration
- uiLanguages : Array<string>
- weekDays : Array<int>
- zeroDigit : string
方法
- string createSeparatedList(list<string> list)
(since 6.10) - string currencySymbol(format)
- string dateFormat(type)
- string dateTimeFormat(type)
- string dayName(day, type)
- string formattedDataSize(int bytes, int precision, DataSizeFormat format)
(since 6.2) - string monthName(month, type)
- string standaloneDayName(day, type)
- string standaloneMonthName(month, type)
- string timeFormat(type)
- string toString(int i)
(since 6.5) - string toString(Date date, FormatType format)
(since 6.5) - string toString(Date date, string format)
(since 6.5) - string toString(double f, char format, int precision)
(since 6.5)
详细说明
Locale 对象只能通过Qt.locale() 函数创建。不能直接创建。
Qt.locale() 函数返回一个 JS Locale 对象,该对象代表具有指定名称的本地语言,其格式为 "language[_territory][.codeset][@modifier]"或 "C"。
Locale 支持默认 locale 的概念,默认 locale 由应用程序启动时的系统 locale 设置决定。如果没有向 Qt.locale() 传递参数,则会返回默认 locale 对象。
Locale 对象提供了大量函数和属性,为指定的本地语言提供数据。
还可将 Locale 对象传递给日期和数字toLocaleString() 和 fromLocaleString() 方法,以便使用指定的本地语言转换字符串。
本示例显示了按德语本地语言格式化的当前日期:
import QtQuick 2.0 Text { text: "The date is: " + Date().toLocaleString(Qt.locale("de_DE")) }
下面的示例以默认本地语言的正确格式显示指定的数字:
import QtQuick 2.0 Text { text: "The value is: " + Number(23443.34).toLocaleString(Qt.locale()) }
Qt Quick Locale 的数据基于 Common Locale Data Repository v1.8.1。
本地字符串格式类型
monthName(),standaloneMonthName(),dayName() 和standaloneDayName() 可以使用以下枚举值来指定日期对象字符串表示的格式。
| 常量 | 说明 |
|---|---|
Locale.LongFormat | 日期和月份名称的长版本;例如,返回 "1 月 "作为月份名称。 |
Locale.ShortFormat | 日期和月份名称的简短版本;例如,返回 "Jan "作为月份名称。 |
Locale.NarrowFormat | 在空间有限的情况下使用的特殊格式的星期和月份名称;例如,返回 "J "作为月份名称。请注意,窄格式可能包含不同月份和日期的相同文本,如果本地语言不支持窄名称,窄格式甚至可能是空字符串,因此应避免将其用于日期格式化。此外,在系统语言中,该格式与 ShortFormat 相同。 |
此外,双字符串到字符串和字符串到双字符串的转换功能也包含在以下许可证中:
版权所有 (c) 1991 AT&T。
特此允许为任何目的使用、复制、修改和分发本软件而无需支付任何费用,条件是本通知必须包含在作为或包括本软件的复制或修改的任何软件的所有副本中,以及这些软件的支持文档的所有副本中。
本软件按 "原样 "提供,没有任何明示或暗示的保证。特别是,作者和 At&T 公司均不对本软件的适销性或对任何特定用途的适用性作任何陈述或保证。
本产品包含由加州大学伯克利分校及其贡献者开发的软件。
另请参阅 日期 JavaScript 对象和数字 JavaScript 对象。
属性文档
amText : string
AM" 后缀的本地化名称,用于按 12 小时时钟惯例指定的时间。
decimalPoint : string
保存该语言的小数点字符。
exponential : string
保存此本地语言的指数字符。
firstDayOfWeek : enumeration
根据当前地区保存一周的第一天。
| 常量 | 说明 |
|---|---|
Locale.Sunday | 0 |
Locale.Monday | 1 |
Locale.Tuesday | 2 |
Locale.Wednesday | 3 |
Locale.Thursday | 4 |
Locale.Friday | 5 |
Locale.Saturday | 6 |
注意: 这些值符合 JS Date API,与 Qt XML C++ API 中的Qt::Sunday = 7 不同。
groupSeparator : string
保存此本地语言的组分隔符。
measurementSystem : enumeration
该属性定义了用于测量的单位。
| 常数 | 说明 |
|---|---|
Locale.MetricSystem | 该值表示公制单位,如米、厘米和毫米。 |
Locale.ImperialUSSystem | 该值表示英制单位,如美国使用的英寸和英里。 |
Locale.ImperialUKSystem | 该值表示英制单位,如英国使用的英寸和英里。 |
Locale.ImperialSystem | 提供兼容性。与 Locale.ImperialUSSystem 相同。 |
name : string
以字符串 "language_territory "的形式保存本地语言和地域,其中 language 是小写的双字母 ISO 639 语言代码,territory 是大写的双或三字母 ISO 3166 地域代码。
nativeLanguageName : string
本地语言的本地名称。例如,"Schwiizertüütsch "表示瑞士德语区。
另请参阅 nativeTerritoryName 。
nativeTerritoryName : string
保存地域的本地名称。例如,"España "表示西班牙语/西班牙语地区。
另请参阅 nativeLanguageName 。
negativeSign : string
表示该地域的负号字符。
numberOptions : enumeration
包含一组数字到字符串和字符串到数字的转换选项。
另请参阅 string Number::toLocaleString(locale, format, precision)和string Number::fromLocaleString(locale, number)。
percent : string
表示该位置的百分号字符。
pmText : string
PM "后缀的本地化名称,用于按 12 小时时钟惯例指定的时间。
positiveSign : string
保存该位置的正符号字符。
textDirection : enumeration
保持语言的文本方向:
| 常量 | 说明 |
|---|---|
Qt.LeftToRight | 文本通常从左侧开始。 |
Qt.RightToLeft | 文本通常从右侧开始。 |
uiLanguages : Array<string>
按偏好顺序返回用于翻译的本地名称有序列表。
返回值代表用户希望看到用户界面翻译的语言名称。
列表中的第一项是最优先选择的。
weekDays : Array<int>
保存一个数组,数组中的天数在当前地区被视为周日,其中周日为 0,周六为 6。
另请参阅 firstDayOfWeek 。
zeroDigit : string
持有 返回此本地语言的零位字符。
方法文档
[since 6.10] string createSeparatedList(list<string> list)
返回一个字符串,该字符串表示用 locale 定义的分隔符连接给定list 的字符串。
此方法在 Qt 6.10 中引入。
另请参阅 QLocale::createSeparatedList()。
string currencySymbol(format)
返回指定format 的货币符号:
| 常量 | 说明 |
|---|---|
Locale.CurrencyIsoCode | 货币的 ISO-4217 代码。 |
Locale.CurrencySymbol | 货币符号。 |
Locale.CurrencyDisplayName | 用户可读的货币名称。 |
另请参阅 字符串 Number::toLocaleCurrencyString(locale, symbol)。
string dateFormat(type)
type 指定要返回的FormatType 。
另请参阅 日期 JavaScript 对象。
string dateTimeFormat(type)
type 指定要返回的FormatType 。
另请参阅 日期 JavaScript 对象。
string dayName(day, type)
在type 指定的可选FormatType 中,返回day 的本地化名称(其中 0 代表星期天,1 代表星期一,依此类推)。
另请参阅 monthName() 和standaloneDayName()。
[since 6.2] string formattedDataSize(int bytes, int precision, DataSizeFormat format)
将bytes 中的大小转换为人类可读的本地化字符串,其中包括一个数字和一个量化单位。
precision 和format 参数为可选参数。
更多信息,请参阅QLocale::formattedDataSize() 。
此方法在 Qt 6.2 中引入。
另请参阅 QLocale::DataSizeFormats 。
string monthName(month, type)
在type 指定的可选FormatType 中,返回month 的本地化名称(0-11)。
注意: QLocale C++ API 期望的范围是(1-12),而 Locale.monthName() 期望的范围是 0-11,与 JS Date 对象一致。
另请参阅 dayName() 和standaloneMonthName()。
string standaloneDayName(day, type)
在type 指定的FormatType 中,返回作为独立文本使用的day 的本地化名称(其中 0 代表星期日,1 代表星期一,依此类推)。
如果本地信息未指定独立日名称,则返回值与dayName() 中的相同。
另请参阅 dayName() 和standaloneMonthName()。
string standaloneMonthName(month, type)
在type 指定的可选FormatType 中,返回作为独立文本使用的month (0-11)的本地化名称。
如果本地信息未指定独立月份名称,则返回值与monthName() 中的相同。
注意: QLocale C++ API 期望的范围是(1-12),而 Locale.standaloneMonthName() 期望的范围是 0-11,与 JS Date 对象一致。
另请参阅 monthName() 和standaloneDayName()。
string timeFormat(type)
type 指定要返回的FormatType 。
另请参阅 日期 JavaScript 对象。
[since 6.5] string toString(int i)
返回i 的本地化字符串表示。
此方法在 Qt 6.5 中引入。
另请参阅 QLocale::toString(int)。
[since 6.5] string toString(Date date, FormatType format = LongFormat)
以指定的format 返回给定date 的本地化字符串表示。如果省略format ,则使用Locale.LongFormat 。
这是一个重载函数。
此方法在 Qt 6.5 中引入。
另请参阅 QLocale::toString(QDate, QLocale::FormatType).
[since 6.5] string toString(Date date, string format)
以指定的format 返回给定date 的本地化字符串表示。如果format 是空字符串,则返回空字符串。
这是一个重载函数。
此方法在 Qt 6.5 中引入。
另请参阅 QLocale::toString(QDate, QStringView)。
[since 6.5] string toString(double f, char format = 'g', int precision = 6)
返回表示浮点数f 的字符串。
表示形式由可选的format 和precision 参数控制。
更多信息请参见QLocale::toString(double, char, int)。
这是一个重载函数。
该方法在 Qt 6.5 中引入。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.