paul@375 | 1 | #!/usr/bin/env python |
paul@375 | 2 | |
paul@375 | 3 | """ |
paul@375 | 4 | Locale functions. |
paul@375 | 5 | |
paul@375 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@375 | 7 | |
paul@375 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@375 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@375 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@375 | 11 | version. |
paul@375 | 12 | |
paul@375 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@375 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@375 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@375 | 16 | details. |
paul@375 | 17 | |
paul@375 | 18 | You should have received a copy of the GNU General Public License along with |
paul@375 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@375 | 20 | """ |
paul@375 | 21 | |
paul@375 | 22 | from __builtins__.types import check_int, check_string |
paul@375 | 23 | from native import getlocale as _getlocale, setlocale as _setlocale |
paul@375 | 24 | |
paul@375 | 25 | LC_CTYPE = 0 |
paul@375 | 26 | LC_NUMERIC = 1 |
paul@375 | 27 | LC_TIME = 2 |
paul@375 | 28 | LC_COLLATE = 3 |
paul@375 | 29 | LC_MONETARY = 4 |
paul@375 | 30 | LC_MESSAGES = 5 |
paul@375 | 31 | LC_ALL = 6 |
paul@375 | 32 | |
paul@414 | 33 | def getlocale(category=LC_CTYPE): |
paul@414 | 34 | |
paul@414 | 35 | "Return the locale value for 'category'." |
paul@414 | 36 | |
paul@414 | 37 | check_int(category) |
paul@414 | 38 | return _getlocale(category) |
paul@414 | 39 | |
paul@414 | 40 | def getpreferredencoding(): |
paul@414 | 41 | |
paul@414 | 42 | "Return the encoding from the environment's locale." |
paul@414 | 43 | |
paul@414 | 44 | s = getlocale() |
paul@414 | 45 | |
paul@414 | 46 | try: |
paul@414 | 47 | dot = s.index(".") |
paul@414 | 48 | return s[dot+1:] |
paul@414 | 49 | except ValueError: |
paul@414 | 50 | return None |
paul@414 | 51 | |
paul@414 | 52 | def initlocale(category=LC_CTYPE): |
paul@414 | 53 | |
paul@414 | 54 | "Initialise the locale for 'category' from the environment." |
paul@414 | 55 | |
paul@414 | 56 | check_int(category) |
paul@414 | 57 | return _setlocale(category, "") |
paul@414 | 58 | |
paul@375 | 59 | def setlocale(category, value): |
paul@375 | 60 | |
paul@375 | 61 | "Set the locale for 'category' to 'value'." |
paul@375 | 62 | |
paul@375 | 63 | check_int(category) |
paul@375 | 64 | check_string(value) |
paul@375 | 65 | return _setlocale(category, value) |
paul@375 | 66 | |
paul@375 | 67 | # vim: tabstop=4 expandtab shiftwidth=4 |