Skip to content

Commit

Permalink
restored dutch translations
Browse files Browse the repository at this point in the history
  • Loading branch information
lingthio committed Oct 3, 2014
1 parent dc780e3 commit 94908aa
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 180 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ docs/source/includes/*_app.py
example_apps/translations/

# Private files
example_apps/local_settings.py
example_apps/local_settings.py
example_apps/translations/
14 changes: 8 additions & 6 deletions example_apps/test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import Flask, redirect, render_template_string, request, url_for
#from flask.ext.babel import Babel
from flask.ext.babel import Babel
from flask.ext.mail import Mail
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.user import current_user, login_required, UserManager, UserMixin, SQLAlchemyAdapter
Expand Down Expand Up @@ -44,14 +44,16 @@ def create_app(test_config=None): # For automated tests
app.config.update(test_config)

# Initialize Flask extensions
# babel = Babel(app) # Initialize Flask-Babel
babel = Babel(app) # Initialize Flask-Babel
db = SQLAlchemy(app) # Initialize Flask-SQLAlchemy
mail = Mail(app) # Initialize Flask-Mail

# @babel.localeselector
# def get_locale():
# translations = [str(translation) for translation in babel.list_translations()]
# return request.accept_languages.best_match(translations)
@babel.localeselector
def get_locale():
translations = [str(translation) for translation in babel.list_translations()]
language = request.accept_languages.best_match(translations)
print('translations=',repr(translations), 'language=', repr(language))
return language

# Define User model. Make sure to add flask.ext.user UserMixin!!
class User(db.Model, UserMixin):
Expand Down
4 changes: 4 additions & 0 deletions flask_user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def init_app(self, app):
translations.gettext,
translations.ngettext,
newstyle=True)
else:
app.jinja_env.add_extension('jinja2.ext.i18n')
app.jinja_env.install_null_translations()


# Create password_crypt_context if needed
if not self.password_crypt_context:
Expand Down
116 changes: 66 additions & 50 deletions flask_user/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,74 @@
:author: Ling Thio ([email protected])
:license: Simplified BSD License, see LICENSE.txt for more details."""

import os
import gettext as python_gettext

from flask import _request_ctx_stack, current_app, render_template
from flask.ext.babel import get_locale, support

def get_translations():
""" Search the Application directory and the Flask-User directory for the
Flask-User translations file and return a Translations() object."""
ctx = _request_ctx_stack.top
if ctx is None:
return None
translations = getattr(ctx, 'flask_user_translations', None)
if translations is None:
# Prepare settings
domain = 'flask_user'
locales = [get_locale()]
languages = [str(locale) for locale in locales]
# Search Application directory
app_dir = os.path.join(current_app.root_path, 'translations')
filename = python_gettext.find(domain, app_dir, languages)
if filename:
translations = support.Translations.load(app_dir, locales, domain=domain)
else:
# Search Flask-User directory
flask_user_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'translations')
translations = support.Translations.load(flask_user_dir, locales, domain=domain)

ctx.flask_user_translations = translations

return ctx.flask_user_translations

def gettext(string, **variables):
""" Translate specified string."""
translations = get_translations()
if not translations:
return string % variables
return translations.ugettext(string) % variables

def ngettext(singular, plural, num, **variables):
""" Translate a singular/plural string based on the number 'num'."""
translations = get_translations()
variables.setdefault('num', num)
if not translations:
return (singular if num == 1 else plural) % variables
return translations.ungettext(singular, plural, num) % variables

def lazy_gettext(string, **variables):
""" Similar to 'gettext' but the string returned is lazy which means
it will be translated when it is used as an actual string."""
from speaklater import make_lazy_string
return make_lazy_string(gettext, string, **variables)
# Flask-User can be used with or without Flask-Babel and Babel
try:
from flask.ext.babel import get_locale, support

# Flask-Babel is installed
import os
from flask import _request_ctx_stack, current_app, render_template

def get_translations():
""" Search the Application directory and the Flask-User directory for the
Flask-User translations file and return a Translations() object."""
ctx = _request_ctx_stack.top
if ctx is None:
return None
translations = getattr(ctx, 'flask_user_translations', None)
if translations is None:
# Prepare settings
domain = 'flask_user'
locales = [get_locale()]
languages = [str(locale) for locale in locales]
# Search Application directory
app_dir = os.path.join(current_app.root_path, 'translations')
filename = python_gettext.find(domain, app_dir, languages)
if filename:
translations = support.Translations.load(app_dir, locales, domain=domain)
else:
# Search Flask-User directory
flask_user_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'translations')
translations = support.Translations.load(flask_user_dir, locales, domain=domain)

ctx.flask_user_translations = translations

return ctx.flask_user_translations

def gettext(string, **variables):
""" Translate specified string."""
translations = get_translations()
if not translations:
return string % variables
return translations.ugettext(string) % variables

def ngettext(singular, plural, num, **variables):
""" Translate a singular/plural string based on the number 'num'."""
translations = get_translations()
variables.setdefault('num', num)
if not translations:
return (singular if num == 1 else plural) % variables
return translations.ungettext(singular, plural, num) % variables

def lazy_gettext(string, **variables):
""" Similar to 'gettext' but the string returned is lazy which means
it will be translated when it is used as an actual string."""
from speaklater import make_lazy_string
return make_lazy_string(gettext, string, **variables)

print('translations is using Flask-Babel')

except ImportError:
# Flask-Babel has not been installed
def gettext(string, **variables):
return python_gettext.gettext(string, **variables)

def lazy_gettext(string, **variables):
return python_gettext.gettext(string, **variables)

print('translations is using python gettext')

_ = lazy_gettext
_home_page = _('Home Page')
Expand Down
Binary file modified flask_user/translations/en/LC_MESSAGES/flask_user.mo
Binary file not shown.
48 changes: 26 additions & 22 deletions flask_user/translations/en/LC_MESSAGES/flask_user.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2014-10-01 17:36-0700\n"
"POT-Creation-Date: 2014-10-03 10:53-0700\n"
"PO-Revision-Date: 2014-10-01 17:36-0700\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: en <[email protected]>\n"
Expand Down Expand Up @@ -151,7 +151,7 @@ msgid "Password and Retype Password did not match"
msgstr ""

#: flask_user/forms.py:234
#: flask_user/templates/flask_user/login_or_register.html:36
#: flask_user/templates/flask_user/login_or_register.html:39
#: flask_user/templates/flask_user/register.html:5
msgid "Register"
msgstr ""
Expand All @@ -160,15 +160,15 @@ msgstr ""
msgid "Resend email confirmation email"
msgstr ""

#: flask_user/translations.py:66
#: flask_user/translations.py:82
msgid "Home Page"
msgstr ""

#: flask_user/translations.py:67
#: flask_user/translations.py:83
msgid "Profile Page"
msgstr ""

#: flask_user/translations.py:68
#: flask_user/translations.py:84
msgid "Special Page"
msgstr ""

Expand Down Expand Up @@ -201,9 +201,10 @@ msgid ""
msgstr ""

#: flask_user/views.py:268
#, python-format
msgid ""
"Your email address has not yet been confirmed. Check your email Inbox and"
" Spam folders for the confirmation email or <a href=\"\">Re-send "
" Spam folders for the confirmation email or <a href=\"%(url)s\">Re-send "
"confirmation email</a>."
msgstr ""

Expand All @@ -215,42 +216,42 @@ msgstr ""
msgid "You have signed out successfully."
msgstr ""

#: flask_user/views.py:446
#: flask_user/views.py:455
msgid "Your reset password token has expired."
msgstr ""

#: flask_user/views.py:450 flask_user/views.py:461
#: flask_user/views.py:459 flask_user/views.py:470
msgid "Your reset password token is invalid."
msgstr ""

#: flask_user/views.py:483
#: flask_user/views.py:492
msgid ""
"Your password has been reset successfully. Please sign in with your new "
"password"
msgstr ""

#: flask_user/views.py:535
#: flask_user/views.py:517
#, python-format
msgid ""
"A confirmation email has been sent to %(email)s with instructions to "
"complete your registration."
msgid "You must be signed in to access '%(url)s'."
msgstr ""

#: flask_user/views.py:537
msgid "You have registered successfully."
#: flask_user/views.py:530
#, python-format
msgid "You do not have permission to access '%(url)s'."
msgstr ""

#: flask_user/views.py:544
#: flask_user/views.py:553 flask_user/views.py:574
#, python-format
msgid "You must be signed in to access '%(url)s'."
msgid ""
"A confirmation email has been sent to %(email)s with instructions to "
"complete your registration."
msgstr ""

#: flask_user/views.py:557
#, python-format
msgid "You do not have permission to access '%(url)s'."
#: flask_user/views.py:555
msgid "You have registered successfully."
msgstr ""

#: flask_user/views.py:572
#: flask_user/views.py:587
msgid "You have signed in successfully."
msgstr ""

Expand All @@ -263,7 +264,7 @@ msgid "New here? Register."
msgstr ""

#: flask_user/templates/flask_user/login.html:44
#: flask_user/templates/flask_user/login_or_register.html:29
#: flask_user/templates/flask_user/login_or_register.html:32
msgid "Forgot your Password?"
msgstr ""

Expand Down Expand Up @@ -291,3 +292,6 @@ msgid_plural "I have %(count)s apples"
msgstr[0] ""
msgstr[1] ""

#~ msgid ""
#~ msgstr ""

Loading

0 comments on commit 94908aa

Please sign in to comment.