def lang_data(LANGUAGE_CODE, **kwargs): # These are needed for i18n from django.http import HttpRequest from django.views.i18n import javascript_catalog LANGUAGE_BIDI = LANGUAGE_CODE.split('-')[0] in \ settings.LANGUAGES_BIDI request = HttpRequest() request.GET['language'] = LANGUAGE_CODE # Add some JavaScript data content = 'var LANGUAGE_CODE = "%s";\n' % LANGUAGE_CODE content += 'var LANGUAGE_BIDI = ' + \ (LANGUAGE_BIDI and 'true' or 'false') + ';\n' content += javascript_catalog(request, packages=settings.INSTALLED_APPS).content # The hgettext() function just calls gettext() internally, but # it won't get indexed by makemessages. content += '\nwindow.hgettext = function(text) { return gettext(text); };\n' # Add a similar hngettext() function content += 'window.hngettext = function(singular, plural, count) { return ngettext(singular, plural, count); };\n' return content
def cached_javascript_catalog(request): packages = ['wirecloud.commons', 'wirecloud.catalogue', 'wirecloud.platform'] for plugin in get_plugins(): packages.append(plugin.__module__) return javascript_catalog(request, "djangojs", packages)
def i18n_javascript(self, request): if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, domain='django', packages=['django_mptt_admin'])
def update_jsi18n_file(code="en"): """ For efficieny's sake, we want to cache Django's js18n file. So, generate that file here, then save to disk--it won't change until the next language pack update! """ translation.activate(code) # we switch the language of the whole thread output_dir = os.path.join(settings.CONTENT_ROOT, 'locale', 'js', 'i18n') ensure_dir(output_dir) output_file = os.path.join(output_dir, "%s.js" % code) request = HttpRequest() request.path = output_file request.session = {settings.LANGUAGE_COOKIE_NAME: code} response = javascript_catalog(request, packages=('ka-lite.locale',), domain="djangojs") icu_js = "" for path in settings.LOCALE_PATHS: try: icu_js = open(os.path.join(path, code, "%s_icu.js" % code), "r").read() except IOError: logging.warn("No {code}_icu.js file found in locale_path {path}".format(code=code, path=path)) output_js = response.content + "\n" + icu_js logging.info("Writing i18nized js file to {0}".format(output_file)) with open(output_file, "w") as fp: fp.write(output_js) translation.deactivate()
def update_jsi18n_file(code="en"): """ For efficieny's sake, we want to cache Django's js18n file. So, generate that file here, then save to disk--it won't change until the next language pack update! """ translation.activate(code) # we switch the language of the whole thread output_dir = os.path.join(settings.CONTENT_ROOT, 'locale', 'js', 'i18n') ensure_dir(output_dir) output_file = os.path.join(output_dir, "%s.js" % code) request = HttpRequest() request.path = output_file request.session = {settings.LANGUAGE_COOKIE_NAME: code} response = javascript_catalog(request, packages=('ka-lite.locale', ), domain="djangojs") icu_js = "" for path in settings.LOCALE_PATHS: try: icu_js = open(os.path.join(path, code, "%s_icu.js" % code), "r").read() except IOError: logging.warn( "No {code}_icu.js file found in locale_path {path}".format( code=code, path=path)) output_js = response.content + "\n" + icu_js logging.info("Writing i18nized js file to {0}".format(output_file)) with open(output_file, "w") as fp: fp.write(output_js) translation.deactivate()
def i18n_javascript(self, request): if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, packages=['django_mptt_admin'])
def i18n_javascript(self, request): """Displays the i18n JavaScript that the Django admin requires. This takes into account the ``USE_I18N`` setting. If it's set to False, the generated JavaScript will be leaner and faster. """ return javascript_catalog(request, packages='pages')
def jsi18n(request): """ Returns the localisation code and strings for javascript """ js_info_web = { 'packages': ('sendinel') } return javascript_catalog(request, packages = js_info_web)
def javascript_catalog(request): return i18n.javascript_catalog( request, 'djangojs', { 'packages': settings.MISAGO_JS_CATALOG_PACKAGES, }, )
def i18n_javascript(request): """ Displays the i18n JavaScript that the Django admin requires. """ if settings.USE_I18N: # pragma: no cover from django.views.i18n import javascript_catalog else: # pragma: no cover from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, packages=['djangobmf'])
def handle(self, *args, **options): fake_request = HttpRequest() for lang in settings.AMO_LANGUAGES: filename = os.path.join(settings.STATICFILES_DIRS[0], 'js', 'i18n', '%s.js' % lang) with translation.override(lang): response = javascript_catalog(fake_request) with open(filename, 'w') as f: f.write(response.content)
def cached_javascript_catalog(request): packages = [ 'wirecloud.commons', 'wirecloud.catalogue', 'wirecloud.platform' ] for plugin in get_plugins(): packages.append(plugin.__module__) return javascript_catalog(request, "djangojs", packages)
def cached_javascript_catalog(request, domain="djangojs", packages=None): """View for cached JavaScript i18n. See https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#note-on-performance. In contrast to the Django docs, however, we set a max-age header so that the browser does not try to re-fetch the JavaScript translation catalog for one hour. This way, one can reduce the latency drastically with slow server configurations. The downside, namely that new translations are delivered after at most one hour, is absolutely bearable. """ return javascript_catalog(request, domain, packages)
def i18n_javascript(self, request): """ Override the original js catalog function to include additional js translation messages based on a yawd-admin setting. """ if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, packages=['django.conf', 'django.contrib.admin'] + getattr(settings, 'ADMIN_JS_CATALOG', []))
def jsCatalog(request, lang, domain='djangojs', packages=None): ''' Returns the js translation catalog :param request: :param lang: :param domain: :param packages: ''' if lang != '': request.GET = {'language': lang} # Fake args for catalog :-) return javascript_catalog(request, domain, packages)
def i18n_javascript(self, request): """ Библиотека переводов текста """ if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, domain='django', packages=['django_mptt_admin', 'edw'])
def i18n_javascript(self, request): """ Displays the i18n JavaScript that the Django admin requires. This takes into account the USE_I18N setting. If it's set to False, the generated JavaScript will be leaner and faster. """ if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, packages=['django.conf', 'xadmin'])
def i18n_javascript(request): # taken from django.contrib.admin.sites.AdminSite.i18n_javascript if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog response = javascript_catalog(request, packages=["django.conf", "django.contrib.admin"]) response["Content-Type"] = "application/javascript" ##expires = datetime.datetime.now() + datetime.timedelta(minutes=5) ts = time.time() + 60 * 60 * 24 # 24 hours response["Expires"] = http_date(ts) return response
def i18n_javascript(self, request): """ Displays the i18n JavaScript that the Django admin requires. This takes into account the USE_I18N setting. If it's set to False, the generated JavaScript will be leaner and faster. """ if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, packages=["django.conf", "django.contrib.admin"])
def javascript_catalog_cached(request): global JS_CATALOG_CACHE lang = request.session['default_language'] if lang in JS_CATALOG_CACHE: logging.debug('Using js translation catalog cache for %s' % lang) src = JS_CATALOG_CACHE[lang] return HttpResponse(src, 'text/javascript') else: logging.debug('Generating js translation catalog for %s' % lang) resp = javascript_catalog(request, 'djangojs', settings.INSTALLED_APPS) src = resp.content JS_CATALOG_CACHE[lang] = src return resp
def i18n_javascript(self, request): """ Override the original js catalog function to include additional js translation messages based on a phantom setting. """ if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog( request, packages=['django.conf', 'django.contrib.admin', 'phantom'] + getattr(settings, 'ADMIN_JS_CATALOG', []))
def get(self, request, *args, **kwargs): context = self.get_context() """ Displays the i18n JavaScript that the Django admin requires. This takes into account the USE_I18N setting. If it's set to False, the generated JavaScript will be leaner and faster. """ if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog return javascript_catalog(request, packages=['django.conf', 'xadmin'])
def cached_javascript_catalog(request, domain='djangojs', packages=None): """A cached version of javascript_catalog.""" global locale_serials package_str = '_'.join(packages) try: serial = locale_serials[package_str] except KeyError: serial = generate_locale_serial(packages) locale_serials[package_str] = serial return cache_memoize( 'jsi18n-%s-%s-%s-%d' % (domain, package_str, get_language(), serial), lambda: javascript_catalog(request, domain, packages), large_data=True, compress_large_data=True)
def _generate(self, language): language_bidi = language.split('-')[0] in settings.LANGUAGES_BIDI request = HttpRequest() request.GET['language'] = language # Add some JavaScript data content = 'var LANGUAGE_CODE = "%s";\n' % language content += 'var LANGUAGE_BIDI = ' + \ (language_bidi and 'true' or 'false') + ';\n' content += javascript_catalog(request, packages=settings.INSTALLED_APPS).content # The hgettext() function just calls gettext() internally, but # it won't get indexed by makemessages. content += '\nwindow.hgettext = function(text) { return gettext(text); };\n' # Add a similar hngettext() function content += 'window.hngettext = function(singular, plural, count) { return ngettext(singular, plural, count); };\n' return content
def update_jsi18n_file(code="en"): """ For efficieny's sake, we want to cache Django's js18n file. So, generate that file here, then save to disk--it won't change until the next language pack update! """ output_dir = os.path.join(settings.STATIC_ROOT, "js", "i18n") ensure_dir(output_dir) output_file = os.path.join(output_dir, "%s.js" % code) request = HttpRequest() request.path = output_file request.session = {'django_language': code} response = javascript_catalog(request, packages=('ka-lite.locale', )) with open(output_file, "w") as fp: fp.write(response.content)
def update_jsi18n_file(code="en"): """ For efficieny's sake, we want to cache Django's js18n file. So, generate that file here, then save to disk--it won't change until the next language pack update! """ output_dir = os.path.join(settings.STATIC_ROOT, "js", "i18n") ensure_dir(output_dir) output_file = os.path.join(output_dir, "%s.js" % code) request = HttpRequest() request.path = output_file request.session = {"django_language": code} response = javascript_catalog(request, packages=("ka-lite.locale",)) with open(output_file, "w") as fp: fp.write(response.content)
def get_output(self, variation): if "language" in variation: # This file uses i18n, so generate a separate file per language. # The language data is always added before all other files. if not os.path.exists(i18n_dir): os.makedirs(i18n_dir) for LANGUAGE_CODE in LANGUAGES: LANGUAGE_BIDI = LANGUAGE_CODE.split("-")[0] in settings.LANGUAGES_BIDI filename = combined % {"LANGUAGE_CODE": LANGUAGE_CODE} filepath = os.path.join(i18n_dir, filename) tocombine.append(filepath) language_group = [filepath] + abspathed_group COMBINE_MEDIA[filename] = language_group request = HttpRequest() request.GET["language"] = LANGUAGE_CODE # Add some JavaScript data content = 'var LANGUAGE_CODE = "%s";\n' % LANGUAGE_CODE content += "var LANGUAGE_BIDI = " + (LANGUAGE_BIDI and "true" or "false") + ";\n" content += javascript_catalog(request, packages=settings.INSTALLED_APPS).content # The hgettext() function just calls gettext() internally, but # it won't get indexed by makemessages. content += "\nwindow.hgettext = function(text) { return gettext(text); };\n" # Add a similar hngettext() function content += "window.hngettext = function(singular, plural, count) { return ngettext(singular, plural, count); };\n" # Check if content changed, so we don't regenerate the i18n # file unnecessarily. if os.path.exists(filepath): fp = codecs.open(filepath, "r", "utf-8") old_content = fp.read() fp.close() if old_content == content: continue if not silent: print "Updating i18n file %s..." % filename else: if not silent: print "Generating i18n file %s..." % filename fp = codecs.open(filepath, "w", "utf-8") fp.write(content) fp.close() elif "language_direction" in variation: # Generate CSS files for both text directions for LANGUAGE_DIR in ("ltr", "rtl"): value = {"LANGUAGE_DIR": LANGUAGE_DIR} COMBINE_MEDIA[combined % value] = [item % value for item in abspathed_group]
def update_jsi18n_file(code="en"): """ For efficieny's sake, we want to cache Django's js18n file. So, generate that file here, then save to disk--it won't change until the next language pack update! """ translation.activate(code) # we switch the language of the whole thread output_dir = os.path.join(os.path.dirname(__file__), 'static', 'js', 'i18n') ensure_dir(output_dir) output_file = os.path.join(output_dir, "%s.js" % code) request = HttpRequest() request.path = output_file request.session = {settings.LANGUAGE_COOKIE_NAME: code} response = javascript_catalog(request, packages=('ka-lite.locale',)) with open(output_file, "w") as fp: fp.write(response.content)
def i18n_javascript(self, request): """ Displays the i18n JavaScript that the Django admin requires. This takes into account the USE_I18N setting. If it's set to False, the generated JavaScript will be leaner and faster. """ if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog # Gives plugins the ability to add your translation scripts. packages = getattr(settings, 'XADMIN_I18N_JAVASCRIPT_PACKAGES', []) try: packages.extend(['django.conf', 'xadmin']) except AttributeError: raise ImproperlyConfigured('Expected list type as attribute ' 'in "XADMIN_I18N_JAVASCRIPT_PACKAGES"') return javascript_catalog(request, packages=packages)
def i18n_javascript(self, request): """ Override the original js catalog function to include additional js translation messages based on a yawd-admin setting and cache the results per language. """ cache_key = 'ya-jsi18n-%s' % get_language() cache_timeout = getattr(settings, 'ADMIN_JS_CATALOG_CACHE_TIMEOUT', 60 * 60) cached = cache.get(cache_key) if not cached or not cache_timeout: if settings.USE_I18N: from django.views.i18n import javascript_catalog else: from django.views.i18n import null_javascript_catalog as javascript_catalog cached = javascript_catalog(request, packages=['django.conf', 'django.contrib.admin', 'yawdadmin'] + getattr(settings, 'ADMIN_JS_CATALOG', [])) cache.set(cache_key, cached, cache_timeout) return cached
def jsi18n(request, domain='djangojs', packages=None): return javascript_catalog(request, domain, packages)
def jsi18n(request, packages=None, domain=None): if not domain: domain = 'djangojs' from django.views.i18n import javascript_catalog return javascript_catalog(request, domain, packages)
def jsi18n(request): return javascript_catalog(request, 'djangojs', ['userlog'])
def javascript_catalog_all(request, domain='djangojs'): """ Get JavaScript message catalog for all apps in INSTALLED_APPS. """ all_apps = [x.name for x in apps.get_app_configs()] return javascript_catalog(request, domain, all_apps)
def jsi18n(request, packages=None, domain=None): if not domain: domain = 'djangojs' return javascript_catalog(request, domain, packages)
from functools import update_wrapper
def cached_js_catalog(request, domain='djangojs', packages=None): return javascript_catalog(request, domain, packages)
def read(self, request): return javascript_catalog(request, **js_info_dict)
def javascript_catalog(request): return i18n.javascript_catalog(request, 'djangojs', None)
def i18n_javascript(self, request): return javascript_catalog(request, packages='lemon.filebrowser')
def cached_javascript_catalog(request, domain="djangojs", packages=None): return javascript_catalog(request, domain, packages)
def cached_javascript_catalog(request, domain='djangojs', packages=None): """ https://docs.djangoproject.com/en/1.5/topics/i18n/translation/#note-on-performance """ return javascript_catalog(request, domain, packages)
def cached_javascript_catalog(request, domain='djangojs', packages=None): return i18n.javascript_catalog(request, domain, packages)
def cached_javascript_catalog(request, domain='djangojs', packages=None): if packages is None: packages = tuple(settings.INSTALLED_APPS) + ('django.conf',) return javascript_catalog(request, domain, packages)