Created
October 13, 2015 08:39
-
-
Save oprypin/9a37c2c8e04ffa475542 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
from flask import Flask, g | |
app = Flask(__name__) | |
languages = ['en', 'hr'] | |
def translate_to(lang, text): | |
return { | |
'/about': { | |
'hr': '/onama', | |
}, | |
"Hello, World": { | |
'hr': "Bok svijete", | |
} | |
}[text].get(lang, text) | |
def int_route(path): | |
def decorator(f): | |
@functools.wraps(f) | |
def decorated(lang, *args, **kwargs): | |
g.lang = lang | |
return f(*args, **kwargs) | |
for lang in languages: | |
app.route('/' + lang + translate_to(lang, path), defaults={'lang': lang})(decorated) | |
return decorated | |
return decorator | |
@int_route('/about') | |
def about(): | |
return translate_to(g.lang, "Hello, World") | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment