def langfiles_for_path(path): """ Find and return any extra lang files specified in templates or python source files, or the first entry in the DOTLANG_FILES setting if none. :param path: path to a file containing strings to translate :return: list of langfile names. """ lang_files = None if is_template(path): # If the template explicitly specifies lang files, use those lang_files = parse_template(join(settings.ROOT, path)) # Otherwise, normalize the path name to a lang file if not lang_files: lang_files = [get_lang_path(path)] elif is_python(path): # If the python file explicitly specifies lang files, use those lang_files = parse_python(join(settings.ROOT, path)) if not lang_files: # All other sources use the first main file lang_files = settings.DOTLANG_FILES[:1] return lang_files
def template_is_active(path, lang): """Given a template path, determine if it should be active for a locale. It is active if either the template's lang file, or the lang file specified in the "set_lang_files" template tag has the active tag. :param path: relative path to the template. :param lang: language code :return: boolean """ if settings.DEV: return True cache_key = 'template_active:{lang}:{path}'.format(lang=lang, path=path) is_active = cache.get(cache_key) if is_active is None: # try the quicker and more efficient check first is_active = lang_file_is_active(get_lang_path(path), lang) if not is_active: template = get_template(path) lang_files = parse_template(template.filename) is_active = lang_files and lang_file_is_active(lang_files[0], lang) cache.set(cache_key, is_active, settings.DOTLANG_CACHE) return is_active
def render(request, template, context={}, **kwargs): """ Same as jingo's render() shortcut, but with l10n template support. If used like this:: return l10n_utils.render(request, 'myapp/mytemplate.html') ... this helper will render the following template:: l10n/LANG/myapp/mytemplate.html if present, otherwise, it'll render the specified (en-US) template. """ # Look for localized template if not default lang. if request.locale != settings.LANGUAGE_CODE: localized_tmpl = '%s/templates/%s' % (request.locale, template) try: return jingo.render(request, localized_tmpl, context, **kwargs) except TemplateNotFound: # If not found, just go on and try rendering the parent template. pass # Every template gets its own .lang file, so figure out what it is # and pass it in the context path = get_lang_path(template) (base, ext) = os.path.splitext(path) context['langfile'] = base return jingo.render(request, template, context, **kwargs)
def _get_template_tag_set(lang, path): lang_files = [get_lang_path(path)] template = get_template(path) lang_files.extend(parse_template(template.template.filename)) tag_set = set() for lf in lang_files: tag_set |= lang_file_tag_set(lf, lang) return tag_set
def pot_to_langfiles(): """Update the lang files in /locale/templates with extracted strings.""" all_msgs = po_msgs() root = 'templates' # Start off with some global lang files so that strings don't # get duplicated everywhere main_msgs = parse_lang(lang_file('main.lang', root)) main_msgs.update(parse_lang(lang_file('base.lang', root))) main_msgs.update(parse_lang(lang_file('newsletter.lang', root))) # Walk through the msgs and put them in the appropriate place. The # complex part about this is that templates can specify a list of # lang files to pull from, so we need to check all of them for the # strings and add it to the first lang file specified if not # found. for path, msgs in all_msgs.items(): target = None lang_files = None if is_template(path): # If the template explicitly specifies lang files, use those lang_files = [ lang_file('%s.lang' % f, root) for f in parse_template(join(settings.ROOT, path)) ] # Otherwise, normalize the path name to a lang file if not lang_files: lang_files = [lang_file(get_lang_path(path), root)] else: # All other sources use the first main file lang_files = [ lang_file('%s.lang' % settings.DOTLANG_FILES[0], root) ] # Get the current translations curr = {} for f in lang_files: if os.path.exists(f): curr.update(parse_lang(f)) # Add translations to the first lang file target = lang_files[0] if not os.path.exists(target): d = os.path.dirname(target) if not os.path.exists(d): os.makedirs(d) with codecs.open(target, 'a', 'utf-8') as out: for msg in msgs: if msg not in curr and msg not in main_msgs: out.write(';%s\n%s\n\n\n' % (msg, msg))
def pot_to_langfiles(): """Update the lang files in /locale/templates with extracted strings.""" all_msgs = po_msgs() root = 'templates' # Start off with some global lang files so that strings don't # get duplicated everywhere main_msgs = parse_lang(lang_file('main.lang', root)) main_msgs.update(parse_lang(lang_file('base.lang', root))) main_msgs.update(parse_lang(lang_file('newsletter.lang', root))) # Walk through the msgs and put them in the appropriate place. The # complex part about this is that templates can specify a list of # lang files to pull from, so we need to check all of them for the # strings and add it to the first lang file specified if not # found. for path, msgs in all_msgs.items(): target = None lang_files = None if is_template(path): # If the template explicitly specifies lang files, use those lang_files = [lang_file('%s.lang' % f, root) for f in parse_template(join(settings.ROOT, path))] # Otherwise, normalize the path name to a lang file if not lang_files: lang_files = [lang_file(get_lang_path(path), root)] else: # All other sources use the first main file lang_files = [lang_file('%s.lang' % settings.DOTLANG_FILES[0], root)] # Get the current translations curr = {} for f in lang_files: if os.path.exists(f): curr.update(parse_lang(f)) # Add translations to the first lang file target = lang_files[0] if not os.path.exists(target): d = os.path.dirname(target) if not os.path.exists(d): os.makedirs(d) with codecs.open(target, 'a', 'utf-8') as out: for msg in msgs: if msg not in curr and msg not in main_msgs: out.write(';%s\n%s\n\n\n' % (msg, msg))
def translations_for_template(template_name): """ Return the list of available translations for the template. :param template_name: name of the template passed to render. :return: dict, like {'en-US': 'English (US)', 'fr': 'Français'} """ lang_files = [get_lang_path(template_name)] template = get_template(template_name) lang_files.extend(parse_template(template.template.filename)) active_translations = {} for lf in lang_files: active_translations.update(get_translations_for_langfile(lf)) return active_translations
def translations_for_template(template_name): """ Return the list of available translations for the template. :param template_name: name of the template passed to render. :return: list, like ['en-US', 'fr'] """ lang_files = [get_lang_path(template_name)] template = get_template(template_name) lang_files.extend(parse_template(template.template.filename)) active_translations = [] for lf in lang_files: active_translations.extend(get_translations_for_langfile(lf)) return active_translations
def render(request, template, context=None, **kwargs): """ Same as django's render() shortcut, but with l10n template support. If used like this:: return l10n_utils.render(request, 'myapp/mytemplate.html') ... this helper will render the following template:: l10n/LANG/myapp/mytemplate.html if present, otherwise, it'll render the specified (en-US) template. """ context = {} if context is None else context # Make sure we have a single template if isinstance(template, list): template = template[0] # Every template gets its own .lang file, so figure out what it is # and pass it in the context context['langfile'] = get_lang_path(template) # Get the available translation list of the current page context['translations'] = get_translations(context['langfile']) # Look for localized template if not default lang. if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE: # redirect to default lang if locale not active if not (settings.DEV or lang_file_is_active(context['langfile'], request.locale)): return HttpResponseRedirect('/' + '/'.join([ settings.LANGUAGE_CODE, split_path(request.get_full_path())[1] ])) localized_tmpl = '%s/templates/%s' % (request.locale, template) try: return django_render(request, localized_tmpl, context, **kwargs) except TemplateDoesNotExist: # If not found, just go on and try rendering the parent template. pass return django_render(request, template, context, **kwargs)
def merge_lang_files(langs): all_msgs = po_msgs() for lang in langs: print "Merging into %s..." % lang # Start off with some global lang files so that strings don't # get duplicated everywhere main_msgs = parse_lang(lang_file("main.lang", lang)) main_msgs.update(parse_lang(lang_file("base.lang", lang))) main_msgs.update(parse_lang(lang_file("newsletter.lang", lang))) for path, msgs in all_msgs.items(): target = None lang_files = None if is_template(path): # If the template explicitly specifies lang files, use those lang_files = [lang_file("%s.lang" % f, lang) for f in parse_template(join(settings.ROOT, path))] # Otherwise, normalize the path name to a lang file if not lang_files: lang_files = [lang_file(get_lang_path(path), lang)] else: # All other sources use the first main file lang_files = [lang_file("%s.lang" % settings.DOTLANG_FILES[0], lang)] # Get the current translations curr = {} for f in lang_files: if os.path.exists(f): curr.update(parse_lang(f)) # Add translations to the first lang file target = lang_files[0] if not os.path.exists(target): d = os.path.dirname(target) if not os.path.exists(d): os.makedirs(d) with codecs.open(target, "a", "utf-8") as out: for msg in msgs: if msg not in curr and msg not in main_msgs: out.write(";%s\n%s\n\n\n" % (msg, msg))
def render(request, template, context=None, **kwargs): """ Same as django's render() shortcut, but with l10n template support. If used like this:: return l10n_utils.render(request, 'myapp/mytemplate.html') ... this helper will render the following template:: l10n/LANG/myapp/mytemplate.html if present, otherwise, it'll render the specified (en-US) template. """ context = {} if context is None else context # Every template gets its own .lang file, so figure out what it is # and pass it in the context context['langfile'] = get_lang_path(template) # Get the available translation list of the current page context['translations'] = get_translations(context['langfile']) # Look for localized template if not default lang. if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE: # redirect to default lang if locale not active if not (settings.DEV or lang_file_is_active(context['langfile'], request.locale)): return HttpResponseRedirect('/' + '/'.join([ settings.LANGUAGE_CODE, split_path(request.get_full_path())[1] ])) localized_tmpl = '%s/templates/%s' % (request.locale, template) try: return django_render(request, localized_tmpl, context, **kwargs) except TemplateDoesNotExist: # If not found, just go on and try rendering the parent template. pass return django_render(request, template, context, **kwargs)
def render(request, template, context={}, **kwargs): """ Same as django's render() shortcut, but with l10n template support. If used like this:: return l10n_utils.render(request, 'myapp/mytemplate.html') ... this helper will render the following template:: l10n/LANG/myapp/mytemplate.html if present, otherwise, it'll render the specified (en-US) template. """ # Every template gets its own .lang file, so figure out what it is # and pass it in the context context['langfile'] = get_lang_path(template) # Look for localized template if not default lang. if request.locale != settings.LANGUAGE_CODE: # redirect to default lang if locale not active if not (settings.DEV or lang_file_is_active(context['langfile'], request.locale)): return HttpResponseRedirect('/' + '/'.join([ settings.LANGUAGE_CODE, split_path(request.get_full_path())[1] ])) localized_tmpl = '%s/templates/%s' % (request.locale, template) try: return django_render(request, localized_tmpl, context, **kwargs) except TemplateNotFound: # If not found, just go on and try rendering the parent template. pass return django_render(request, template, context, **kwargs)