Fixes for use gettext in paramecio, deprecating old language system
This commit is contained in:
parent
fc15bd74bb
commit
2ba2ac6d37
11 changed files with 66 additions and 34 deletions
|
|
@ -1,9 +1,11 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from importlib import import_module
|
||||
import gettext
|
||||
#from paramecio.citoplasma.sessions import get_session
|
||||
import json
|
||||
from flask import session, has_request_context
|
||||
import os
|
||||
|
||||
yes_session=False
|
||||
|
||||
|
|
@ -28,6 +30,15 @@ def load_lang(*args):
|
|||
|
||||
# here load the language
|
||||
|
||||
def pgettext(module_file):
|
||||
|
||||
module=os.path.dirname(os.path.realpath(module_file))
|
||||
|
||||
base_name=os.path.dirname(os.path.realpath(module))
|
||||
|
||||
l=gettext.translation(os.path.basename(base_name), module+'/languages/', languages=I18n.dict_i18n, fallback=True)
|
||||
|
||||
return l.gettext
|
||||
|
||||
class I18n:
|
||||
"""Class for i18n tasks
|
||||
|
|
|
|||
|
|
@ -18,9 +18,11 @@ from paramecio2.libraries.i18n import I18n
|
|||
from paramecio2.libraries.urls import make_url, make_media_url, add_get_parameters
|
||||
from paramecio2.libraries.formsutils import csrf_token
|
||||
|
||||
"""
|
||||
def _(text):
|
||||
|
||||
return gettext.gettext(text)
|
||||
"""
|
||||
|
||||
def env_theme(module, cache_enabled=True, cache_impl='', cache_args={}, module_directory="./tmp/modules"):
|
||||
"""Function for create an environment for mako templates
|
||||
|
|
@ -47,6 +49,8 @@ def env_theme(module, cache_enabled=True, cache_impl='', cache_args={}, module_d
|
|||
|
||||
standard_templates=path.dirname(__file__)+'/templates'
|
||||
|
||||
standard_languages=path.dirname(__file__)+'/languages'
|
||||
|
||||
module_directory+='/'+module
|
||||
|
||||
module_templates=module+'/templates'
|
||||
|
|
@ -94,8 +98,6 @@ class PTemplate:
|
|||
|
||||
self.add_filter(I18n.lang)
|
||||
|
||||
self.add_filter(_)
|
||||
|
||||
#self.add_filter(make_url)
|
||||
|
||||
self.add_filter(make_media_url)
|
||||
|
|
@ -110,6 +112,22 @@ class PTemplate:
|
|||
|
||||
self.add_filter(self.load_js)
|
||||
|
||||
# Loading language domain for gettext in templates
|
||||
|
||||
base_name=path.dirname(path.realpath(__file__))
|
||||
|
||||
module_env=self.env.directories[1].replace('/templates', '')
|
||||
|
||||
#print(path.basename(module_env)+' '+base_name+'/languages/')
|
||||
|
||||
self.l=gettext.translation(path.basename(module_env), base_name+'/languages/', languages=I18n.dict_i18n, fallback=True)
|
||||
|
||||
self.add_filter(self._)
|
||||
|
||||
def _(self, text):
|
||||
|
||||
return self.l.gettext(text)
|
||||
|
||||
def add_js(self, js, module=''):
|
||||
"""Function for add js to self.js attribute
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from flask import Blueprint, redirect, session, url_for, request, g, make_response, abort
|
||||
from settings import config
|
||||
from paramecio2.libraries.i18n import I18n
|
||||
from paramecio2.libraries.i18n import I18n, pgettext
|
||||
from paramecio2.libraries.datetime import now, format_local_strtime, timestamp_to_datetime, obtain_timestamp
|
||||
from paramecio2.libraries.formsutils import show_form, generate_csrf, set_extra_forms_user, pass_values_to_form
|
||||
from paramecio2.libraries.db.webmodel import WebModel
|
||||
|
|
@ -15,6 +15,9 @@ from os import path
|
|||
from paramecio2.modules.admin import admin_app, t
|
||||
from paramecio2.libraries.sendmail import SendMail
|
||||
from paramecio2.libraries.formsutils import check_csrf
|
||||
#import gettext
|
||||
|
||||
_=pgettext(__file__)
|
||||
|
||||
yes_recovery_login=False
|
||||
email_address='localhost'
|
||||
|
|
@ -116,7 +119,7 @@ for app_load in config_admin:
|
|||
@admin_app.route('/admin/')
|
||||
def admin():
|
||||
|
||||
return t.load_template('home.phtml', title=I18n.lang('admin', 'admin', 'Admin'))
|
||||
return t.load_template('home.phtml', title=_('Admin'))
|
||||
|
||||
"""
|
||||
@admin_app.route('/admin/')
|
||||
|
|
@ -250,7 +253,7 @@ def login():
|
|||
|
||||
# def send(self, from_address, to_address: list, subject, message, content_type='plain', attachments=[]):
|
||||
|
||||
sendmail.send(config.portal_email, [arr_user['email']], I18n.lang('admin', 'code_for_complete_login', 'Code for complete login'), I18n.lang('admin', 'code_for_complete_login_explain', 'We send to you a code for activate your account using double authentication:')+"\n"+token_auth, content_type='plain', attachments=[])
|
||||
sendmail.send(config.portal_email, [arr_user['email']], _('Code for complete login'), _('We send to you a code for activate your account using double authentication:')+"\n"+token_auth, content_type='plain', attachments=[])
|
||||
|
||||
if arr_user['dark_theme']:
|
||||
session['theme']='1'
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ ${load_js()|n}
|
|||
|
||||
<nav id="menu" class="nav-collapse">
|
||||
<ul>
|
||||
<li class="menu_title"><%block name="applications"><i class="fa fa-gear" aria-hidden="true"></i>${lang('admin', 'applications', 'Applications')}</li></%block>
|
||||
<li class="menu_title"><%block name="applications"><i class="fa fa-gear" aria-hidden="true"></i>${_('Applications')}</li></%block>
|
||||
<%block name="menu_list">
|
||||
<%
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
<%inherit file="dashboard.phtml"/>
|
||||
<%block name="content">
|
||||
<div class="title">
|
||||
Welcome to Paramecio Admin
|
||||
${_('Welcome to Paramecio Admin')}
|
||||
</div>
|
||||
<div class="cont">
|
||||
From here you can admin your site
|
||||
${_('From here you can admin your site')}
|
||||
</div>
|
||||
</%block>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
<%inherit file="home.html"/>
|
||||
<%block name="content">
|
||||
<div class="title">
|
||||
${lang('admin', 'welcome_to_admin_dashboard', 'Welcome to Admin dashboard')}
|
||||
${_('Welcome to Admin dashboard')}
|
||||
</div>
|
||||
<div class="cont">
|
||||
${lang('admin', 'from_here_you_can_configure_your_site', 'From here you can configure your site')}.
|
||||
${_('From here you can configure your site')}.
|
||||
</div>
|
||||
</%block>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%block name="title">${lang('admin', 'login', 'Paramecio Login')}</%block></title>
|
||||
<title><%block name="title">${_('Paramecio Login')}</%block></title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
|
||||
<link href="${make_media_url('css/login.css', 'admin')}" rel='stylesheet' type='text/css'>
|
||||
|
|
@ -58,25 +58,25 @@
|
|||
|
||||
if(data.hasOwnProperty('disable')) {
|
||||
|
||||
$('#username_error').html("${lang('common', 'error_disabled', 'Error, your user is disabled, you need support of web administration')}");
|
||||
$('#username_error').html("${_('Error, your user is disabled, you need support of web administration')}");
|
||||
|
||||
} if(data.hasOwnProperty('you_cannot_login')) {
|
||||
|
||||
if(data.you_cannot_login) {
|
||||
|
||||
$('#username_error').html("${lang('common', 'error_tries_disabled', 'Error, excessive tries, wait some minutes for login again')}");
|
||||
$('#username_error').html("${_('Error, excessive tries, wait some minutes for login again')}");
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$('#username_error').html("${lang('common', 'error_login', 'Error, wrong username or password')}");
|
||||
$('#username_error').html("${_('Error, wrong username or password')}");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$('#username_error').html("${lang('common', 'error_login', 'Error, wrong username or password')}");
|
||||
$('#username_error').html("${_('Error, wrong username or password')}");
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -96,20 +96,20 @@
|
|||
<%block name="content">
|
||||
<form id="login">
|
||||
<div id="title">
|
||||
${lang('admin', 'login', 'Paramecio Login')}
|
||||
${_('Paramecio Login')}
|
||||
</div>
|
||||
${forms|n}
|
||||
<div class="form">
|
||||
${lang('admin', 'remember_login', 'Remember login?')} <input type="checkbox" id="remember_login" name="remember_login" value="1">
|
||||
${_('Remember login?')} <input type="checkbox" id="remember_login" name="remember_login" value="1">
|
||||
</div>
|
||||
<div id="submit_block">
|
||||
<input type="submit" value="${lang('common', 'login', 'Login')}" class="submit" id="login_submit"/>
|
||||
<input type="submit" value="${_('Login')}" class="submit" id="login_submit"/>
|
||||
<span id="loading"> </span>
|
||||
</div>
|
||||
% if yes_recovery_login:
|
||||
<div class="form"><a href="${url_for('.recovery_password')}">${lang('admin', 'recovery_password', 'Recovery password?')}</a></div>
|
||||
<div class="form"><a href="${url_for('.recovery_password')}">${_('Recovery password?')}</a></div>
|
||||
% endif
|
||||
<div class="form">${lang('admin', 'remember_tries', 'Remember that only have 3 attempts')}</div>
|
||||
<div class="form">${_('Remember that only have 3 attempts')}</div>
|
||||
</form>
|
||||
</%block>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
<%inherit file="login.phtml"/>
|
||||
<%block name="title">${lang('admin', 'login', 'Paramecio Login')}</%block>
|
||||
<%block name="title">${_('Paramecio Login')}</%block>
|
||||
<%block name="content">
|
||||
<form id="login">
|
||||
<div id="title">
|
||||
${lang('admin', 'login', 'Paramecio Login')}
|
||||
${_('Paramecio Login')}
|
||||
</div>
|
||||
<div class="form">
|
||||
<p align="center">${lang('admin', 'check_your_email', 'Check your email for get instructions for complete login with double auth or')} <a href="${url_for('.logout')}">logout</a> and login again with other user</p>
|
||||
<p><label>${lang('admin', 'code', 'Code')} *</label><input type="text" class="" name="code" id="code_form" value="" /> <span class="error" id="code_error"></span></p>
|
||||
<p align="center">${_('Check your email for get instructions for complete login with double auth or')} <a href="${url_for('.logout')}">logout</a> and login again with other user</p>
|
||||
<p><label>${_('Code')} *</label><input type="text" class="" name="code" id="code_form" value="" /> <span class="error" id="code_error"></span></p>
|
||||
${csrf_token()|n}
|
||||
</div>
|
||||
<div id="submit_block">
|
||||
<input type="submit" value="${lang('common', 'send_code', 'Send code')}" class="submit" id="code_submit"/>
|
||||
<input type="submit" value="${_('Send code')}" class="submit" id="code_submit"/>
|
||||
<span id="loading"> </span>
|
||||
</div>
|
||||
</form>
|
||||
|
|
@ -60,17 +60,17 @@
|
|||
|
||||
if(data.hasOwnProperty('disable')) {
|
||||
|
||||
$('#code_error').html("${lang('common', 'error_disabled', 'Error, your user is disabled, you need support of web administration')}");
|
||||
$('#code_error').html("${_('Error, your user is disabled, you need support of web administration')}");
|
||||
|
||||
} else {
|
||||
|
||||
$('#code_error').html("${lang('common', 'error_wrong_code', 'Error, wrong code')}");
|
||||
$('#code_error').html("${_('Error, wrong code')}");
|
||||
|
||||
}
|
||||
|
||||
if(data.you_cannot_login) {
|
||||
|
||||
$('#code_error').html("${lang('common', 'error_tries_disabled', 'Error, excessive tries, wait some minutes for login again')}");
|
||||
$('#code_error').html("${_('Error, excessive tries, wait some minutes for login again')}");
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,16 +48,16 @@
|
|||
});
|
||||
</script>
|
||||
</%block>
|
||||
<%block name="title">${lang('admin', 'sign_up', 'Paramecio Sign up')}</%block>
|
||||
<%block name="title">${_('Paramecio Sign up')}</%block>
|
||||
<%block name="content">
|
||||
<form id="login">
|
||||
<div id="title">
|
||||
${lang('admin', 'sign_up', 'Paramecio Sign up')}
|
||||
${_('Paramecio Sign up')}
|
||||
</div>
|
||||
${forms|n}
|
||||
<div id="result_register"></div>
|
||||
<div id="submit_block">
|
||||
<input type="submit" value="${lang('common', 'sign_up', 'Sign up')}" class="submit" id="register_submit"/>
|
||||
<input type="submit" value="${_('Sign up')}" class="submit" id="register_submit"/>
|
||||
<span id="loading"> </span>
|
||||
</div>
|
||||
</form>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
<%inherit file="dashboard.phtml"/>
|
||||
<%block name="content">
|
||||
<div class="title">
|
||||
Welcome to Paramecio Admin
|
||||
${_('Welcome to Paramecio Admin')}
|
||||
</div>
|
||||
<div class="cont">
|
||||
From here you can admin your site
|
||||
${_('From here you can admin your site')}
|
||||
</div>
|
||||
</%block>
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@
|
|||
${content}
|
||||
</div>
|
||||
|
||||
<div class="footer">Paramecio, a system created for create webapps</div>
|
||||
<div class="footer">${_('Paramecio, a system created for create webapps')}</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue