示例#1
0
文件: __init__.py 项目: IanLewis/kay
def load_translations(locale):
  """Load the translation for a locale.  If a locale does not exist
  the return value a fake translation object.
  """
  from werkzeug.utils import import_string

  from kay.i18n.translations import KayTranslations
  domain = "messages"
  ret = KayTranslations.load(utils.get_kay_locale_path(), locale, domain)
  def _merge(path):
    t = KayTranslations.load(path, locale, domain)
    if t is not None:
      if ret is None:
        return t
      elif isinstance(ret, KayTranslations):
        ret.merge(t)
    return ret
  try:
    installed_apps = local.app.app_settings.INSTALLED_APPS
  except AttributeError:
    installed_apps = settings.INSTALLED_APPS
  for appname in installed_apps:
    app = import_string(appname)
    apppath = os.path.join(os.path.dirname(app.__file__), 'i18n')

    if os.path.isdir(apppath):
      ret = _merge(apppath)
  # Add I18N_DIR
  try:
    target = os.path.join(kay.PROJECT_DIR, local.app.app_settings.I18N_DIR)
    if os.path.isdir(target):
      ret = _merge(target)
  except AttributeError:
    pass
  return ret
示例#2
0
文件: __init__.py 项目: IanLewis/kay
def get_language_from_request(request):
  global _accepted
  lang = request.cookies.get(settings.LANG_COOKIE_NAME)
  if lang:
    return lang
  for lang in request.accept_languages.itervalues():
    pos = lang.find('-')
    if pos >= 0:
      lang = lang[:pos].lower()+'_'+lang[pos+1:].upper()
    else:
      lang = lang.lower()
    if lang in _accepted:
      return _accepted[lang]
    for dirname in (lang, lang.split("_")[0]):
      if os.path.exists(os.path.join(utils.get_kay_locale_path(), dirname,
                                     "LC_MESSAGES", "messages.mo")):
        _accepted[lang] = dirname
        return dirname
  return None
示例#3
0
def get_language_from_request(request):
    global _accepted
    lang = request.cookies.get(settings.LANG_COOKIE_NAME)
    if lang:
        return lang
    for lang in request.accept_languages.itervalues():
        pos = lang.find('-')
        if pos >= 0:
            lang = lang[:pos].lower() + '_' + lang[pos + 1:].upper()
        else:
            lang = lang.lower()
        if lang in _accepted:
            return _accepted[lang]
        for dirname in (lang, lang.split("_")[0]):
            if os.path.exists(
                    os.path.join(utils.get_kay_locale_path(), dirname,
                                 "LC_MESSAGES", "messages.mo")):
                _accepted[lang] = dirname
                return dirname
    return None
示例#4
0
def load_translations(locale):
    """Load the translation for a locale.  If a locale does not exist
  the return value a fake translation object.
  """
    from werkzeug.utils import import_string

    from kay.i18n.translations import KayTranslations
    domain = "messages"
    ret = KayTranslations.load(utils.get_kay_locale_path(), locale, domain)

    def _merge(path):
        t = KayTranslations.load(path, locale, domain)
        if t is not None:
            if ret is None:
                return t
            elif isinstance(ret, KayTranslations):
                ret.merge(t)
        return ret

    try:
        installed_apps = local.app.app_settings.INSTALLED_APPS
    except AttributeError:
        installed_apps = settings.INSTALLED_APPS
    for appname in installed_apps:
        app = import_string(appname)
        apppath = os.path.join(os.path.dirname(app.__file__), 'i18n')

        if os.path.isdir(apppath):
            ret = _merge(apppath)
    # Add I18N_DIR
    try:
        target = os.path.join(kay.PROJECT_DIR, local.app.app_settings.I18N_DIR)
        if os.path.isdir(target):
            ret = _merge(target)
    except AttributeError:
        pass
    return ret