Locale QML Type

提供本地特定属性和格式化数据。更多

Import Statement: import QtQml

属性

方法

详细说明

Locale 对象只能通过Qt.locale() 函数创建。不能直接创建。

Qt.locale() 函数返回一个 JS Locale 对象,该对象代表具有指定名称的本地语言,其格式为 "language[_territory][.codeset][@modifier]"或 "C"。

Locale 支持默认 locale 的概念,默认 locale 由应用程序启动时的系统 locale 设置决定。如果没有向 Qt.locale() 传递参数,则会返回默认 locale 对象。

Locale 对象提供了大量函数和属性,为指定的本地语言提供数据。

Locale 对象还可以传递给DateNumber 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 公司均不对本软件的适销性或对任何特定用途的适用性作任何陈述或保证。

本产品包含加州大学伯克利分校及其贡献者开发的软件。

另请参阅 DateNumber

属性文档

amText : string

AM" 后缀的本地化名称,用于使用 12 小时时钟惯例指定的时间。


decimalPoint : string

包含该本地的小数点字符。


exponential : string

该地区的指数字符。


firstDayOfWeek : enumeration

根据当前语言保存一周的第一天。

常数说明
Locale.Sunday0
Locale.Monday1
Locale.Tuesday2
Locale.Wednesday3
Locale.Thursday4
Locale.Friday5
Locale.Saturday6

注意: 这些值符合 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 "的形式保存此locale的语言和地域,其中language是小写的两个字母的ISO 639语言代码,territory是大写的两个或三个字母的ISO 3166地域代码。


nativeLanguageName : string

表示本地语言的本地名称。例如,"Schwiizertüütsch "表示瑞士-德语地区。

另请参阅 nativeTerritoryName


nativeTerritoryName : string

本地区域的本地名称。例如,"España "表示西班牙语/西班牙语区域。

另请参阅 nativeLanguageName


negativeSign : string

表示该地区的负号字符。


numberOptions : enumeration

持有一组用于数字到字符串和字符串到数字转换的选项。

另请参阅 Number::toLocaleString() 和Number::fromLocaleString()。


percent : string

保存此本地语言的百分号字符。


pmText : string

PM "后缀的本地化名称,用于使用 12 小时时钟约定指定的时间。


positiveSign : string

表示该地区的正号字符。


textDirection : enumeration

表示语言的文本方向:

常量说明
Qt.LeftToRight文本通常从左侧开始。
Qt.RightToLeft文本通常从右侧开始。

uiLanguages : Array<string>

按偏好顺序返回用于翻译的本地名称有序列表。

返回值代表用户希望看到 UI 翻译的本地名称。

列表中的第一项为首选项。


weekDays : Array<int>

保存一个数组,其中包含根据当前本地语言被视为周日的天数,其中周日为 0,周六为 6。

另请参阅 firstDayOfWeek


zeroDigit : string

Holds 返回此本地语言的零位字符。


方法文档

string currencySymbol(format)

返回指定format 的货币符号:

常量说明
Locale.CurrencyIsoCode货币的 ISO-4217 代码。
Locale.CurrencySymbol货币符号。
Locale.CurrencyDisplayName用户可读的货币名称。

另请参阅 Number::toLocaleCurrencyString()。


string dateFormat(type)

type 指定要返回的FormatType

另请参阅 Date


string dateTimeFormat(type)

type 指定要返回的FormatType

另请参阅 Date


string dayName(day, type)

type 指定的可选FormatType 中,返回day 的本地化名称(其中 0 代表星期日,1 代表星期一,依此类推)。

另请参阅 monthName() 和standaloneDayName()。


[since 6.2] string formattedDataSize(int bytes, int precision, DataSizeFormat format)

bytes 中的大小转换为人类可读的本地化字符串,其中包括一个数字和一个量化单位。

precisionformat 参数为可选参数。

更多信息,请参阅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)

返回day 的本地化名称(其中 0 代表星期日,1 代表星期一,以此类推),该名称在type 指定的FormatType 中用作独立文本。

如果本地信息未指定独立日名称,则返回值与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

另请参阅 Date


[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 的字符串。

表示形式由可选的formatprecision 参数控制。

更多信息请参阅QLocale::toString(double, char, int)。

此方法在 Qt 6.5 中引入。


© 2025 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.