Skip to content

Commit

Permalink
Falsk-Babel 5 lang
Browse files Browse the repository at this point in the history
  • Loading branch information
tiritibambix committed Jan 12, 2025
1 parent a890abc commit d6f04a1
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 39 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ WORKDIR /app
COPY . /app

# Installer les dépendances Python
RUN pip install --no-cache-dir -r /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt \
&& pip install --no-cache-dir Flask-Babel

# Compiler les fichiers de traduction
RUN pybabel compile -d translations

# Exposer le port
EXPOSE 5000
Expand Down
45 changes: 19 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests
import logging
import re
from translations import TRANSLATIONS
from flask_babel import Babel, gettext as _

# Configuration
UPLOAD_FOLDER = 'uploads'
Expand All @@ -25,11 +25,27 @@
os.makedirs(OUTPUT_FOLDER, exist_ok=True)

app = Flask(__name__)
babel = Babel(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
app.config['BABEL_DEFAULT_LOCALE'] = 'en'
app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'translations'
app.secret_key = 'supersecretkey'
app.logger.setLevel(logging.INFO)

@babel.localeselector
def get_locale():
# Essaie d'obtenir la langue depuis le paramètre 'lang' de l'URL
lang = request.args.get('lang')
if lang and lang in LANGUAGES:
session['lang'] = lang
return lang
# Essaie d'obtenir la langue depuis la session
if 'lang' in session and session['lang'] in LANGUAGES:
return session['lang']
# Sinon, utilise la langue du navigateur
return request.accept_languages.best_match(LANGUAGES)

def allowed_file(filename):
"""Allow all file types supported by ImageMagick."""
return '.' in filename
Expand Down Expand Up @@ -168,29 +184,6 @@ def get_recommended_formats(image_type):
'compatible': compatible
}

def get_user_language():
"""Get user's preferred language."""
# Essaie d'obtenir la langue depuis le paramètre 'lang' de l'URL
lang = request.args.get('lang')
if lang in LANGUAGES:
session['lang'] = lang
return lang
# Essaie d'obtenir la langue depuis la session
if 'lang' in session and session['lang'] in LANGUAGES:
return session['lang']
# Par défaut, utilise l'anglais
return 'en'

def translate(key):
"""Translate a key into the current language."""
lang = get_user_language()
return TRANSLATIONS[lang].get(key, key)

@app.context_processor
def utility_processor():
"""Make translation function available in templates."""
return {'translate': translate}

def analyze_image_type(filepath):
"""Analyze image to determine its type and best suitable formats."""
try:
Expand Down Expand Up @@ -320,12 +313,12 @@ def index():
def upload_file():
"""Handle file uploads."""
if 'file' not in request.files:
flash(translate('no_file_selected'), 'error')
flash(_('No file selected'), 'error')
return redirect(url_for('index'))

files = request.files.getlist('file')
if not files or all(file.filename == '' for file in files):
flash(translate('select_at_least_one_file'), 'error')
flash(_('Please select at least one file'), 'error')
return redirect(url_for('index'))

uploaded_files = []
Expand Down
3 changes: 3 additions & 0 deletions babel.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
47 changes: 47 additions & 0 deletions messages.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Translations template for ImaGUIck.
# Copyright (C) 2025 ImaGUIck
# This file is distributed under the same license as the ImaGUIck project.
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-12 14:20+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "No file selected"
msgstr ""

msgid "Please select at least one file"
msgstr ""

msgid "Upload Images"
msgstr ""

msgid "Drag and drop files here, or click to select files"
msgstr ""

msgid "Upload"
msgstr ""

msgid "Upload Image by URL"
msgstr ""

msgid "Enter image URL"
msgstr ""

msgid "Download and Upload"
msgstr ""

msgid "Keep original format"
msgstr ""

msgid "Recommended formats"
msgstr ""

msgid "Other compatible formats"
msgstr ""
12 changes: 6 additions & 6 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,23 @@
</div>
</fieldset>
<fieldset>
<legend>{{ translate('upload_images') }}</legend>
<legend>{{ _('Upload Images') }}</legend>
<div class="form-group">
<!-- Drag and Drop Zone -->
<div id="drop-zone" class="drop-zone">
<p>{{ translate('drag_and_drop') }}</p>
<p>{{ _('Drag and drop files here, or click to select files') }}</p>
<input type="file" id="file-input" name="file" multiple hidden>
</div>
<button type="submit">{{ translate('upload') }}</button>
<button type="submit">{{ _('Upload') }}</button>
</div>
</fieldset>
</form>
<form action="/upload_url" method="post">
<fieldset>
<legend>{{ translate('upload_by_url') }}</legend>
<legend>{{ _('Upload Image by URL') }}</legend>
<div class="form-group">
<input type="text" name="url" placeholder="{{ translate('enter_url') }}" required>
<button type="submit">{{ translate('download_and_upload') }}</button>
<input type="text" name="url" placeholder="{{ _('Enter image URL') }}" required>
<button type="submit">{{ _('Download and Upload') }}</button>
</div>
</fieldset>
</form>
Expand Down
12 changes: 6 additions & 6 deletions templates/resize.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,26 @@ <h4>Resize Image: {{ filename }}</h4>
<div class="form-group">
<label for="format">Convert to format:</label>
<select name="format" id="format">
<option value="">{{ translate('keep_original') }}</option>
<option value="">{{ _('Keep original format') }}</option>
{% if formats.recommended %}
<optgroup label="{{ translate('recommended_formats') }}">
<optgroup label="{{ _('Recommended formats') }}">
{% for fmt in formats.recommended %}
<option value="{{ fmt|lower }}">{{ fmt }}
{% if image_type %}
{% if (image_type.has_transparency and fmt in ['PNG', 'WebP', 'AVIF']) %}
(Best for transparency)
({{ _('Best for transparency') }})
{% elif (image_type.is_photo and fmt in ['JPEG', 'WebP', 'AVIF']) %}
(Best for photos)
({{ _('Best for photos') }})
{% elif (not image_type.is_photo and fmt in ['PNG', 'GIF', 'WebP']) %}
(Best for graphics)
({{ _('Best for graphics') }})
{% endif %}
{% endif %}
</option>
{% endfor %}
</optgroup>
{% endif %}
{% if formats.compatible %}
<optgroup label="{{ translate('compatible_formats') }}">
<optgroup label="{{ _('Other compatible formats') }}">
{% for fmt in formats.compatible %}
<option value="{{ fmt|lower }}">{{ fmt }}</option>
{% endfor %}
Expand Down
58 changes: 58 additions & 0 deletions translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# English translations for ImaGUIck.
# Copyright (C) 2025 ImaGUIck
# This file is distributed under the same license as the ImaGUIck project.
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-12 14:22+0100\n"
"PO-Revision-Date: 2025-01-12 14:22+0100\n"
"Last-Translator: ImaGUIck Team\n"
"Language: en\n"
"Language-Team: English\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "No file selected"
msgstr "No file selected"

msgid "Please select at least one file"
msgstr "Please select at least one file"

msgid "Upload Images"
msgstr "Upload Images"

msgid "Drag and drop files here, or click to select files"
msgstr "Drag and drop files here, or click to select files"

msgid "Upload"
msgstr "Upload"

msgid "Upload Image by URL"
msgstr "Upload Image by URL"

msgid "Enter image URL"
msgstr "Enter image URL"

msgid "Download and Upload"
msgstr "Download and Upload"

msgid "Keep original format"
msgstr "Keep original format"

msgid "Recommended formats"
msgstr "Recommended formats"

msgid "Other compatible formats"
msgstr "Other compatible formats"

msgid "Best for transparency"
msgstr "Best for transparency"

msgid "Best for photos"
msgstr "Best for photos"

msgid "Best for graphics"
msgstr "Best for graphics"
58 changes: 58 additions & 0 deletions translations/es/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Spanish translations for ImaGUIck.
# Copyright (C) 2025 ImaGUIck
# This file is distributed under the same license as the ImaGUIck project.
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-12 14:22+0100\n"
"PO-Revision-Date: 2025-01-12 14:22+0100\n"
"Last-Translator: ImaGUIck Team\n"
"Language: es\n"
"Language-Team: Spanish\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "No file selected"
msgstr "Ningún archivo seleccionado"

msgid "Please select at least one file"
msgstr "Por favor, seleccione al menos un archivo"

msgid "Upload Images"
msgstr "Subir imágenes"

msgid "Drag and drop files here, or click to select files"
msgstr "Arrastre y suelte archivos aquí, o haga clic para seleccionar"

msgid "Upload"
msgstr "Subir"

msgid "Upload Image by URL"
msgstr "Subir imagen por URL"

msgid "Enter image URL"
msgstr "Ingrese la URL de la imagen"

msgid "Download and Upload"
msgstr "Descargar y subir"

msgid "Keep original format"
msgstr "Mantener formato original"

msgid "Recommended formats"
msgstr "Formatos recomendados"

msgid "Other compatible formats"
msgstr "Otros formatos compatibles"

msgid "Best for transparency"
msgstr "Mejor para transparencia"

msgid "Best for photos"
msgstr "Mejor para fotos"

msgid "Best for graphics"
msgstr "Mejor para gráficos"
58 changes: 58 additions & 0 deletions translations/fr/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# French translations for ImaGUIck.
# Copyright (C) 2025 ImaGUIck
# This file is distributed under the same license as the ImaGUIck project.
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-12 14:20+0100\n"
"PO-Revision-Date: 2025-01-12 14:20+0100\n"
"Last-Translator: ImaGUIck Team\n"
"Language: fr\n"
"Language-Team: French\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

msgid "No file selected"
msgstr "Aucun fichier sélectionné"

msgid "Please select at least one file"
msgstr "Veuillez sélectionner au moins un fichier"

msgid "Upload Images"
msgstr "Importer des images"

msgid "Drag and drop files here, or click to select files"
msgstr "Glissez et déposez vos fichiers ici, ou cliquez pour sélectionner"

msgid "Upload"
msgstr "Importer"

msgid "Upload Image by URL"
msgstr "Importer une image depuis une URL"

msgid "Enter image URL"
msgstr "Entrez l'URL de l'image"

msgid "Download and Upload"
msgstr "Télécharger et importer"

msgid "Keep original format"
msgstr "Conserver le format d'origine"

msgid "Recommended formats"
msgstr "Formats recommandés"

msgid "Other compatible formats"
msgstr "Autres formats compatibles"

msgid "Best for transparency"
msgstr "Idéal pour la transparence"

msgid "Best for photos"
msgstr "Idéal pour les photos"

msgid "Best for graphics"
msgstr "Idéal pour les graphiques"
Loading

0 comments on commit d6f04a1

Please sign in to comment.