trying to internationalize my bottlepy app

40 Views Asked by At

Sorry to bother, but I'm really having a hard time trying to implement i18n to my bottle app. I try to be concise: So I wrote a code for bottle_utils.i18n usage as it was in docs. And the problem is my app stopped working because of the autoadding prefixes (like 'en_US') in my URL. Like so: 127.0.0.1:8080/test becomes 127.0.0.1:8080/en_US/test.... And when I try to add en_US to my route (it looks like this @route('/test')) -> @route('/en_US/test') my URL would look like 127.0.0.1/en_US/en_US/test.....

I really don't know what to do. I have searched the internet, I tried asking LLMs - nothing! Please, could you kindly help? This is my main.py code:

import os
import bottle
from bottle import run, route, template, static_file, url, request, redirect, BaseTemplate
from bottle_utils.i18n import I18NPlugin, lazy_gettext as _, lazy_ngettext as ngettext
from bottle_utils.i18n import i18n_view
from services_list import ServicesList
from projects_list import ProjectsList
import requests

LANGS = [
    ('en_US', 'English'),
    ('ru_RU', 'Русский')
]
DEFAULT_LOCAL = 'en_US'
LOCALES_DIR = './locales'

app = bottle.default_app()
BaseTemplate.defaults['get_url'] = app.get_url
wsgi_app = I18NPlugin(app, langs=LANGS, default_locale=DEFAULT_LOCAL, locale_dir=LOCALES_DIR, domain='test')
app.install(wsgi_app)

@route('/static/<filename:path>', name='static')
def serve_static(filename):
    return static_file(filename, root='./views/static')

# @route('/static/<filename:re:.*\.css>', name='static')
# def send_css(filename):
#     print("css:", filename)
#     return static_file(filename, root='./views/static', mimetype='text/css')

@route('/')
def home():
    page = template('index')
    return page

@route('/about')
def about():
    page = template('about')
    return page

@route('/services')
def services():
    page = template('services', services=ServicesList)
    return page

@route('/contact_us')
def contact_us():
    page = template('contact_us')
    return page

@route('/projects')
def projects():
    page = template('projects', projects=ProjectsList)
    return page

@route('/projects/<dir>')
def display_project(dir):
    project = next((p for p in ProjectsList if p['dir'] == dir), None)
    if project:
        return template('project.tpl', dir=project['dir'], title=project['title'], scope=project['scope'], etd=project['ETD'], main=project['images'][0], images=project['images'])
    else:
        return "<h2>Project not found</h2>"

@route('/confirmation')
def confirmation_page():
    page = template('confirmation')
    return page

@route('/confirmation', method='POST')
def send_confirmation():
    name = request.forms.get('fullName')
    email = request.forms.get('email')
    mobile = request.forms.get('mobile')
    message = request.forms.get('message')
    text = f"<b>You have a new application!</b>\nName - {name}\nEmail - {email}\nNumber - +971{mobile}\nMessage - {message}\n<i>Keep up the good work!</i>"

    def send_message(form):
        botToken = 'BOT_TOKEN'
        chat_id = 'CHAT_ID'
        url = f"https://api.telegram.org/bot{botToken}/sendMessage?chat_id={chat_id}&text={text}&parse_mode=html"

        try:
            response = requests.get(url)
            response.raise_for_status()
            return response.json()
        except requests.exceptions.RequestException as e:
            return {"error": str(e)}

    result = send_message(text)
    if 'error' in result:
        # Handle error here
        return template('error_template', error=result['error'])
    else:
        return template('confirmation', name=name, email=email, mobile=mobile, message=message)
    return result

@route('/test')
def testing():
    page = template('test')
    return page
    # return {}

run(debug=True, reloader=True, port=1100)

I was expecting that when I try to access the website it would serve the pages like it did before using the I18NPlugin, however the result is : error 404. Also, i tried to use @i18n_view decorator but it didn't help either. In addition, I was thinking about redirecting routes, but in my opinion this is not how it should be done. And there has to be a better way to resolve this issue...

0

There are 0 best solutions below