Django

Code

Changeset 5340

Show
Ignore:
Timestamp:
05/25/07 04:16:34 (1 year ago)
Author:
mtredinnick
Message:

unicode: Filled in some missing pieces of documentation.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/unicode/docs/i18n.txt

    r5324 r5340  
    296296 
    297297.. _Django templates: ../templates_python/ 
     298 
     299Working with lazy translation objects 
     300===================================== 
     301 
     302Using ``ugettext_lazy()`` and ``ungettext_lazy()`` to mark strings in models 
     303and utility functions is a common operation. When you are working with these 
     304objects elsewhere in your code, you should ensure that you don't accidentally 
     305convert them to strings, because they should be converted as late as possible 
     306(so that the correct locale is in effect). This necessitates the use of a 
     307couple of helper functions. 
     308 
     309Joining strings: string_concat() 
     310-------------------------------- 
     311 
     312Standard Python string joins (``''.join([...])``) will not work on lists 
     313containing lazy translation objects. Instead, you can use 
     314``django.utils.translation.string_concat()``, which creates a lazy object that 
     315concatenates its contents *and* converts them to strings only when the result 
     316is included in a string. For example:: 
     317 
     318    from django.utils.translation import string_concat 
     319    ... 
     320    name = ugettext_lazy(u'John Lennon') 
     321    instrument = ugettext_lazy(u'guitar') 
     322    result = string_concat([name, ': ', instrument]) 
     323 
     324In this case, the lazy translations in ``result`` will only be converted to 
     325strings when ``result`` itself is used in a string (usually at template 
     326rendering time). 
     327 
     328The allow_lazy() decorator 
     329-------------------------- 
     330 
     331There are a lot of useful utility functions in Django (particularly in 
     332``django.utils``) that take a string as their first argument and do something 
     333to that string. These functions are used by template filters as well as 
     334directly in other code. 
     335 
     336If you write your own similar functions, you will rapidly come across the 
     337problem of what to do when the first argument is a lazy translation object. 
     338You don't want to convert it to a string immediately, because you may be using 
     339this function outside of a view (and hence the current thread's locale setting 
     340will not be correct). For cases like this, the 
     341``django.utils.functional.allow_lazy()`` decorator will be useful. It modifies 
     342the function so that *if* it is called with a lazy translation as the first 
     343argument, the function evaluation is delayed until it needs to be converted to 
     344a string. 
     345 
     346For example:: 
     347 
     348    from django.utils.functional import allow_lazy 
     349 
     350    def fancy_utility_function(s, ...): 
     351        # Do some conversion on string 's' 
     352        ... 
     353    fancy_utility_function = allow_lazy(fancy_utility_function, unicode) 
     354 
     355The ``allow_lazy()`` decorator takes, in addition to the function to decorate, 
     356a number of extra arguments specifying the type(s) that the original function 
     357can return. Usually, it will be enough to just include ``unicode`` here and 
     358ensure that your function returns Unicode strings. 
     359 
     360Using this decorator means you can write your function and assume that the 
     361input is a proper string, then add support for lazy translation objects at the 
     362end. 
    298363 
    299364How to create language files 
  • django/branches/unicode/docs/unicode.txt

    r5338 r5340  
    8888string in the current locale. 
    8989 
     90For more details about lazy translation objects, refer to the 
     91internationalization_ documentation. 
     92 
     93.. _internationalization: ../i18n/#lazy-translation 
     94 
    9095.. _utility functions: 
    9196 
     
    148153 
    149154    * The function ``django.utils.encoding.iri_to_uri()`` implements the 
    150       conversion from IRI to URI as required by `the specification`_. 
     155      conversion from IRI to URI as required by the specification (`RFC 
     156      3987`_). 
    151157 
    152158    * The functions ``django.utils.http.urlquote()`` and 
     
    188194result. 
    189195 
     196The ``iri_to_uri()`` function is also idempotent, which means the following is 
     197always true:: 
     198 
     199    iri_to_uri(iri_to_uri(some_string)) = iri_to_uri(some_string) 
     200 
     201So you can safely call it multiple times on the same IRI without risking 
     202double-quoting problems. 
     203 
    190204.. _URI: http://www.ietf.org/rfc/rfc2396.txt 
    191205.. _IRI: http://www.ietf.org/rfc/rfc3987.txt 
    192 .. _the specification: IRI_ 
     206.. _RFC 3987: IRI_ 
    193207 
    194208Models 
     
    247261non-ASCII characters would have been removed in quoting in the first line.) 
    248262 
    249 .. _RFC 3987: IRI_ 
    250263.. _above: uri_and_iri_ 
    251264 
     
    278291default setting is utf-8). 
    279292 
     293Template tags and filters 
     294------------------------- 
     295 
     296A couple of tips to remember when writing your own template tags and filters: 
     297 
     298    * Always return Unicode strings from a template tag's ``render()`` method 
     299      and from template filters. 
     300 
     301    * Use ``force_unicode()`` in preference to ``smart_unicode()`` in these 
     302      places. Tag rendering and filter calls occur as the template is being 
     303      rendered, so there is no advantage to postponing the conversion of lazy 
     304      transation objects into strings any longer. It is easier to work solely 
     305      with Unicode strings at this point. 
     306 
    280307E-mail 
    281308======