forked from lingthio/Flask-User
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
223 additions
and
180 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 "" | ||
|
@@ -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 "" | ||
|
||
|
@@ -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 "" | ||
|
||
|
@@ -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 "" | ||
|
||
|
@@ -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 "" | ||
|
||
|
@@ -291,3 +292,6 @@ msgid_plural "I have %(count)s apples" | |
msgstr[0] "" | ||
msgstr[1] "" | ||
|
||
#~ msgid "" | ||
#~ msgstr "" | ||
|
Oops, something went wrong.