Skip to content

Commit

Permalink
fix: ignore global translations (frappe#18733) (frappe#18735)
Browse files Browse the repository at this point in the history
_("string") outside of function/methods don't make any sense cause they
are initialized the moment module is imported. This is already checked
in CI yet people make this mistake.

Ignore and refuse to translate in those cases.

(cherry picked from commit a1a296a)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush authored Nov 3, 2022
1 parent 2f51b3c commit 5f36923
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions frappe/tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ def test_guest_request_language_resolution_with_cookie(self):

self.assertIn(return_val, [second_lang, get_parent_language(second_lang)])

def test_global_translations(self):
""" """
site = frappe.local.site
frappe.destroy()
_("this shouldn't break")
frappe.init(site=site)
frappe.connect()

def test_guest_request_language_resolution_with_request_header(self):
"""Test for frappe.translate.get_language
Expand Down
7 changes: 6 additions & 1 deletion frappe/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ def _merge_translations():

return all_translations

return frappe.cache().hget(MERGED_TRANSLATION_KEY, lang, generator=_merge_translations)
try:
return frappe.cache().hget(MERGED_TRANSLATION_KEY, lang, generator=_merge_translations)
except Exception:
# People mistakenly call translation function on global variables
# where locals are not initalized, translations dont make much sense there
return {}


def get_translations_from_apps(lang, apps=None):
Expand Down

0 comments on commit 5f36923

Please sign in to comment.