From 9be42a2a4be54a2f89f148b536b752211d629d1d Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Thu, 20 Mar 2025 21:49:35 +0100 Subject: [PATCH] Fixes in lang --- paramecio/libraries/adminutils.py | 2 +- paramecio/libraries/generate_admin_class.py | 12 ++-- paramecio/libraries/js.py | 2 +- paramecio/libraries/lists.py | 14 ++-- paramecio/libraries/mtemplates.py | 4 ++ paramecio/libraries/pages.py | 8 ++- paramecio/libraries/slugify.py | 80 +++++++-------------- paramecio/modules/admin2/app.py | 26 +++---- paramecio/modules/admin2/ausers.py | 20 +++--- 9 files changed, 77 insertions(+), 91 deletions(-) diff --git a/paramecio/libraries/adminutils.py b/paramecio/libraries/adminutils.py index 7698755..86925c2 100644 --- a/paramecio/libraries/adminutils.py +++ b/paramecio/libraries/adminutils.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from collections import OrderedDict -from paramecio.libraries.sessions import get_session +from paramecio.libraries.sessionplugin import get_session from paramecio.libraries.urls import make_url from paramecio.libraries.i18n import I18n from paramecio.libraries.httputils import GetPostFiles diff --git a/paramecio/libraries/generate_admin_class.py b/paramecio/libraries/generate_admin_class.py index ec2e8da..4f8ee4e 100644 --- a/paramecio/libraries/generate_admin_class.py +++ b/paramecio/libraries/generate_admin_class.py @@ -3,10 +3,14 @@ from bottle import request from paramecio.libraries.urls import add_get_parameters, redirect from paramecio.libraries.mtemplates import set_flash_message from paramecio.libraries.db.formsutils import show_form -from paramecio.libraries.i18n import I18n +from paramecio.libraries.i18n import I18n, PGetText from paramecio.libraries.httputils import GetPostFiles from collections import OrderedDict +gtext=PGetText(__file__) + +_=gtext.gettext + class GenerateAdminClass: def __init__(self, model, url, t): @@ -53,7 +57,7 @@ class GenerateAdminClass: self.post_update=None - self.text_home=I18n.lang('common', 'home', 'Home') + self.text_home=_('Home') def show(self): @@ -218,7 +222,7 @@ class GenerateConfigClass: self.post_update=None - self.text_home=I18n.lang('common', 'home', 'Home') + self.text_home=_('Home') def show(self): @@ -232,7 +236,7 @@ class GenerateConfigClass: self.model.create_forms() - title_edit=I18n.lang('common', 'edit', 'Edit')+' '+self.title_name + title_edit=_('Edit')+' '+self.title_name edit_forms=OrderedDict() diff --git a/paramecio/libraries/js.py b/paramecio/libraries/js.py index 230ef17..e7eaec0 100644 --- a/paramecio/libraries/js.py +++ b/paramecio/libraries/js.py @@ -1,4 +1,4 @@ -from paramecio.libraries.sessions import get_session +from paramecio.libraries.sessionplugin import get_session from paramecio.libraries.i18n import I18n from settings import config diff --git a/paramecio/libraries/lists.py b/paramecio/libraries/lists.py index 93cf2aa..25ad245 100644 --- a/paramecio/libraries/lists.py +++ b/paramecio/libraries/lists.py @@ -3,13 +3,17 @@ from paramecio.libraries.pages import Pages from paramecio.libraries.urls import add_get_parameters from paramecio.libraries.sessions import get_session -from paramecio.libraries.i18n import I18n +from paramecio.libraries.i18n import I18n, PGetText from paramecio.libraries.httputils import GetPostFiles from bottle import request import sys import re from paramecio.libraries.pages import Pages +gtext=PGetText(__file__) + +_=gtext.gettext + class SimpleList: def __init__(self, model, url, t): @@ -76,7 +80,7 @@ class SimpleList: #self.yes_options=True - self.arr_extra_fields=[I18n.lang('common', 'options', 'Options')] + self.arr_extra_fields=[_('Options')] self.arr_extra_options=[SimpleList.standard_options] @@ -167,8 +171,8 @@ class SimpleList: @staticmethod def standard_options(url, id, arr_row): options=[] - options.append(''+I18n.lang('common', 'edit', 'Edit')+'') - options.append(''+I18n.lang('common', 'delete', 'Delete')+'') + options.append(''+_('Edit')+'') + options.append(''+_('Delete')+'') return options def show(self): @@ -310,7 +314,7 @@ class AjaxList(SimpleList): pages=Pages() - html_pages=I18n.lang('cuchulu', 'pages', 'Pages')+': '+pages.show( begin_page, total_elements, limit, '#' ,initial_num_pages=self.initial_num_pages, variable='begin_page', label='', func_jscript='') + html_pages=_('Pages')+': '+pages.show( begin_page, total_elements, limit, '#' ,initial_num_pages=self.initial_num_pages, variable='begin_page', label='', func_jscript='') with self.db.query(str_query, params) as cursor: for row in cursor: diff --git a/paramecio/libraries/mtemplates.py b/paramecio/libraries/mtemplates.py index fc054ae..05f9e96 100644 --- a/paramecio/libraries/mtemplates.py +++ b/paramecio/libraries/mtemplates.py @@ -33,6 +33,10 @@ from os import path from collections import OrderedDict from paramecio.wsgiapp import app +gtext=PGetText(__file__) + +_=gtext.gettext + # Preparing envs for views of modules, and views of def env_theme(module, cache_enabled=True, cache_impl='', cache_args={}, module_directory="./tmp/modules"): diff --git a/paramecio/libraries/pages.py b/paramecio/libraries/pages.py index 33a68a6..e72def0 100644 --- a/paramecio/libraries/pages.py +++ b/paramecio/libraries/pages.py @@ -2,7 +2,11 @@ from math import ceil, floor from paramecio.libraries.urls import add_get_parameters -from paramecio.libraries.i18n import I18n +from paramecio.libraries.i18n import I18n, PGetText + +gtext=PGetText(__file__) + +_=gtext.gettext class Pages: @@ -64,7 +68,7 @@ class Pages: middle_link=add_get_parameters(link, **{variable: str(x+num_elements)} ); last_link=add_get_parameters(link, **{variable: str( ( ( total_page*num_elements ) - num_elements) ) } ) - pages += ">> "+I18n.lang('common', 'last', 'Last')+"" + pages += ">> "+_('Last')+"" return pages diff --git a/paramecio/libraries/slugify.py b/paramecio/libraries/slugify.py index e6143f7..433758c 100644 --- a/paramecio/libraries/slugify.py +++ b/paramecio/libraries/slugify.py @@ -1,60 +1,30 @@ -#!/usr/bin/python +""" +Paramecio2fm is a series of wrappers for Flask, mako and others and construct a simple headless cms. -#A very simple version of strtr of php. +Copyright (C) 2023 Antonio de la Rosa Caballero -def strtr(str_in, pat_str, rep_str): - - ret_str='' - - arr_dict={} - - if(len(pat_str)!=len(rep_str)): - raise NameError('Ups, pat_str len != rep_str len') - - #Create dictionary - - for (i, l) in enumerate(pat_str): - arr_dict[l]=rep_str[i] - - #Make a for to the str_in and substr. - - for le in str_in: - - if le in arr_dict: - - ret_str+=arr_dict[le] - - else: - ret_str+=le - - return (ret_str) +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. -def slugify(str_in, respect_upper=False, replace_space='-', replace_dot=False, replace_barr=False): - - str_out='' - - from_str='àáâãäåæçèéêëìíîïðòóôõöøùúûýþÿŕñÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÒÓÔÕÖØÙÚÛÝỲŸÞŔÑ¿?!¡()"|#*%,;+&$ºª<>`çÇ{}@~=^:´[]\'' - to_str= 'aaaaaaaceeeeiiiidoooooouuuybyrnAAAAAACEEEEIIIIDOOOOOOUUUYYYBRN----------------------------------' - - if replace_dot==True: - - from_str+='.' - from_to+='-' +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. - - if replace_barr==True: - - from_str+="/" - to_str+="-" - - str_out=str_in.strip() - - str_out=strtr(str_out, from_str, to_str) +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +""" - str_out=str_out.replace(" ", replace_space) - - if respect_upper==False: - str_out=str_out.lower() - - return str_out - +from slugify import slugify as slugify_func + +def slugify(slug, *args, **wargs): + """Simple wrapper for slugify module https://github.com/un33k/python-slugify + + Args: + slug (str): The string to be slugified + + """ + + return slugify_func(slug, *args, **wargs) diff --git a/paramecio/modules/admin2/app.py b/paramecio/modules/admin2/app.py index 9e3c3de..b1d1ce4 100644 --- a/paramecio/modules/admin2/app.py +++ b/paramecio/modules/admin2/app.py @@ -68,9 +68,9 @@ def home_admin(session={}): #s=get_session() - i18n=I18n('admin2') + #i18n=I18n('admin2') - return t.load_template('layout.phtml', title=i18n.tlang('Admin'), module_selected='home_admin', session=session) + return t.load_template('layout.phtml', title=_('Admin'), module_selected='home_admin', session=session) #return {} @@ -103,9 +103,9 @@ def login_admin(session={}): db.close() - i18n=I18n('admin2') + #i18n=I18n('admin2') - return t.load_template('login.phtml', title=i18n.tlang('Login')) + return t.load_template('login.phtml', title=_('Login')) @admin_app.get('/signup', skip=[check_login], name='admin_app.signup_admin') def signup_admin(session={}): @@ -120,20 +120,20 @@ def signup_admin(session={}): db.close() - i18n=I18n('admin2') + #i18n=I18n('admin2') - return t.load_template('signup.phtml', title=i18n.tlang('Signup')) + return t.load_template('signup.phtml', title=_('Signup')) @admin_app.post('/login', skip=[check_login], name='admin_app.check_login_admin') def check_login_admin(session={}): db=WebModel.connection() - i18n=I18n('admin2') + #i18n=I18n('admin2') error=1 - message=i18n.tlang('Invalid user and password') + message=_('Invalid user and password') no_login=check_login_tries(request, db) @@ -183,7 +183,7 @@ def check_login_admin(session={}): sendmail=SendMail(ssl=True) - sendmail.send(config.portal_email, [result['email']], i18n.tlang('Code for complete login'), i18n.tlang('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, [result['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 result['privileges']==0: with db.query('select module from privilegesmodule2 WHERE user_id=%s', [result['id']]) as cursor: @@ -221,7 +221,7 @@ def check_login_admin(session={}): @admin_app.post('/signup', skip=[check_login], name='admin_app.signup_insert_admin') def signup_insert_admin(session={}): - i18n=I18n('admin2') + #i18n=I18n('admin2') error=1 @@ -260,7 +260,7 @@ def signup_insert_admin(session={}): error=0 message="User added!" else: - message=i18n.tlang('Sorry, cannot create the new user') + message=_('Sorry, cannot create the new user') db.close() @@ -315,9 +315,9 @@ def change_theme(session={}): @admin_app.get('/need_auth', skip=[check_login], name='admin_app.need_auth') def need_auth(session={}): - i18n=I18n('admin2') + #i18n=I18n('admin2') - return t.load_template('need_auth.phtml', title=i18n.tlang('Auth check')) + return t.load_template('need_auth.phtml', title=_('Auth check')) @admin_app.post('/auth_check', skip=[check_login], name='admin_app.auth_check') def auth_check(session={}): diff --git a/paramecio/modules/admin2/ausers.py b/paramecio/modules/admin2/ausers.py index ebe8d00..3e30bba 100644 --- a/paramecio/modules/admin2/ausers.py +++ b/paramecio/modules/admin2/ausers.py @@ -21,7 +21,7 @@ env=env_theme(__file__) t=PTemplate(env) -i18n=I18n('admin2') +#i18n=I18n('admin2') gtext=PGetText(__file__) @@ -59,11 +59,11 @@ def admin_users(session={}): user_admin.create_forms(['username', 'password', 'email', 'privileges', 'lang', 'dark_theme', 'double_auth']) - user_admin.forms['privileges'].arr_select={0: I18n.lang('admin', 'without_privileges', 'Without privileges'), 1: I18n.lang('admin', 'selected_privileges', 'Selected privileges'), 2: I18n.lang('admin', 'administrator', 'Administrator')} + user_admin.forms['privileges'].arr_select={0: _('Without privileges'), 1: _('Selected privileges'), 2: _('Administrator')} - user_admin.forms['dark_theme'].arr_select={0: i18n.tlang('Light theme'), 1: i18n.tlang('Dark theme')} + user_admin.forms['dark_theme'].arr_select={0: _('Light theme'), 1: _('Dark theme')} - user_admin.forms['double_auth'].arr_select={0: i18n.tlang('No'), 1: i18n.tlang('Yes')} + user_admin.forms['double_auth'].arr_select={0: _('No'), 1: _('Yes')} user_admin.fields['password'].protected=False @@ -83,7 +83,7 @@ def admin_users(session={}): db.close() - return t.load_template('users.phtml', title=i18n.tlang('Admin users'), tlang=i18n.tlang, module_selected='admin_app.admin_users', slist=slist, session=session) + return t.load_template('users.phtml', title=_('Admin users'), tlang=_, module_selected='admin_app.admin_users', slist=slist, session=session) @admin_app.get('/ausers/permissions/', name="admin_app.admin_permissions") @admin_app.post('/ausers/permissions/', name="admin_app.admin_permissions") @@ -129,18 +129,18 @@ def admin_permissions(user_id, session={}): privileges_admin=admin.show() else: - arr_user={'username': i18n.tlang('User not found')} + arr_user={'username': _('User not found')} privileges_admin='' db.close() - return t.load_template('access.phtml', title=i18n.tlang('Users permissions'), privileges_admin=privileges_admin, user=arr_user, module_selected='admin_app.admin_users') + return t.load_template('access.phtml', title=_('Users permissions'), privileges_admin=privileges_admin, user=arr_user, module_selected='admin_app.admin_users') def user_options(url, id, arr_row): options=[] - options.append(''+I18n.lang('common', 'edit', 'Edit')+'') + options.append(''+_('Edit')+'') if not arr_row['privileges']: - options.append(f''+i18n.tlang('User access')+'') - options.append(''+I18n.lang('common', 'delete', 'Delete')+'') + options.append(f''+_('User access')+'') + options.append(''+_('Delete')+'') return options