fix: Contextual translation key generation

This commit is contained in:
Suraj Shetty 2020-05-04 22:27:06 +05:30
parent c59418cadb
commit 981da526f9
4 changed files with 14 additions and 2 deletions

View file

@ -49,7 +49,11 @@ class _dict(dict):
return _dict(dict(self).copy())
def _(msg, lang=None, context=None):
"""Returns translated string in current lang, if exists."""
"""Returns translated string in current lang, if exists.
Usage:
_('Change')
_('Change', context='Coins')
"""
from frappe.translate import get_full_dict
from frappe.utils import strip_html_tags, is_html

View file

@ -4,6 +4,7 @@ from __future__ import unicode_literals
import frappe, unittest, os
import frappe.translate
from frappe import _
dirname = os.path.dirname(__file__)
translation_string_file = os.path.join(dirname, 'translation_test_file.txt')
@ -13,6 +14,11 @@ class TestTranslate(unittest.TestCase):
data = frappe.translate.get_messages_from_file(translation_string_file)
self.assertListEqual(data, expected_output)
def test_translation_with_context(self):
frappe.local.lang = 'fr'
self.assertEqual(_('Change'), 'Changement')
self.assertEqual(_('Change', context='Coins'), 'la monnaie')
expected_output = [
('apps/frappe/frappe/tests/translation_test_file.txt', 'Warning: Unable to find {0} in any table related to {1}', 'This is some context', 2),
('apps/frappe/frappe/tests/translation_test_file.txt', 'Warning: Unable to find {0} in any table related to {1}', None, 4),

View file

@ -232,7 +232,7 @@ def get_translation_dict_from_file(path, lang, app):
for item in csv_content:
if len(item)==3 and item[2]:
key = item[1] + ':' + item[2]
key = item[0] + ':' + item[2]
translation_map[key] = strip(item[1])
elif len(item) in [2, 3]:
translation_map[item[0]] = strip(item[1])

View file

@ -4141,3 +4141,5 @@ Path,Chemin,
Components,Composants,
Verified By,Vérifié Par,
Lead Conversion Time,Temps de conversion des prospects,
Change,Changement,
Change,la monnaie,Coins
1 A4 A4
4141 Components Composants
4142 Verified By Vérifié Par
4143 Lead Conversion Time Temps de conversion des prospects
4144 Change Changement
4145 Change la monnaie Coins