Skip to content

Latest commit

 

History

History
135 lines (99 loc) · 5.99 KB

good_practices.rst

File metadata and controls

135 lines (99 loc) · 5.99 KB

Good practices

Rules

.. TODO:: NELLE : I'd probably replace rules per tips

To limit or avoid issues with Unicode, try to follow these rules:

  • :ref:`decode <decode>` all bytes data as early as possible: keyboard strokes, files, data received from the network, ...
  • :ref:`encode <encode>` back Unicode to bytes as late as possible: write text to a file, log a message, send data to the network, ...
  • always store and manipulate text as :ref:`character strings <str>`
  • if you have to encode text and you can choose the encoding: prefer the :ref:`UTF-8` encoding. It is able to encode all Unicode 6.0 characters (including :ref:`non-BMP characters <bmp>`), has no endian issue, is well supported by most programs, and its good compromise is size.
.. TODO:: problem grammatical dans la dernière phrase du dernier point

Unicode support levels

There are different levels of Unicode support:

These levels should help you to estimate the status of the Unicode support of your project. A basic support is enough if all of your users speak the same language or live in close countries. A basic Unicode support usually means an excellent support of Western Europe languages. Full Unicode support is required to support Asian languages.

By default, the :ref:`C <c>`, :ref:`C++ <cpp>` and :ref:`PHP5 <php>` languages have a basic Unicode support. For the C and C++ languages, you can have a basic of full Unicode support using a third-party library like :ref:`glib <glib>`, :ref:`Qt <qt>` or :ref:`ICU <icu>`. With PHP5, you can have a basic Unicode support using "mb_" functions.

By default, the :ref:`Python 2 <python2>` language doesn't support Unicode. You can have a basic Unicode support if you store text into the unicode type and take care of input and output encodings. For :ref:`Python 3 <python3>`, the situation is different: it has directly a basic Unicode support by using the wide character API on Windows and by taking care of input and output encodings for you (e.g. decode command line arguments and environment variables). The unicodedata module is a first step for a full Unicode support.

Most UNIX and Windows programs don't support Unicode. Firefox web browser and OpenOffice.org office suite have a full Unicode support. Slowly, more and more programs have a basic Unicode support.

Don't expect to have directly a full Unicode support: it requires a lot of work. Your project may be fully Unicode compliant for a specific task (e.g. :ref:`filenames <filename>`), but only have a basic Unicode support for the other parts of the project.

Test the Unicode support of a program

Tests to evaluate the Unicode support of a program:

  • Write non-ASCII characters (e.g. é, U+00E9) in all input fields: if the program fails with an error, it has no Unicode support.
  • Write characters not encodable to the :ref:`locale encoding <locale encoding>` (e.g. Ł, U+0141) in all input fields: if the program fails with an error, it has probably a basic Unicode support.
  • To test if a program is fully Unicode compliant, write text mixing different languages in different directions and characters with diacritics, especially in Persian characters. Try also :ref:`decomposed characters <normalization>`, for example: {e, U+0301} (decomposed form of é, U+00E9).
.. seealso::

   Wikipedia article to `test the Unicode support of your web browser
   <http://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Unicode/Test>`_. `UTF-8 encoded
   sample plain-text file <http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt>`_
   (Markus Kuhn, 2002).


Get the encoding of your inputs

Console:

File formats:

  • XML: the encoding can be specified in the <?xml ...?> header, use :ref:`UTF-8` if the encoding is not specified. For example, <?xml version="1.0" encoding="iso-8859-1"?>.
  • HTML: the encoding can be specified in a "Content type" HTTP header, e.g. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">. If it is not, you have to guess the encoding.

Filesystem (filenames):

.. seealso:: :ref:`guess`


Switch from byte strings to character strings

Use character strings, instead of byte strings, to avoid :ref:`mojibake issues <mojibake>`.

.. todo:: explain why byte strings are still used (backward compatibility)
.. todo:: explain how to switch from byte to unicode strings: Python 2=>3, Windows A=>W, PHP 5=>6