Skip to content

Flask Application with Multi-Language Support (TR-EN-DE-FR)

License

Notifications You must be signed in to change notification settings

nuriozbey/flask-babel-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💠 Python-Flask-Babel-Example

GitHub issues GitHub stars GitHub license GitHub last commit GitHub repo size GitHub contributors GitHub watchers

Flask Application with Multi-Language Support (TR-EN-DE-FR) flask-babel

👍 Installation

Clone Repository

git clone https://github.com/nuriozbey/flask-babel-example.git

Create Virtual Environment

python -m venv venv

Activate Virtual Environment

venv\Scripts\activate

Change Directory

cd flask-babel-example

📌 Requirements

pip install flask
pip install flask-babel
pip install pybabel

Install Requirements

pip install -r requirements.txt

🚀 Usage

python .\main.py

📝 Babel Configuration

# config.py
BABEL_DEFAULT_LOCALE = 'tr'
BABEL_DEFAULT_TIMEZONE = 'Europe/Istanbul'
BABEL_TRANSLATION_DIRECTORIES = 'translations'

Translation

# main.py
from flask_babel import _

@app.route('/')
def index():
    return render_template('index.html', title=_('Home'))

📝 Translation File Example

# translations/tr/LC_MESSAGES/messages.po
msgid "Home"
msgstr "Ana Sayfa"

Babel.cfg File example

# babel.cfg
[python: **.py]
[jinja2: **/templates/**.html]
[extractors]
jinja2 = jinja2.ext.babel_extract

Extract and Compile

pybabel extract -F babel.cfg -o messages.pot .

 

pybabel init -i messages.pot -d translations -l tr
pybabel init -i messages.pot -d translations -l de
pybabel init -i messages.pot -d translations -l fr
pybabel init -i messages.pot -d translations -l es
pybabel compile -d translations

Run Application

python .\main.py

 

🎯 Update Translation Files (Extract and Compile)

 

pybabel extract -F babel.cfg -o messages.pot . --no-wrap

 

pybabel update -i messages.pot -d translations --no-wrap

 

pybabel compile -d translations

 

Run Application

python .\main.py

📢 License

GNU General Public License v3.0

📧 Contact

Linkedin Badge Medium Badge Gmail Badge Github Badge Portfolio Badge

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

About

Flask Application with Multi-Language Support (TR-EN-DE-FR)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published