Ticket #11714: i18n-docs.diff

File i18n-docs.diff, 1.9 KB (added by zgoda, 6 years ago)

missing documentation on few functions from django.utils.translation

  • docs/topics/i18n.txt

    859859strings that are connected to your explicit project and not strings that are
    860860distributed independently.
     862Using translations outside views and templates
     865While Django provides rich set of i18n tools for use in views and templates,
     866it does not restrict the usage to Django-specific code. The Django translation
     867mechanisms can be used to translate arbitrary texts to any language that is
     868supported by Django (as long as appropriate translation catalog exists, of
     869course). You can load translation catalog, activate it and translate text to
     870language of your choice, but remember to switch back to original language, as
     871activating translation catalog is done in per-thread basis and such change
     872will affect code running in the same thread.
     874For example::
     876    from django.utils import translation
     877    def welcome_translated(language):
     878        cur_language = translation.get_language()
     879        try:
     880            translation.activate(language)
     881            text = translation.ugettext('welcome')
     882        finally:
     883            translation.activate(cur_language)
     884        return text
     886Calling this function with argument 'de' will give you ``"wilkommen"``,
     887regardless of :setting:`LANGUAGE_CODE` and language set by middleware.
     889Function of particular interest are ``django.utils.translation.get_language()``
     890which returns language used in current thread, and
     891``django.utils.translation.activate()`` which activates translation catalog for
     892current thread. Other useful function is
     893``django.utils.translation.check_for_language()`` checks if language is
     894supported by Django.
    862896The ``set_language`` redirect view
Back to Top