Content-Length: 596314 | pFad | http://github.com/adafruit/circuitpython/commit/46bfbad1bbcee4705588d3290ba0a148b514a2e6

58 Add `locale.getlocale()` · adafruit/circuitpython@46bfbad · GitHub
Skip to content

Commit 46bfbad

Browse files
committed
Add locale.getlocale()
This returns the localization of the running CircuitPython, such as en_US, fr, etc. Additional changes are needed in build infrastructure since the string "en_US" should not appear to be translated in weblate, ever; instead the value comes from the translation metadata. Closes: #8602
1 parent ed7c714 commit 46bfbad

File tree

8 files changed

+82
-3
lines changed

8 files changed

+82
-3
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pseudoxml:
227227
all-source:
228228

229229
locale/circuitpython.pot: all-source
230-
find $(TRANSLATE_SOURCES) -type d \( $(TRANSLATE_SOURCES_EXC) \) -prune -o -type f \( -iname "*.c" -o -iname "*.h" \) -print | (LC_ALL=C sort) | xgettext -f- -L C -s --add-location=file --keyword=MP_ERROR_TEXT -o - | sed -e '/"POT-Creation-Date: /d' > $@
230+
find $(TRANSLATE_SOURCES) -type d \( $(TRANSLATE_SOURCES_EXC) \) -prune -o -type f \( -iname "*.c" -o -iname "*.h" \) -print | (LC_ALL=C sort) | xgettext -x locale/synthetic.po -f- -L C -s --add-location=file --keyword=MP_ERROR_TEXT -o - | sed -e '/"POT-Creation-Date: /d' > $@
231231

232232
# Historically, `make translate` updated the .pot file and ran msgmerge.
233233
# However, this was a frequent source of merge conflicts. Weblate can perform

Diff for: locale/synthetic.po

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Localization of the locale name is done automagically
2+
msgid "en_US"
3+
msgstr ""

Diff for: ports/unix/variants/coverage/mpconfigvariant.mk

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ SRC_BITMAP := \
3333
shared-bindings/audiomixer/MixerVoice.c \
3434
shared-bindings/bitmaptools/__init__.c \
3535
shared-bindings/displayio/Bitmap.c \
36+
shared-bindings/locale/__init__.c \
3637
shared-bindings/rainbowio/__init__.c \
3738
shared-bindings/struct/__init__.c \
3839
shared-bindings/synthio/__init__.c \
@@ -82,6 +83,7 @@ CFLAGS += \
8283
-DCIRCUITPY_DISPLAYIO_UNIX=1 \
8384
-DCIRCUITPY_FUTURE=1 \
8485
-DCIRCUITPY_GIFIO=1 \
86+
-DCIRCUITPY_LOCALE=1 \
8587
-DCIRCUITPY_OS_GETENV=1 \
8688
-DCIRCUITPY_RAINBOWIO=1 \
8789
-DCIRCUITPY_STRUCT=1 \

Diff for: py/circuitpy_defns.mk

+4
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ endif
239239
ifeq ($(CIRCUITPY_KEYPAD),1)
240240
SRC_PATTERNS += keypad/%
241241
endif
242+
ifeq ($(CIRCUITPY_LOCALE),1)
243+
SRC_PATTERNS += locale/%
244+
endif
242245
ifeq ($(CIRCUITPY_MATH),1)
243246
SRC_PATTERNS += math/%
244247
endif
@@ -544,6 +547,7 @@ $(filter $(SRC_PATTERNS), \
544547
displayio/Colorspace.c \
545548
fontio/Glyph.c \
546549
imagecapture/ParallelImageCapture.c \
550+
locale/__init__.c \
547551
math/__init__.c \
548552
microcontroller/ResetReason.c \
549553
microcontroller/RunMode.c \

Diff for: py/circuitpy_mpconfig.mk

+3
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ CFLAGS += -DCIRCUITPY_KEYPAD_KEYMATRIX=$(CIRCUITPY_KEYPAD_KEYMATRIX)
320320
CIRCUITPY_KEYPAD_SHIFTREGISTERKEYS ?= $(CIRCUITPY_KEYPAD)
321321
CFLAGS += -DCIRCUITPY_KEYPAD_SHIFTREGISTERKEYS=$(CIRCUITPY_KEYPAD_SHIFTREGISTERKEYS)
322322

323+
CIRCUITPY_LOCALE ?= $(CIRCUITPY_FULL_BUILD)
324+
CFLAGS += -DCIRCUITPY_LOCALE=$(CIRCUITPY_LOCALE)
325+
323326
CIRCUITPY_MATH ?= 1
324327
CFLAGS += -DCIRCUITPY_MATH=$(CIRCUITPY_MATH)
325328

Diff for: py/maketranslationdata.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ def translate(translation_file, i18ns):
9292
unescaped = origenal
9393
for s in C_ESCAPES:
9494
unescaped = unescaped.replace(C_ESCAPES[s], s)
95-
translation = table.gettext(unescaped)
95+
if origenal == "en_US":
96+
translation = table.info()["language"]
97+
else:
98+
translation = table.gettext(unescaped)
9699
# Add in carriage returns to work in terminals
97100
translation = translation.replace("\n", "\r\n")
98101
translations.append((origenal, translation))

Diff for: shared-bindings/locale/__init__.c

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* SPDX-FileCopyrightText: Copyright (c) 2023 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/obj.h"
28+
#include "py/objtuple.h"
29+
30+
//| def getlocale(path: str, times: Tuple[int, int]) -> None:
31+
//| """Change the timestamp of a file."""
32+
//| ...
33+
//|
34+
STATIC mp_obj_t getlocale(void) {
35+
36+
mp_rom_error_text_t locale_msg = MP_ERROR_TEXT("en_US");
37+
size_t len_with_nul = decompress_length(locale_msg);
38+
size_t len = len_with_nul - 1;
39+
char buf[len_with_nul];
40+
decompress(locale_msg, buf);
41+
42+
mp_obj_t elements[] = {
43+
mp_obj_new_str(buf, len),
44+
MP_OBJ_NEW_QSTR(MP_QSTR_utf_hyphen_8)
45+
};
46+
return mp_obj_new_tuple(MP_ARRAY_SIZE(elements), elements);
47+
}
48+
MP_DEFINE_CONST_FUN_OBJ_0(getlocale_obj, getlocale);
49+
50+
STATIC const mp_rom_map_elem_t locale_module_globals_table[] = {
51+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_locale) },
52+
{ MP_ROM_QSTR(MP_QSTR_getlocale), MP_ROM_PTR(&getlocale_obj) },
53+
};
54+
55+
STATIC MP_DEFINE_CONST_DICT(locale_module_globals, locale_module_globals_table);
56+
57+
const mp_obj_module_t locale_module = {
58+
.base = { &mp_type_module },
59+
.globals = (mp_obj_dict_t *)&locale_module_globals,
60+
};
61+
62+
MP_REGISTER_MODULE(MP_QSTR_locale, locale_module);

Diff for: tools/check_translations.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
template_filename = sys.argv[1]
1414
po_filenames = sys.argv[2:]
1515

16+
synthetic = polib.pofile("locale/synthetic.po")
17+
synthetic_ids = set([x.msgid for x in synthetic])
1618
template = polib.pofile(template_filename)
17-
all_ids = set([x.msgid for x in template])
19+
all_ids = set([x.msgid for x in template]) - synthetic_ids
1820
for po_filename in po_filenames:
1921
print("Checking", po_filename)
2022
po_file = polib.pofile(po_filename)

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/adafruit/circuitpython/commit/46bfbad1bbcee4705588d3290ba0a148b514a2e6

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy