Set the locale based on the config if specified, otherwise the user environment

This commit is contained in:
Micah Jerome Ellison 2021-11-20 13:50:29 -08:00
parent 882721f859
commit b2251655bf
2 changed files with 27 additions and 0 deletions

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2021 jrnl contributors # Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html # License: https://www.gnu.org/licenses/gpl-3.0.html
import locale
import logging import logging
import sys import sys
@ -48,6 +49,9 @@ def run(args):
print(f"\n{err}", file=sys.stderr) print(f"\n{err}", file=sys.stderr)
sys.exit(1) sys.exit(1)
# From here on, dates and numbers will use the locale supplied by the user
_init_locale(config)
# Run post-config command now that config is ready # Run post-config command now that config is ready
if callable(args.postconfig_cmd): if callable(args.postconfig_cmd):
return args.postconfig_cmd( return args.postconfig_cmd(
@ -343,3 +347,12 @@ def _display_search_results(args, journal, **kwargs):
print(exporter.export(journal, args.filename)) print(exporter.export(journal, args.filename))
else: else:
print(journal.pprint()) print(journal.pprint())
def _init_locale(config):
"""
Initializes the locale from the environment variables, or the config
if specified in the config.
"""
user_locale = config["locale"] if "locale" in config else ""
locale.setlocale(locale.LC_ALL, user_locale)

View file

@ -172,3 +172,17 @@ Feature: Reading and writing to journal with custom date formats
Then we should get no error Then we should get no error
And the output should be And the output should be
2013-10-27 03:27 Some text. 2013-10-27 03:27 Some text.
Scenario Outline: Dates should be displayed using the specified locale
Given we use the config "basic_onefile.yaml"
When we run "jrnl --config-override locale <locale> --config-override timeformat \%c -1"
Then the output should contain "<expected_date>"
Examples: configs
| locale | expected_date |
| en_US | 9/24/2020 9:14:00 AM |
| zh_CN | 2020/9/24 9:14:00 |
| ru_RU | 24.09.2020 9:14:00 |
| fr_FR | 24/09/2020 09:14:00 |
| es_ES | 24/09/2020 9:14:00 |
| de_DE | 24.09.2020 09:14:00 |