def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = stack.top if ctx is None: return NullTranslations() locale = get_locale() cache = self.get_translations_cache(ctx) translations = cache.get(str(locale)) if translations is None: translations_dir = self.get_translations_path(ctx) translations = Translations.load(translations_dir, locale, domain=self.domain) # Load plugins translations if isinstance(translations, Translations): # Load core extensions translations from wtforms.i18n import messages_path wtforms_translations = Translations.load(messages_path(), locale, domain='wtforms') translations.merge(wtforms_translations) import flask_security flask_security_translations = Translations.load( join(flask_security.__path__[0], 'translations'), locale, domain='flask_security' ) translations.merge(flask_security_translations) for pkg in entrypoints.get_roots(current_app): package = pkgutil.get_loader(pkg) path = join(package.filename, 'translations') domains = [f.replace(path, '').replace('.pot', '')[1:] for f in iglob(join(path, '*.pot'))] for domain in domains: translations.merge(Translations.load(path, locale, domain=domain)) # Allows the theme to provide or override translations from . import theme theme_translations_dir = join(theme.current.path, 'translations') if exists(theme_translations_dir): domain = theme.current.identifier theme_translations = Translations.load(theme_translations_dir, locale, domain=domain) translations.merge(theme_translations) cache[str(locale)] = translations return translations
def get_translations(self): ''' Override to merge wtforms translations with flask-admin ''' ctx = _request_ctx_stack.top if ctx is None: return NullTranslations() locale = get_locale() cache = self.get_translations_cache(ctx) translations = cache.get(str(locale)) if translations is None: dirname = self.get_translations_path(ctx) translations = support.Translations.load(dirname, locale, domain=self.domain) # start of overridden section - merging wtforms translations wtf_translations = support.Translations.load(messages_path(), locale, domain='wtforms') translations.merge(wtf_translations) cache[str(locale)] = translations return translations
def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = stack.top if ctx is None: return NullTranslations() locale = get_locale() cache = self.get_translations_cache(ctx) translations = cache.get(str(locale)) if translations is None: translations_dir = self.get_translations_path(ctx) translations = Translations.load(translations_dir, locale, domain=self.domain) # Load plugins translations if isinstance(translations, Translations): # Load core extensions translations from wtforms.i18n import messages_path wtforms_translations = Translations.load(messages_path(), locale, domain='wtforms') translations.merge(wtforms_translations) for plugin_name in current_app.config['PLUGINS']: module_name = 'udata_{0}'.format(plugin_name) module = import_module(module_name) translations_dir = join(dirname(module.__file__), 'translations') if exists(translations_dir): domain = '-'.join((self.domain, plugin_name)) plugins_translations = Translations.load( translations_dir, locale, domain=domain) translations.merge(plugins_translations) # Allows the theme to provide or override translations from . import theme theme_translations_dir = join(theme.current.path, 'translations') if exists(theme_translations_dir): domain = theme.current.identifier theme_translations = Translations.load(theme_translations, locale, domain=domain) translations.merge(theme_translations) cache[str(locale)] = translations return translations
class Translations: """Fixes WTForms translation support and uses wtforms translations.""" wtforms_domain = Domain(messages_path(), domain="wtforms") def gettext(self, string): return self.wtforms_domain.gettext(string) def ngettext(self, singular, plural, n): return self.wtforms_domain.ngettext(singular, plural, n)
def _get_translations(): """Returns the correct gettext translations. Copy from flask-babel with some modifications. """ ctx = _request_ctx_stack.top if ctx is None: return None # babel should be in extensions for get_locale if "babel" not in ctx.app.extensions: return None translations = getattr(ctx, "wtforms_translations", None) if translations is None: dirname = messages_path() translations = support.Translations.load(dirname, [get_locale()], domain="wtforms") ctx.wtforms_translations = translations return translations
def _get_translations(): """Returns the correct gettext translations. Copy from flask-babel with some modifications. """ ctx = _request_ctx_stack.top if ctx is None: return None # babel should be in extensions for get_locale if 'babel' not in ctx.app.extensions: return None translations = getattr(ctx, 'wtforms_translations', None) if translations is None: dirname = messages_path() translations = support.Translations.load(dirname, [get_locale()], domain='wtforms') ctx.wtforms_translations = translations return translations
def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = stack.top if ctx is None: return NullTranslations() locale = get_locale() cache = self.get_translations_cache(ctx) translations = cache.get(str(locale)) if translations is None: translations_dir = self.get_translations_path(ctx) translations = Translations.load(translations_dir, locale, domain=self.domain) # Load plugins translations if isinstance(translations, Translations): # Load core extensions translations from wtforms.i18n import messages_path wtforms_translations = Translations.load(messages_path(), locale, domain='wtforms') translations.merge(wtforms_translations) for plugin_name in current_app.config['PLUGINS']: module_name = 'udata.ext.{0}'.format(plugin_name) module = import_module(module_name) translations_dir = join(dirname(module.__file__), 'translations') if exists(translations_dir): domain = '-'.join((self.domain, plugin_name)) plugins_translations = Translations.load( translations_dir, locale, domain=domain) translations.merge(plugins_translations) cache[str(locale)] = translations return translations
def _get_translations(): """Returns the correct gettext translations. Copy from flask-babel with some modifications. """ if not request: return None # babel should be in extensions for get_locale if 'babel' not in current_app.extensions: return None translations = getattr(request, 'wtforms_translations', None) if translations is None: translations = support.Translations.load( messages_path(), [get_locale()], domain='wtforms' ) request.wtforms_translations = translations return translations
def _get_translations(): """Returns the correct gettext translations. Copy from flask-babel with some modifications. """ if not request: return None # babel should be in extensions for get_locale if "babel" not in current_app.extensions: return None translations = getattr(request, "wtforms_translations", None) if translations is None: translations = support.Translations.load(messages_path(), [get_locale()], domain="wtforms") request.wtforms_translations = translations return translations
def have_babel(): return False def is_lazy_string(obj): return False def make_lazy_string(__func, msg): return msg if _domain_cls: # Have either Flask-Babel or Flask-BabelEx from babel.support import LazyProxy wtforms_domain = _domain_cls(messages_path(), domain="wtforms") def get_i18n_domain(app): kwargs = { _dir_keyword: cv("I18N_DIRNAME", app=app), "domain": cv("I18N_DOMAIN", app=app), } return _domain_cls(**kwargs) def have_babel(): return True def is_lazy_string(obj): """Checks if the given object is a lazy string.""" return isinstance(obj, LazyProxy)
def _get_translations(self): from babel import support return support.Translations.load( messages_path(), self.get_locales(), domain='wtforms')
""" flask_security.babel ~~~~~~~~~~~~~~~~~~~~ I18N support for Flask-Security. """ from flask_babelex import Domain from wtforms.i18n import messages_path wtforms_domain = Domain(messages_path(), domain="wtforms") class Translations: """Fixes WTForms translation support and uses wtforms translations.""" def gettext(self, string): return wtforms_domain.gettext(string) def ngettext(self, singular, plural, n): return wtforms_domain.ngettext(singular, plural, n)
dirname = view.admin.translations_path if dirname is not None: return dirname return super(CustomDomain, self).get_translations_path(ctx) domain = CustomDomain() gettext = domain.gettext ngettext = domain.ngettext lazy_gettext = domain.lazy_gettext try: from wtforms.i18n import messages_path except ImportError: from wtforms.ext.i18n.utils import messages_path wtforms_domain = Domain(messages_path(), domain='wtforms') class Translations(object): ''' Fixes WTForms translation support and uses wtforms translations ''' def gettext(self, string): return wtforms_domain.gettext(string) def ngettext(self, singular, plural, n): return wtforms_domain.ngettext(singular, plural, n) # lazy imports from .helpers import get_current_view
pkg_resources.get_distribution('invenio_theme') from invenio_theme import InvenioTheme INVENIO_THEME_AVAILABLE = True except pkg_resources.DistributionNotFound: INVENIO_THEME_AVAILABLE = False # Create Flask application app = Flask(__name__) app.config.update( MAIL_SUPPRESS_SEND=True, SECRET_KEY='CHANGE_ME', ACCOUNTS_USE_CELERY=False, BABEL_DEFAULT_LOCALE='en', I18N_TRASNLATION_PATHS=[ messages_path(), ] ) FlaskCLI(app) Babel(app) InvenioMail(app) InvenioI18N(app) InvenioDB(app) if INVENIO_ASSETS_AVAILABLE: InvenioAssets(app) if INVENIO_THEME_AVAILABLE: InvenioTheme(app) InvenioAccounts(app) app.register_blueprint(blueprint) InvenioUserProfiles(app) InvenioAdmin(app, permission_factory=lambda x: x, view_class_factory=lambda x: x)
return dirname return super(CustomDomain, self).get_translations_path(ctx) domain = CustomDomain() gettext = domain.gettext ngettext = domain.ngettext lazy_gettext = domain.lazy_gettext try: from wtforms.i18n import messages_path except ImportError: from wtforms.ext.i18n.utils import messages_path wtforms_domain = Domain(messages_path(), domain='wtforms') class Translations(object): ''' Fixes WTForms translation support and uses wtforms translations ''' def gettext(self, string): t = wtforms_domain.get_translations() return t.ugettext(string) def ngettext(self, singular, plural, n): t = wtforms_domain.get_translations() return t.ungettext(singular, plural, n) # lazy imports from .helpers import get_current_view
except pkg_resources.DistributionNotFound: INVENIO_ASSETS_AVAILABLE = False try: pkg_resources.get_distribution('invenio_theme') from invenio_theme import InvenioTheme INVENIO_THEME_AVAILABLE = True except pkg_resources.DistributionNotFound: INVENIO_THEME_AVAILABLE = False # Create Flask application app = Flask(__name__) app.config.update( ACCOUNTS_USE_CELERY=False, BABEL_DEFAULT_LOCALE='da', I18N_TRASNLATION_PATHS=[messages_path()], MAIL_SUPPRESS_SEND=True, SECRET_KEY='CHANGE_ME', SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), SQLALCHEMY_TRACK_MODIFICATIONS=True, WTF_CSRF_ENABLED=False, ) Babel(app) InvenioMail(app) InvenioI18N(app) InvenioDB(app) if INVENIO_ASSETS_AVAILABLE: InvenioAssets(app) if INVENIO_THEME_AVAILABLE: InvenioTheme(app)
INVENIO_ASSETS_AVAILABLE = False try: pkg_resources.get_distribution('invenio_theme') from invenio_theme import InvenioTheme INVENIO_THEME_AVAILABLE = True except pkg_resources.DistributionNotFound: INVENIO_THEME_AVAILABLE = False # Create Flask application app = Flask(__name__) app.config.update( ACCOUNTS_USE_CELERY=False, BABEL_DEFAULT_LOCALE='da', I18N_TRASNLATION_PATHS=[messages_path()], MAIL_SUPPRESS_SEND=True, SECRET_KEY='CHANGE_ME', SQLALCHEMY_TRACK_MODIFICATIONS=True, WTF_CSRF_ENABLED=False, ) Babel(app) InvenioMail(app) InvenioI18N(app) InvenioDB(app) if INVENIO_ASSETS_AVAILABLE: InvenioAssets(app) if INVENIO_THEME_AVAILABLE: InvenioTheme(app) InvenioAccounts(app) app.register_blueprint(blueprint)