Hướng dẫn locale python currency - tiền tệ python địa phương

Mã nguồn: lib/locale.py Lib/locale.py


Mô -đun locale mở quyền truy cập vào cơ sở dữ liệu và chức năng địa phương POSIX. Cơ chế địa phương POSIX cho phép các lập trình viên đối phó với một số vấn đề văn hóa trong một ứng dụng, mà không yêu cầu lập trình viên biết tất cả các chi tiết cụ thể của mỗi quốc gia nơi phần mềm được thực thi.

Mô -đun locale được triển khai trên đầu mô -đun _locale, từ đó sử dụng triển khai local ANSI C nếu có.

Mô -đun locale xác định ngoại lệ và chức năng sau:

ExceptionLocale.error¶locale.Error

Ngoại lệ được nâng lên khi địa phương được chuyển đến setlocale() không được công nhận.

Locale.setLocale (danh mục, locale = none) ¶setlocale(category, locale=None)

Nếu Locale được đưa ra và không phải None, setlocale() sẽ sửa đổi cài đặt Locale cho danh mục. Các danh mục có sẵn được liệt kê trong mô tả dữ liệu dưới đây. Locale có thể là một chuỗi, hoặc có thể lặp lại hai chuỗi (mã ngôn ngữ và mã hóa). Nếu nó có một sự khác biệt, thì nó đã chuyển đổi thành một tên địa phương bằng cách sử dụng động cơ bí danh địa phương. Một chuỗi trống chỉ định cài đặt mặc định của người dùng. Nếu việc sửa đổi địa phương thất bại, ngoại lệ Error sẽ được nâng lên. Nếu thành công, cài đặt địa phương mới được trả về.

Nếu Locale bị bỏ qua hoặc None, cài đặt hiện tại cho danh mục được trả về.

setlocale() không an toàn cho hầu hết các hệ thống. Các ứng dụng thường bắt đầu bằng một cuộc gọi

import locale
locale.setlocale(locale.LC_ALL, '')

Điều này đặt locale cho tất cả các danh mục thành cài đặt mặc định của người dùng (thường được chỉ định trong biến môi trường

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
2). Nếu địa điểm không thay đổi sau đó, việc sử dụng đa luồng sẽ không gây ra vấn đề.
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
2 environment variable). If the locale is not changed thereafter, using multithreading should not cause problems.

Locale.localeconv () ¶localeconv()

Trả về cơ sở dữ liệu của các quy ước địa phương như một từ điển. Từ điển này có các chuỗi sau làm khóa:

Loại

Chìa khóa

Nghĩa

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
4

Ký tự điểm thập phân.

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
5

Trình tự các số chỉ định vị trí tương đối mà

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
6 dự kiến. Nếu chuỗi bị chấm dứt với
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
7, không có nhóm nào được thực hiện thêm. Nếu chuỗi kết thúc với
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
8, kích thước nhóm cuối cùng được sử dụng nhiều lần.

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
6

Nhân vật được sử dụng giữa các nhóm.

locale0

locale1

Biểu tượng tiền tệ quốc tế.

locale2

Biểu tượng nội tệ.

locale3

Liệu ký hiệu tiền tệ có đi trước giá trị (đối với các giá trị âm dương tích cực).

locale4

Cho dù ký hiệu tiền tệ được tách ra khỏi giá trị bằng một không gian (cho các giá trị âm dương tích cực).

locale5

Điểm thập phân được sử dụng cho các giá trị tiền tệ.

locale6

Số lượng các chữ số phân số được sử dụng trong định dạng cục bộ của các giá trị tiền tệ.

locale7

Số lượng các chữ số phân số được sử dụng trong định dạng quốc tế của các giá trị tiền tệ.

locale8

Phân tách nhóm được sử dụng cho các giá trị tiền tệ.

locale9

Tương đương với

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
5, được sử dụng cho các giá trị tiền tệ.

locale1

Biểu tượng được sử dụng để chú thích giá trị tiền tệ tích cực.

locale2

Biểu tượng được sử dụng để chú thích giá trị tiền tệ âm.

locale3

Vị trí của dấu hiệu (cho các giá trị tiêu cực tích cực), xem bên dưới.

Tất cả các giá trị số có thể được đặt thành

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
7 để chỉ ra rằng không có giá trị được chỉ định trong ngôn ngữ này.

Các giá trị có thể cho locale5 và locale6 được đưa ra dưới đây.

Giá trị

Giải trình

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
8

Tiền tệ và giá trị được bao quanh bởi dấu ngoặc đơn.

locale8

Dấu hiệu sẽ đi trước giá trị và ký hiệu tiền tệ.

locale9

Dấu hiệu nên theo biểu tượng giá trị và tiền tệ.

_locale0

Dấu hiệu phải ngay trước giá trị.

_locale1

Dấu hiệu nên ngay lập tức làm theo giá trị.

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
7

Không có gì được chỉ định trong địa phương này.

Hàm tạm thời đặt địa phương _locale3 thành ngôn ngữ

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3 hoặc địa phương locale0 nếu các địa phương là khác nhau và các chuỗi tiền tệ hoặc tiền tệ là không ASCII. Thay đổi tạm thời này ảnh hưởng đến các chủ đề khác.

Đã thay đổi trong phiên bản 3.7: Hàm hiện tạm thời đặt locale _locale3 thành ngôn ngữ

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3 trong một số trường hợp.The function now temporarily sets the _locale3 locale to the
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3 locale in some cases.

Locale.nl_langinfo (tùy chọn) ¶nl_langinfo(option)

Trả về một số thông tin cụ thể địa phương dưới dạng chuỗi. Hàm này không có sẵn trên tất cả các hệ thống và tập hợp các tùy chọn có thể cũng có thể thay đổi giữa các nền tảng. Các giá trị đối số có thể là các số, trong đó các hằng số biểu tượng có sẵn trong mô -đun địa phương.

Hàm _locale8 chấp nhận một trong các khóa sau. Hầu hết các mô tả được lấy từ mô tả tương ứng trong thư viện GNU C.

Locale.Codeset¶CODESET

Nhận một chuỗi có tên của mã hóa ký tự được sử dụng trong ngôn ngữ đã chọn.

Locale.D_T_FMT¶D_T_FMT

Nhận một chuỗi có thể được sử dụng làm chuỗi định dạng cho _locale9 để biểu diễn ngày và thời gian theo cách cụ thể của ngôn ngữ.

Locale.D_FMT¶D_FMT

Nhận một chuỗi có thể được sử dụng làm chuỗi định dạng cho _locale9 để biểu diễn một ngày theo cách cụ thể của ngôn ngữ.

Locale.T_FMT¶T_FMT

Nhận một chuỗi có thể được sử dụng làm chuỗi định dạng cho _locale9 để biểu diễn một thời gian theo cách cụ thể của ngôn ngữ.

Locale.T_FMT_AMPM¶T_FMT_AMPM

Nhận một chuỗi định dạng cho _locale9 để biểu diễn thời gian ở định dạng AM/pm.

DAY_1...DAY_7 ... DAY_7

Lấy tên của ngày thứ n trong tuần.

Ghi chú

Điều này theo Công ước Hoa Kỳ về locale3 là Chủ nhật, chứ không phải Công ước quốc tế (ISO 8601) rằng thứ Hai là ngày đầu tiên của tuần.

ABDAY_1...ABDAY_7 ... ABDAY_7

Nhận tên viết tắt của ngày thứ n trong tuần.

MON_1...MON_12 ... MON_12

Lấy tên của tháng n.

ABMON_1...ABMON_12 ... ABMON_12

Nhận tên viết tắt của tháng n.

Locale.Radixchar¶RADIXCHAR

Nhận ký tự radix (dấu chấm thập phân, dấu phẩy thập phân, v.v.).

Locale.ThouseP¶THOUSEP

Nhận ký tự phân tách cho hàng ngàn (nhóm ba chữ số).

Locale.yesexpr¶YESEXPR

Nhận một biểu thức thông thường có thể được sử dụng với hàm regex để nhận ra phản hồi tích cực cho câu hỏi có/không.

Locale.NoExpr¶NOEXPR

Nhận một biểu thức thông thường có thể được sử dụng với hàm regex (3) để nhận ra phản hồi tiêu cực đối với câu hỏi có/không.

Ghi chú

Điều này theo Công ước Hoa Kỳ về locale3 là Chủ nhật, chứ không phải Công ước quốc tế (ISO 8601) rằng thứ Hai là ngày đầu tiên của tuần.

Nhận tên viết tắt của ngày thứ n trong tuần.CRNCYSTR

Lấy tên của tháng n.

Nhận tên viết tắt của tháng n.ERA

Locale.Radixchar¶

Nhận ký tự radix (dấu chấm thập phân, dấu phẩy thập phân, v.v.).

Locale.ThouseP¶

Nhận ký tự phân tách cho hàng ngàn (nhóm ba chữ số).ERA_D_T_FMT

Locale.yesexpr¶

Nhận một biểu thức thông thường có thể được sử dụng với hàm regex để nhận ra phản hồi tích cực cho câu hỏi có/không.ERA_D_FMT

Locale.NoExpr¶

Nhận một biểu thức thông thường có thể được sử dụng với hàm regex (3) để nhận ra phản hồi tiêu cực đối với câu hỏi có/không.ERA_T_FMT

Các biểu thức chính quy cho cú pháp locale4 và locale5 sử dụng cú pháp phù hợp cho hàm locale6 từ thư viện C, có thể khác với cú pháp được sử dụng trong locale7.

Locale.crncystr¶ALT_DIGITS

Nhận biểu tượng tiền tệ, trước đó là--Nếu biểu tượng sẽ xuất hiện trước giá trị, thì++nếu biểu tượng sẽ xuất hiện sau giá trị, hoặc. Nếu biểu tượng nên thay thế ký tự radix.

Locale.era¶getdefaultlocale([envvars])

Nhận một chuỗi đại diện cho ERA được sử dụng trong ngôn ngữ hiện tại.

Hầu hết các địa phương không xác định giá trị này. Một ví dụ về một địa điểm xác định giá trị này là giá trị Nhật Bản. Tại Nhật Bản, đại diện truyền thống của ngày bao gồm tên của thời đại tương ứng với triều đại của hoàng đế.

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
2 variable. Since we do not want to interfere with the current locale setting we thus emulate the behavior in the way described above.

Thông thường không cần thiết phải sử dụng giá trị này trực tiếp. Chỉ định công cụ sửa đổi locale8 trong chuỗi định dạng của chúng khiến hàm _locale9 sử dụng thông tin này. Định dạng của chuỗi được trả về không được chỉ định và do đó bạn không nên cho rằng kiến ​​thức về nó trên các hệ thống khác nhau.

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
2 variable is tested, but a list of variables given as envvars parameter. The first found to be defined will be used. envvars defaults to the search path used in GNU gettext; it must always contain the variable name setlocale()9. The GNU gettext search path contains None0, None1, setlocale()9 and None3, in that order.

Locale.era_d_t_fmt¶RFC 1766. language code and encoding may be None if their values cannot be determined.

Nhận một chuỗi định dạng cho _locale9 để biểu diễn ngày và thời gian theo cách dựa trên thời đại địa phương.

Locale.era_d_fmt¶getlocale(category=LC_CTYPE)

Nhận một chuỗi định dạng cho _locale9 để biểu diễn một ngày theo cách dựa trên thời đại địa phương.

Locale.era_d_t_fmt¶RFC 1766. language code and encoding may be None if their values cannot be determined.

Nhận một chuỗi định dạng cho _locale9 để biểu diễn ngày và thời gian theo cách dựa trên thời đại địa phương.getpreferredencoding(do_setlocale=True)

Locale.era_d_fmt¶locale encoding used for text data, according to user preferences. User preferences are expressed differently on different systems, and might not be available programmatically on some systems, so this function only returns a guess.

Nhận một chuỗi định dạng cho _locale9 để biểu diễn một ngày theo cách dựa trên thời đại địa phương.

Locale.era_t_fmt¶Python UTF-8 Mode is enabled, always return setlocale()3, the locale encoding and the do_setlocale argument are ignored.

Các tiền đề Python định cấu hình địa phương LC_CTYPE. Xem thêm mã hóa hệ thống tập tin và xử lý lỗi.Python preinitialization configures the LC_CTYPE locale. See also the filesystem encoding and error handler.

Đã thay đổi trong phiên bản 3.7: Hàm bây giờ luôn trả về setlocale()4 trên Android hoặc nếu chế độ Python UTF-8 được bật.The function now always returns setlocale()4 on Android or if the Python UTF-8 Mode is enabled.

locale.getencoding () ¶getencoding()

Nhận mã hóa ngôn ngữ hiện tại:locale encoding:

  • Trên Android và VxWorks, trả lại setlocale()4.

  • Trên UNIX, trả lại mã hóa của địa phương _locale3 hiện tại. Trả về setlocale()4 Nếu setlocale()8 trả về một chuỗi trống: Ví dụ: nếu locale lc_ctype hiện tại không được hỗ trợ.

  • Trên Windows, trả lại trang mã ANSI.

Các tiền đề Python định cấu hình địa phương LC_CTYPE. Xem thêm mã hóa hệ thống tập tin và xử lý lỗi.Python preinitialization configures the LC_CTYPE locale. See also the filesystem encoding and error handler.

Hàm này tương tự như setlocale()9 ngoại trừ hàm này bỏ qua chế độ UTF-8 Python.Python UTF-8 Mode.

Mới trong phiên bản 3.11.

Locale.Normalize (LocalEname) ¶normalize(localename)

Trả về một mã địa phương được chuẩn hóa cho tên địa phương đã cho. Mã địa phương được trả về được định dạng để sử dụng với setlocale(). Nếu chuẩn hóa thất bại, tên ban đầu được trả về không thay đổi.

Nếu mã hóa đã cho không được biết, hàm mặc định là mã hóa mặc định cho mã locale giống như setlocale().

Locale.ResetLocale (Thể loại = LC_ALL) ¶resetlocale(category=LC_ALL)

Đặt Locale cho danh mục thành cài đặt mặc định.

Cài đặt mặc định được xác định bằng cách gọi Error2. Danh mục mặc định là None7.

Không dùng nữa kể từ phiên bản 3.11, sẽ bị xóa trong phiên bản 3.13.

Locale.strcoll (String1, String2) ¶strcoll(string1, string2)

So sánh hai chuỗi theo cài đặt Error4 hiện tại. Như bất kỳ hàm so sánh nào khác, trả về một giá trị âm hoặc giá trị dương hoặc

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
8, tùy thuộc vào việc String1 đối chiếu trước hay sau chuỗi2 hay bằng với nó.

Locale.strxfrm (Chuỗi) ¶strxfrm(string)

Chuyển đổi một chuỗi thành một chuỗi có thể được sử dụng trong các so sánh nhận biết địa phương. Ví dụ, Error6 tương đương với Error7. Hàm này có thể được sử dụng khi cùng một chuỗi được so sánh nhiều lần, ví dụ: Khi đối chiếu một chuỗi các chuỗi.

locale.format_string (định dạng, val, nhóm = false, tiền tệ = false) ¶format_string(format, val, grouping=False, monetary=False)

Định dạng một số Val theo cài đặt

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3 hiện tại. Định dạng tuân theo các quy ước của toán tử Error9. Đối với các giá trị điểm nổi, điểm thập phân được sửa đổi nếu thích hợp. Nếu nhóm là
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
00, cũng sẽ tính đến việc nhóm.

Nếu tiền tệ là đúng, việc chuyển đổi sử dụng các chuỗi hàng ngàn tiền tệ và chuỗi nhóm.

Các quy trình định dạng các nhà xác định như trong

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
01, nhưng tính đến các cài đặt địa phương hiện tại.

Thay đổi trong phiên bản 3.7: Tham số từ khóa tiền tệ đã được thêm vào.The monetary keyword parameter was added.

locale.format (định dạng, val, nhóm = false, tiền tệ = false) ¶format(format, val, grouping=False, monetary=False)

Xin lưu ý rằng chức năng này hoạt động như

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
02 nhưng sẽ chỉ hoạt động cho chính xác một trình xác định
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
03. Ví dụ,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
04 và
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
05 đều là các nhà xác định hợp lệ, nhưng
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
06 thì không.

Đối với toàn bộ chuỗi định dạng, sử dụng

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
02.

Locale.Currency (Val, Symbol = true, nhóm = false, international = false) ¶currency(val, symbol=True, grouping=False, international=False)

Định dạng một số Val theo cài đặt locale0 hiện tại.

Chuỗi được trả về bao gồm ký hiệu tiền tệ nếu biểu tượng là đúng, đó là mặc định. Nếu nhóm là

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
00 (không phải là mặc định), nhóm được thực hiện với giá trị. Nếu quốc tế là
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
00 (không phải là mặc định), biểu tượng tiền tệ quốc tế được sử dụng.

Ghi chú

Chức năng này sẽ không hoạt động với ngôn ngữ ‘C, vì vậy bạn phải đặt một locale qua setlocale() trước.

Locale.str (Float) ¶str(float)

Các định dạng một số điểm nổi sử dụng cùng định dạng với hàm tích hợp

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
12, nhưng tính đến điểm thập phân.

locale.delocalize (chuỗi) ¶delocalize(string)

Chuyển đổi một chuỗi thành một chuỗi số được chuẩn hóa, theo cài đặt

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3.

Mới trong phiên bản 3.5.

locale.locize (chuỗi, nhóm = false, tiền tệ = false) ¶localize(string, grouping=False, monetary=False)

Chuyển đổi một chuỗi số được chuẩn hóa thành một chuỗi được định dạng theo cài đặt

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3.

Mới trong phiên bản 3.10.

Locale.atof (String, func = float) ¶atof(string, func=float)

Chuyển đổi một chuỗi thành một số, theo cài đặt

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3, bằng cách gọi func về kết quả gọi
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
16 trên chuỗi.

Locale.atoi (Chuỗi) ¶atoi(string)

Chuyển đổi một chuỗi thành một số nguyên, theo các quy ước

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
3.

Locale.lc_ctype¶LC_CTYPE

Danh mục địa phương cho các hàm loại ký tự. Tùy thuộc vào cài đặt của danh mục này, các chức năng của mô -đun

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
18 xử lý trường hợp thay đổi hành vi của chúng.

Locale.lc_collate¶LC_COLLATE

Thể loại địa phương để phân loại chuỗi. Các chức năng

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
19 và
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
20 của mô -đun locale bị ảnh hưởng.

Locale.lc_time¶LC_TIME

Thể loại địa phương cho định dạng thời gian. Hàm _locale9 tuân theo các quy ước này.

Locale.lc_monetary¶LC_MONETARY

Thể loại địa phương để định dạng các giá trị tiền tệ. Các tùy chọn có sẵn có sẵn từ chức năng

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
23.

Locale.LC_Messages¶LC_MESSAGES

Danh mục địa phương cho hiển thị tin nhắn. Python hiện không hỗ trợ các tin nhắn nhận biết địa phương cụ thể của ứng dụng. Các tin nhắn được hiển thị bởi hệ điều hành, giống như các tin nhắn được trả về bởi

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
24 có thể bị ảnh hưởng bởi danh mục này.

Locale.lc_numeric¶LC_NUMERIC

Danh mục địa phương để định dạng số. Các hàm

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
25,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
26,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
27 và
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
28 của mô -đun locale bị ảnh hưởng bởi danh mục đó. Tất cả các hoạt động định dạng số khác không bị ảnh hưởng.

Locale.lc_all¶LC_ALL

Kết hợp tất cả các cài đặt địa phương. Nếu cờ này được sử dụng khi địa điểm được thay đổi, hãy đặt địa điểm cho tất cả các danh mục được thử. Nếu điều đó thất bại cho bất kỳ danh mục nào, không có danh mục nào được thay đổi. Khi ngôn ngữ được truy xuất bằng cờ này, một chuỗi chỉ ra cài đặt cho tất cả các danh mục được trả về. Chuỗi này sau đó có thể được sử dụng để khôi phục cài đặt.

Locale.char_max¶CHAR_MAX

Đây là một hằng số tượng trưng được sử dụng cho các giá trị khác nhau được trả về bởi

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
23.

Example:

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale

Bối cảnh, chi tiết, gợi ý, lời khuyên và cảnh báo

Tiêu chuẩn C định nghĩa Locale là một thuộc tính toàn chương trình có thể tương đối tốn kém để thay đổi. Trên hết, một số triển khai bị phá vỡ theo cách mà các thay đổi địa phương thường xuyên có thể gây ra các bãi rác lõi. Điều này làm cho địa điểm hơi đau đớn khi sử dụng chính xác.

Ban đầu, khi một chương trình được bắt đầu, ngôn ngữ là địa phương

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
31, bất kể người dùng ưa thích là địa phương là gì. Có một ngoại lệ: danh mục _locale3 được thay đổi khi khởi động để đặt mã hóa ngôn ngữ hiện tại cho mã hóa ngôn ngữ ưa thích của người dùng. Chương trình phải nói rõ ràng rằng nó muốn cài đặt địa phương ưa thích của người dùng cho các danh mục khác bằng cách gọi setlocale()4.

Nói chung, đó là một ý tưởng tồi để gọi setlocale() trong một số thói quen thư viện, vì là một tác dụng phụ, nó ảnh hưởng đến toàn bộ chương trình. Tiết kiệm và khôi phục nó gần như xấu: nó đắt tiền và ảnh hưởng đến các chủ đề khác xảy ra trước khi cài đặt đã được khôi phục.

Nếu, khi mã hóa một mô -đun để sử dụng chung, bạn cần một phiên bản độc lập địa phương của một hoạt động bị ảnh hưởng bởi locale (chẳng hạn như các định dạng nhất định được sử dụng với _locale9), bạn sẽ phải tìm cách thực hiện mà không cần sử dụng thư viện tiêu chuẩn lịch trình. Thậm chí tốt hơn là thuyết phục bản thân rằng sử dụng cài đặt địa phương là ổn. Chỉ như là phương sách cuối cùng, bạn mới ghi nhận rằng mô-đun của bạn không tương thích với các cài đặt địa phương không phải là -____ 131.

Cách duy nhất để thực hiện các hoạt động số theo ngôn ngữ là sử dụng các hàm đặc biệt được xác định bởi mô -đun này:

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
27,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
26,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
25,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
28.

Không có cách nào để thực hiện chuyển đổi trường hợp và phân loại ký tự theo địa phương. Đối với các chuỗi văn bản (unicode), chúng chỉ được thực hiện theo giá trị ký tự, trong khi đối với chuỗi byte, các chuyển đổi và phân loại được thực hiện theo giá trị ASCII của byte và byte có bit cao được đặt (tức là, không ) không bao giờ được chuyển đổi hoặc được coi là một phần của một lớp ký tự như chữ cái hoặc khoảng trắng.

Cho các nhà văn và chương trình mở rộng nhúng Python¶

Các mô -đun mở rộng không bao giờ nên gọi setlocale(), ngoại trừ để tìm hiểu địa phương hiện tại là gì. Nhưng vì giá trị trả lại chỉ có thể được sử dụng một cách xác định để khôi phục nó, điều đó không hữu ích lắm (ngoại trừ có lẽ để tìm hiểu xem địa phương có phải là

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
31).

Khi mã Python sử dụng mô -đun locale để thay đổi ngôn ngữ, điều này cũng ảnh hưởng đến ứng dụng nhúng. Nếu ứng dụng nhúng không muốn điều này xảy ra, nó sẽ xóa mô-đun mở rộng _locale (thực hiện tất cả các công việc) khỏi bảng các mô-đun tích hợp trong tệp

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
45 và đảm bảo rằng mô-đun _locale không thể truy cập được một thư viện được chia sẻ.

Truy cập vào Danh mục Tin nhắn

locale.gettext (msg) ¶ Locale.DgetText (tên miền, tin nhắn) ¶ Locale.DcgetText (tên miền, tin nhắn, danh mục) ¶ Locale.TextDomain (miền)gettext(msg)locale.dgettext(domain, msg)locale.dcgettext(domain, msg, category)locale.textdomain(domain)locale.bindtextdomain(domain, dir)

Mô -đun Locale hiển thị giao diện GetText thư viện C trên các hệ thống cung cấp giao diện này. Nó bao gồm các chức năng

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
47,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
48,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
49,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
50,
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
51 và
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
52. Chúng tương tự như các chức năng tương tự trong mô -đun
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
53, nhưng sử dụng định dạng nhị phân của thư viện C cho các danh mục tin nhắn và các thuật toán tìm kiếm thư viện C để định vị các danh mục tin nhắn.

Các ứng dụng Python thường không cần phải gọi các chức năng này và nên sử dụng

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
53 thay thế. Một ngoại lệ đã biết đối với quy tắc này là các ứng dụng liên kết với các thư viện C bổ sung để gọi nội bộ
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
47 hoặc
>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
49. Đối với các ứng dụng này, có thể cần phải liên kết miền văn bản, để các thư viện có thể định vị đúng danh mục tin nhắn của họ.