python / python/cpython

Migrate to modern win32/winrt API for `localemodule`

Đang mở
#144,728 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

extension-modules OS-windows type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Feature or enhancement

Proposal:

AFAIK, CPython has dropped support for Windows 7 since 3.9.0, while _localemodule.c contains compatibility fix for Windows NT pre-4.0. Which flag was introduced since Windows 98.

Here I listed some modern way to get locale name in BCP-47 / RFC 1766 format, is it possible to replace the legacy hex code mapping?

import ctypes
from ctypes import wintypes
from locale import getdefaultlocale, getlocale, LC_CTYPE
import warnings

# Introduced since Windows 10, BCP-47
# depends on:
# - winrt-windows-system-userprofile
# - winrt-windows-foundation
# - winrt-windows-foundation-collections
from winrt.windows.system.userprofile import GlobalizationPreferences

warnings.filterwarnings("ignore", category=DeprecationWarning)

# Load kernel32.dll
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)

# Introduced since Windows Vista, RFC 1766
# Define GetUserDefaultLocaleName signature
# int GetUserDefaultLocaleName(LPWSTR lpLocaleName, int cchLocaleName);
kernel32.GetUserDefaultLocaleName.argtypes = [wintypes.LPWSTR, ctypes.c_int]
kernel32.GetUserDefaultLocaleName.restype = ctypes.c_int

# Allocate buffer for locale name
LOCALE_NAME_MAX_LENGTH = 85  # per Windows docs


def user_default_locale() -> str:
    """
    Get user default locale encoded in BCP-47.
    """
    buffer = ctypes.create_unicode_buffer(LOCALE_NAME_MAX_LENGTH)

    # Call the function
    result = kernel32.GetUserDefaultLocaleName(buffer, LOCALE_NAME_MAX_LENGTH)

    if result == 0:
        # Call failed
        error_code = ctypes.get_last_error()
        raise ctypes.WinError(error_code)

    return buffer.value


def bcp47():
    return GlobalizationPreferences.languages[0]
>>> getdefaultlocale()
('zh_CN', 'cp936')
>>> getlocale(LC_CTYPE)
('Chinese (Simplified)_China', '936')
>>> user_default_locale()
'zh-CN'
>>> bcp47()
'zh-Hans-CN'
Has this already been discussed elsewhere?

Yes

Links to previous discussion of this feature:

gh-90817 gh-130796 https://github.com/python/cpython/issues/123853#issuecomment-3132027944

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với Modules/_localemodule.c, đặc biệt là phần ánh xạ tương thích cho Windows NT pre-4.0. Xem lại các cuộc thảo luận được liên kết trong gh-90817, gh-130796 và issue 123853, sau đó so sánh GetUserDefaultLocaleName với GlobalizationPreferences. Công việc chỉ hoàn tất khi dự án thống nhất về một API hiện đại và ánh xạ mã thập lục phân legacy được thay thế một cách nhất quán.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, python
Lĩnh vực
internationalization, operating-systems
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.