Skip to content

Commit dbd1a14

Browse files
committed
i18n: Use fake gettext if .mo files don't exist, and Django isn't installed
1 parent ab4e63e commit dbd1a14

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- Don't fail if compiled translation files (.mo) don't exist, and Django isn't installed
12+
913
## [0.15.1] - 2020-10-21
1014

1115
### Fixed

flattentool/i18n.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@
1414
except ImproperlyConfigured:
1515
raise ImportError
1616

17-
# We set up the translations ourselves, instead of using Django's gettext
18-
# function, so that we can have a custom domain and locale directory.
19-
translations = {}
20-
translations["en"] = gettext.translation(domain, locale_dir, languages=["en"])
21-
translations["es"] = gettext.translation(domain, locale_dir, languages=["es"])
17+
try:
18+
# We set up the translations ourselves, instead of using Django's gettext
19+
# function, so that we can have a custom domain and locale directory.
20+
translations = {}
21+
translations["en"] = gettext.translation(domain, locale_dir, languages=["en"])
22+
translations["es"] = gettext.translation(domain, locale_dir, languages=["es"])
2223

23-
def _(text):
24-
lang = get_language()
25-
if lang not in translations:
26-
lang = "en"
27-
return translations[lang].gettext(text)
24+
def _(text):
25+
lang = get_language()
26+
if lang not in translations:
27+
lang = "en"
28+
return translations[lang].gettext(text)
29+
except FileNotFoundError:
30+
# If .mo files don't exist, pass a fake gettext function instead
31+
_ = lambda x: x
2832

2933

3034
except ImportError:

0 commit comments

Comments
 (0)