|
| 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 | +//| """Locale support module""" |
| 31 | +//| |
| 32 | +//| def getlocale() -> None: |
| 33 | +//| """Returns the current locale setting as a tuple ``(language code, "utf-8")`` |
| 34 | +//| |
| 35 | +//| The language code comes from the installed translation of CircuitPython, specifically the "Language:" code specified in the translation metadata. |
| 36 | +//| This can be useful to allow modules coded in Python to show messages in the user's preferred language. |
| 37 | +//| |
| 38 | +//| Differences from CPython: No ``LC_*`` argument is permitted. |
| 39 | +//| """ |
| 40 | +//| |
| 41 | +STATIC mp_obj_t getlocale(void) { |
| 42 | + |
| 43 | + mp_rom_error_text_t locale_msg = MP_ERROR_TEXT("en_US"); |
| 44 | + size_t len_with_nul = decompress_length(locale_msg); |
| 45 | + size_t len = len_with_nul - 1; |
| 46 | + char buf[len_with_nul]; |
| 47 | + decompress(locale_msg, buf); |
| 48 | + |
| 49 | + mp_obj_t elements[] = { |
| 50 | + mp_obj_new_str(buf, len), |
| 51 | + MP_OBJ_NEW_QSTR(MP_QSTR_utf_hyphen_8) |
| 52 | + }; |
| 53 | + return mp_obj_new_tuple(MP_ARRAY_SIZE(elements), elements); |
| 54 | +} |
| 55 | +MP_DEFINE_CONST_FUN_OBJ_0(getlocale_obj, getlocale); |
| 56 | + |
| 57 | +STATIC const mp_rom_map_elem_t locale_module_globals_table[] = { |
| 58 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_locale) }, |
| 59 | + { MP_ROM_QSTR(MP_QSTR_getlocale), MP_ROM_PTR(&getlocale_obj) }, |
| 60 | +}; |
| 61 | + |
| 62 | +STATIC MP_DEFINE_CONST_DICT(locale_module_globals, locale_module_globals_table); |
| 63 | + |
| 64 | +const mp_obj_module_t locale_module = { |
| 65 | + .base = { &mp_type_module }, |
| 66 | + .globals = (mp_obj_dict_t *)&locale_module_globals, |
| 67 | +}; |
| 68 | + |
| 69 | +MP_REGISTER_MODULE(MP_QSTR_locale, locale_module); |
0 commit comments