def calc_locale_choices(): """Get a list of language (code, label) tuples for each supported locale This is the list of languages that we can translate our interface into (at least partially) """ localedir = os.path.join(PROJECT_ROOT, 'locale') codes = set() for name in os.listdir(localedir): if not os.path.isdir(os.path.join(localedir, name)): continue name = name.split('.', 1)[0] name = name.replace('_', '-').lower() codes.add(name) mapping = get_language_name_mapping('gettext') return [(lc, mapping[lc]) for lc in sorted(codes) if lc in mapping]
def get_language_choices(with_empty=False): """Return a list of language code choices labeled appropriately.""" cache_key = 'simple-langs-cache-%s' % get_language() languages = cache.get(cache_key) if not languages: languages = [] for code, name in get_language_name_mapping('unisubs').items(): languages.append((code, _(name))) languages.sort(key=lambda item: item[1]) cache.set(cache_key, languages, 60 * 60) if with_empty: languages = [('', '---------')] + languages return languages
from django.conf import settings from django.core.cache import cache from django.utils.http import cookie_date from django.utils.translation import (get_language, get_language_info, ugettext as _) from django.utils.translation.trans_real import parse_accept_lang_header import babel import pyuca from unilangs import get_language_name_mapping, LanguageCode collator = pyuca.Collator() # A set of all language codes we support. _supported_languages_map = get_language_name_mapping('unisubs') _all_languages_map = get_language_name_mapping('internal') SUPPORTED_LANGUAGE_CODES = set(_supported_languages_map.keys()) ALL_LANGUAGE_CODES = set(_all_languages_map.keys()) SUPPORTED_LANGUAGE_CHOICES = list( sorted(_supported_languages_map.items(), key=lambda c: c[1])) ALL_LANGUAGE_CHOICES = list( sorted(_all_languages_map.items(), key=lambda c: c[1])) # Top 24 popular languages, taken from: # https://en.wikipedia.org/wiki/Languages_used_on_the_Internet POPULAR_LANGUAGES = [ 'en', 'ru', 'de',
# -*- coding: utf-8 -*- import time from django.conf import settings from django.core.cache import cache from django.utils import simplejson as json from django.utils.http import cookie_date from django.utils.translation import (get_language, get_language_info, ugettext as _) from django.utils.translation.trans_real import parse_accept_lang_header from unilangs import get_language_name_mapping, LanguageCode # A set of all language codes we support. SUPPORTED_LANGUAGE_CODES = set(get_language_name_mapping('unisubs').keys()) def _only_supported_languages(language_codes): """Filter the given list of language codes to contain only codes we support.""" # TODO: Figure out the codec issue here. return [ code for code in language_codes if code in SUPPORTED_LANGUAGE_CODES ] def get_language_choices(with_empty=False): """Return a list of language code choices labeled appropriately.""" cache_key = 'simple-langs-cache-%s' % get_language() languages = cache.get(cache_key)
from django import template from django.conf import settings from django.utils.translation import (get_language_info, gettext_lazy as _) from unilangs import get_language_name_mapping import logging logger = logging.getLogger('utils.languagetags') LANGUAGE_NAMES = get_language_name_mapping('unisubs') register = template.Library() @register.filter() def to_localized_display(language_code): ''' Translates from a language code to the language name in the locale the user is viewing the site. For example: en -> Anglaise (if user is viewing with 'fr' en -> English It uses the django internal machinery to figure out what language the request cicle is in, currently set on the localurl middleware. IF anything is wrong, will log the missing error and will return a '?'. ''' try: return _( get_language_info(unicode(language_code))['name'].encode('utf-8')) except KeyError: logger.error('Uknown language code to be translated',