# -*- coding: utf-8 -*-
from django.utils import six
from django.utils.encoding import force_text
from django.utils.functional import lazy
from django.utils.translation import get_language as _get_language

from .. import settings


def get_cache_key(**kwargs):
    """
    Returns cache key.
    """
    keys = ('identifier', 'object_id', 'language', 'field_name')
    return 'linguist_%s_%s_%s_%s' % tuple(kwargs[attr] for attr in keys)


def get_language_name(code):
    languages = dict((lang_code, lang_name) for lang_code, lang_name in settings.SUPPORTED_LANGUAGES)
    return languages.get(code)


def get_language():
    """
    Returns an active language code that is guaranteed to be in
    settings.SUPPORTED_LANGUAGES.
    """
    lang = _get_language()
    langs = [l[0] for l in settings.SUPPORTED_LANGUAGES]
    if lang not in langs and '-' in lang:
        lang = lang.split('-')[0]
    if lang in langs:
        return lang
    return settings.DEFAULT_LANGUAGE


def build_localized_field_name(field_name, language):
    """
    Build localized field name from ``field_name`` and ``language``.
    """
    return '%s_%s' % (field_name, language.replace('-', '_'))


def _build_localized_verbose_name(verbose_name, language):
    """
    Build localized verbose name from ``verbose_name`` and ``language``.
    """
    return force_text('%s [%s]') % (force_text(verbose_name), language)

build_localized_verbose_name = lazy(_build_localized_verbose_name, six.text_type)

@@ -973,16 +973,135 @@
 GUAGE%0A%0A%0A
+def get_fallback_language():%0A    %22%22%22%0A    Returns the fallback language.%0A    %22%22%22%0A    return settings.DEFAULT_LANGUAGE%0A%0A%0A
 def buil
@@ -1135,32 +1135,37 @@
 d_name, language
+=None
 ):%0A    %22%22%22%0A    B
@@ -1224,32 +1224,32 @@
 language%60%60.%0A
-
     %22%22%22%0A
     return '
@@ -1228,32 +1228,91 @@
 uage%60%60.%0A    %22%22%22%0A
+    if language is None:%0A        language = get_language()%0A
     return '%25s_%25

Add get_fallback_language util.