diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 232eb702..0b40fcbb 100644 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -1,6 +1,7 @@ # Copyright (C) 2012-2021 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html +import locale import logging import sys @@ -48,6 +49,9 @@ def run(args): print(f"\n{err}", file=sys.stderr) 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 if callable(args.postconfig_cmd): return args.postconfig_cmd( @@ -343,3 +347,12 @@ def _display_search_results(args, journal, **kwargs): print(exporter.export(journal, args.filename)) else: 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) diff --git a/tests/bdd/features/datetime.feature b/tests/bdd/features/datetime.feature index 0da3027f..495bdb14 100644 --- a/tests/bdd/features/datetime.feature +++ b/tests/bdd/features/datetime.feature @@ -172,3 +172,17 @@ Feature: Reading and writing to journal with custom date formats Then we should get no error And the output should be 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 --config-override timeformat \%c -1" + Then the output should contain "" + + 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 |