Skip to content

Commit 92017e5

Browse files
25.09.24_translation fonctionnel
1 parent f448e27 commit 92017e5

3 files changed

Lines changed: 23 additions & 21 deletions

File tree

flask_app/__init__.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
app.config['SECRET_KEY'] = 'secret_key' #! change that for deployment
2626
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
2727
app.config['BABEL_TRANSLATION_DIRECTORIES'] = f'{app.root_path}/translations'
28-
app.config['SESSION_COOKIE_PATH'] = '/'
29-
app.config['SESSION_COOKIE_DOMAIN'] = 'localhost'
30-
ic(app.config)
3128
app.jinja_env.add_extension('jinja2.ext.loopcontrols')
3229
app.url_map.default_subdomain = ''
3330

@@ -98,11 +95,11 @@ def decorated_view(*args, **kwargs):
9895
return decorated_view
9996

10097
def lang_url_for(*args, **kwargs):
101-
lang = kwargs.pop('lang', _('app.lang'))
102-
if 'static' in args:
98+
if 'static' in args or kwargs.get('lang'):
99+
return url_for(*args, **kwargs)
100+
lang = _('app.lang')
101+
if lang == request.accept_languages.best_match(LANGAGES):
103102
return url_for(*args, **kwargs)
104-
if lang == 'fr':
105-
return url_for(*args, lang='', **kwargs)
106103
return url_for(*args, lang=lang, **kwargs)
107104

108105
@app.context_processor
@@ -112,7 +109,7 @@ def inject_lang_url_for():
112109
def set_route(blueprint, path, **options):
113110
def decorator(func):
114111
@blueprint.route(path, **options)
115-
@blueprint.route(path, subdomain=options.pop('subdomain', '<lang>'), **options)
112+
@blueprint.route(f'/<lang>{path}', **options)
116113
@wraps(func)
117114
def wrap(*args, **kwargs):
118115
lang = kwargs.pop('lang', 'fr')

flask_app/routes.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ def home():
2323

2424
@app.route('/lang/<lang>/<path:next>')
2525
def change_lang(lang, next:str):
26-
ic(lang, next)
27-
next=next.removeprefix('http://')
28-
ic(lang, next)
29-
ic('.'.join(next.split('.')[1:]), next.find(app.config['SERVER_NAME']))
30-
next = '.'.join(next.split('.')[1:]) if next.find(app.config['SERVER_NAME'])!=0 else next
31-
ic(lang, next)
32-
return redirect('http://'+lang+'.'+next)
26+
lang_index = next.find(app.config['SERVER_NAME']) + len(app.config['SERVER_NAME'])
27+
first = next[lang_index+1:].split('/')[0]
28+
if first in LANGAGES:
29+
if lang == request.accept_languages.best_match(LANGAGES):
30+
return redirect(f'{next[:lang_index]}{next[lang_index+len(lang)+1:]}')
31+
else:
32+
return redirect(f'{next[:lang_index]}/{lang}{next[lang_index+len(lang)+1::]}')
33+
else:
34+
if lang == request.accept_languages.best_match(LANGAGES):
35+
return redirect(next)
36+
else:
37+
return redirect(f'{next[:lang_index]}/{lang}{next[lang_index:]}')

flask_app/view/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from datetime import datetime
77
from flask_babel import _
88

9-
view = Blueprint('view', __name__, template_folder='templates', url_prefix='/view')
9+
view = Blueprint('view', __name__, template_folder='templates')
1010

1111
def deg_to_dms(deg):
1212
"""Convert from decimal degrees to degrees, minutes, seconds."""
@@ -17,7 +17,7 @@ def deg_to_dms(deg):
1717
d, m = int(d), int(m)
1818
return d, m, s
1919

20-
@set_route(view, '/inscription/<inscription>/delete')
20+
@set_route(view, '/view/inscription/<inscription>/delete')
2121
@login_required
2222
def delete_inscription(inscription):
2323
user = current_user
@@ -28,7 +28,7 @@ def delete_inscription(inscription):
2828
return redirect(url_for('home'))
2929

3030

31-
@set_route(view, '/inscription/<inscription>')
31+
@set_route(view, '/view/inscription/<inscription>')
3232
@login_required
3333
def view_inscription_page(inscription):
3434
user = current_user
@@ -56,13 +56,13 @@ def view_inscription_page(inscription):
5656

5757
return render_template('view_inscription.html', user_data=user, inscription=inscription, folium_map=folium_map, rdv_url=rdv_url)
5858

59-
@set_route(view, '/<event>')
59+
@set_route(view, '/view/<event>')
6060
def view_event_page(event):
6161
user_data = current_user if current_user.is_authenticated else None
6262
event = Event.query.filter_by(name=event).first_or_404(_('view.error.eventdontexist:event').format(event=event))
6363
return render_template('view_event.html', user_data = user_data, event_data=event, time = datetime.now())
6464

65-
@set_route(view, '/<event>/edition/<edition>')
65+
@set_route(view, '/view/<event>/edition/<edition>')
6666
def view_edition_page(event, edition):
6767
user = current_user if current_user.is_authenticated else None
6868
event = Event.query.filter_by(name=event).first_or_404(_('view.error.eventdontexist:event').format(event=event))
@@ -71,7 +71,7 @@ def view_edition_page(event, edition):
7171
rdv_url= "https://www.google.com/maps/place/{0}%C2%B0{1}'{2}".format(*deg_to_dms(edition.rdv_lat))+"%22N+{0}%C2%B0{1}'{2}".format(*deg_to_dms(edition.rdv_lng))+f"%22E/@{edition.rdv_lat},{edition.rdv_lng},15z"
7272
return render_template('view_edition.html', user_data = user, event_data = event, edition_data=edition, rdv_url=rdv_url, time= datetime.now())
7373

74-
@set_route(view, '/<event>/parcours/<parcours>')
74+
@set_route(view, '/view/<event>/parcours/<parcours>')
7575
def view_parcours_page(event, parcours):
7676
user_data = current_user if current_user.is_authenticated else None
7777
event = Event.query.filter_by(name=event).first_or_404(_('view.error.eventdontexist:event').format(event=event))

0 commit comments

Comments
 (0)