0

I have several translations (into different languages) of the same static html file. Those were obtained via Sphinx-Python, using the internationalization feature, which ends up with mo files. All this works fine.

However, I have no clue on how to allow the user's browser to choose the proper translation that corresponds to the Accept-Languages header sent by the browser itself.

The static files will be served via Flask's send_static_file.

Thanks a lot for any hint on how it's possible to force the browser to select the "good" translation from a bunch of static files.

Machavity
  • 30,841
  • 27
  • 92
  • 100
mannaia
  • 860
  • 2
  • 11
  • 27
  • Could you put the name of the files you have and the view function for static files? It is not clear for me which files you have in your static folder and whether you use template translation or you have purely html files with different contents? – mehdix Sep 04 '14 at 11:50
  • @MehdiSadeghi I have html files with different contents (or better saying, the same content translated into different languages). They are html files obtained via Sphinx-Python. – mannaia Sep 04 '14 at 12:26

1 Answers1

1

I have a solution that I'd like to share.

I have followed the instructions given here, that are applicable as long as the production server is Apache. The following lines are added for every translation of the same Sphinx files.

Alias /doc_en /location/of/sphinx/english_version/html
<Directory /location/of/sphinx/english_version/html>
    Order deny,allow
    Allow from all
</Directory>

From the Flask's side, one can steer the user to the most appropriate translation (as defined by user's browser), with the help of Flask-Babel extension:

app = Flask(__name__)
app.config.from_pyfile('mysettings.cfg')
babel = Babel(app)

@babel.localeselector
def get_locale():
    bestl = request.accept_languages.best_match(['de', 'fr', 'en'])
    if bestl:
        return request.accept_languages.best_match(['de', 'fr', 'en'])
    else:
        return "en"

Then, static file is served via send_static_file:

@app.route('/<dir>/<filename>', methods=['GET'])
def doc(dir, filename):
    path = os.path.join(dir, filename)
    return app.send_static_file(path)

@app.route('/help')
def docs():
    return redirect(url_for('doc', dir = 'doc_'+get_locale(), filename='index.html'))
Community
  • 1
  • 1
mannaia
  • 860
  • 2
  • 11
  • 27