How to use Translator

115 views
Skip to first unread message

Julio César Albornoz

unread,
Nov 19, 2020, 9:44:19 AM11/19/20
to py4web

Dears:

I have in this script in users.py
...
...
from py4web import  Translator
T_FOLDER = os.path.join(os.path.dirname(__file__), 'translations')
T = Translator(T_FOLDER)

@action('users/showgroup', method=['POST', 'GET'])
@action('users/showgroup/<path:path>', method=['POST', 'GET'])
@action.uses(session, db, auth,Inject(createmenu=createmenu), Inject(T=T),'users/showgroup.html')    
@has_membership(['Super','sso'])
def showgroup(path=None):
    # importing datetime module for now()
    import datetime
    ls_message = T('Group')
...
...
...

In translations --> es.json
{
"Groups":{"0":"Grupos"},
"Hello":{"0":"Hola"},
}

I want  translate from English to Spanish


So I get this messages

Traceback (most recent call last): File "C:\Users\jalbornoz\AppData\Roaming\Python\Python38\site-packages\py4web\core.py", line 1031, in import_app module = importlib.machinery.SourceFileLoader( File "<frozen importlib._bootstrap_external>", line 462, in _check_name_wrapper File "<frozen importlib._bootstrap_external>", line 962, in load_module File "<frozen importlib._bootstrap_external>", line 787, in load_module File "<frozen importlib._bootstrap>", line 265, in _load_module_shim File "<frozen importlib._bootstrap>", line 702, in _load File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "apps\TSGB\__init__.py", line 8, in <module> from .models import db File "apps\TSGB\models.py", line 5, in <module> from .common import db, Field File "apps\TSGB\common.py", line 53, in <module> T = Translator(settings.T_FOLDER) File "C:\Users\jalbornoz\AppData\Roaming\Python\Python38\site-packages\pluralize\__init__.py", line 60, in __init__ self.load(folder) File "C:\Users\jalbornoz\AppData\Roaming\Python\Python38\site-packages\pluralize\__init__.py", line 68, in load self.languages[filename[:-5].lower()] = json.load(fp) File "c:\users\jalbornoz\appdata\local\programs\python\python38-32\lib\json\__init__.py", line 293, in load return loads(fp.read(), File "c:\users\jalbornoz\appdata\local\programs\python\python38-32\lib\json\__init__.py", line 357, in loads return _default_decoder.decode(s) File "c:\users\jalbornoz\appdata\local\programs\python\python38-32\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "c:\users\jalbornoz\appdata\local\programs\python\python38-32\lib\json\decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 5 column 1 (char 72)

What is wrong?

Regards.

Val K

unread,
Nov 19, 2020, 3:22:26 PM11/19/20
to py4web
There is trailing comma in your json - try to remove it

четверг, 19 ноября 2020 г. в 17:44:19 UTC+3, juliocesa...@gmail.com:

Julio César Albornoz

unread,
Nov 19, 2020, 3:28:39 PM11/19/20
to py4web
Thanks for your time, I remove it. But only change de message

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 4 column 1 (char 49)

Regards.

Julio César Albornoz

unread,
Nov 19, 2020, 4:07:24 PM11/19/20
to py4web
Dears:

I added this instruction 

T = Translator(T_FOLDER)
T.select('es')

I can't get error, but not works translation.

Regards

Val K

unread,
Nov 19, 2020, 4:07:46 PM11/19/20
to py4web
Did you reload app?

четверг, 19 ноября 2020 г. в 23:28:39 UTC+3, juliocesa...@gmail.com:

Val K

unread,
Nov 19, 2020, 4:09:59 PM11/19/20
to py4web
just in case, under  trailing comma I mean last comma
I tested this (works fine):
{
"Groups":{"0":"Grupos"},
"Hello":{"0":"Hola"}
}

пятница, 20 ноября 2020 г. в 00:07:46 UTC+3, Val K:

Julio César Albornoz

unread,
Nov 19, 2020, 4:42:40 PM11/19/20
to py4web
Dears:

After to add

T.select('es')
messages = str(T('Groups'))

or

[[=str(T('Groups'))]]

the translation works.

Regards.


Massimo

unread,
Nov 27, 2020, 9:35:51 PM11/27/20
to py4web
The problem is that you inject T but you do not declare T as a fixture to be used. I.e. this:

@action.uses(session, db, auth,Inject(createmenu=createmenu), Inject(T=T),'users/showgroup.html')   

should be

@action.uses(session, db, auth, T, Inject(createmenu=createmenu), Inject(T=T), 'users/showgroup.html')   

Maybe when one uses T, it should inject itself. We can have that discussion on Sunday.
Reply all
Reply to author
Forward
0 new messages