locale_gen.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 filetype=cpp.doxygen
  9. /*!
  10. \page locale_gen Locale Generation
  11. Each locale is defined by a specific locale identifier, which contains a mandatory part (Language) and several optional parts
  12. (Country, Variant, keywords and character encoding of \c std::string). Boost.Locale uses the POSIX naming convention for locales,
  13. i.e. a locale is defined as <tt>language[_COUNTRY][.encoding][\@variant]</tt>, where lang is ISO-639 language name like "en" or "ru",
  14. COUNTRY is the ISO-3166 country identifier like "US" or "DE", encoding is the eight-bit character encoding like \c UTF-8 or \c ISO-8859-1,
  15. and variant is additional options for specializing the locale, like \c euro or \c calendar=hebrew, see \ref locale_gen_variant.
  16. Note that each locale should include the encoding in order to handle \c char based strings correctly.
  17. \section locale_gen_basics Basics
  18. The class \ref boost::locale::generator "generator" provides tools to generate the locales we need. The simplest way to use
  19. \c generator is to create a locale and set it as the global one:
  20. \code
  21. #include <boost/locale.hpp>
  22. using namespace boost::locale;
  23. int main()
  24. {
  25. generator gen;
  26. // Create locale generator
  27. std::locale::global(gen(""));
  28. // "" - the system default locale, set
  29. // it globally
  30. }
  31. \endcode
  32. Of course we can also specify the locale manually
  33. \code
  34. std::locale loc = gen("en_US.UTF-8");
  35. // Use English, United States locale
  36. \endcode
  37. \note
  38. - Even if your application uses wide strings everywhere, you should specify the
  39. 8-bit encoding to use for 8-bit stream IO operations like \c cout or \c fstream.
  40. \n
  41. - The default locale is defined by the environment variables \c LC_CTYPE , \c LC_ALL , and \c LANG
  42. in that order (i.e. \c LC_CTYPE first and \c LANG last). On Windows, the library
  43. also queries the \c LOCALE_USER_DEFAULT option in the Win32 API when these variables
  44. are not set.
  45. \b Tip: Prefer using UTF-8 Unicode encoding over 8-bit encodings like the ISO-8859-X ones.
  46. By default the generated locales include all supported categories and character types. However, if your
  47. application uses only 8-bit encodings, only wide-character encodings, or only specific facets, you can
  48. limit the facet generation to specific categories and character types by calling the
  49. \ref boost::locale::generator::categories() "categories" and \ref boost::locale::generator::characters() "characters"
  50. member functions of the \ref boost::locale::generator "generator" class.
  51. For example:
  52. \code
  53. generator gen;
  54. gen.characters(wchar_t_facet);
  55. gen.categories(collation_facet | formatting_facet);
  56. std::locale::global(gen("de_DE.UTF-8"));
  57. \endcode
  58. \section locale_gen_variant Variant
  59. The variant part of the locale (the part that comes after \@ symbol) is localization \ref using_localization_backends "back-end" dependent.
  60. \subsection locale_gen_variant_non_icu Non ICU Backends
  61. \ref posix_backend "POSIX" and \ref std_backend "std" back-ends use their own OS specific naming conventions and
  62. depend on the current OS configuration. For example typical Linux distribution provides \c euro for currency selection,
  63. \c cyrillic and \c latin for specification of language script.
  64. \ref winapi_backend "winapi" back-end does not support any variants.
  65. \subsection locale_gen_variant_icu ICU Backend
  66. ICU provides wide range of locale variant options. For detailed instructions read <a href="http://userguide.icu-project.org/locale">this</a>
  67. ICU manual pages.
  68. However in general it is represented as set of key=value pairs separated with a semicolon ";" For example:
  69. "@collation=phonebook;calendar=islamic-civil".
  70. Currently ICU supports following keys:
  71. - \c calendar - the calendar used for the current locale. For example: \c gregorian, \c japanese,
  72. \c buddhist, \c islamic, \c hebrew, \c chinese, \c islamic-civil.
  73. - \c collation - the collation order used for this locales, for example \c phonebook, \c pinyin, \c traditional,
  74. \c stroke, \c direct, \c posix.
  75. - \c currency - the currency used in this locale, the standard 3 letter code like USD or JPY.
  76. - \c numbers - the numbering system used, for example: \c latn, \c arab, \c thai.
  77. Please refer to CLDR and ICU documentation for exact list of keys and values:
  78. - <a href="http://userguide.icu-project.org/locale#TOC-Keywords">ICU User Guide/Locale/Keywords</a>
  79. - <a href="http://www.unicode.org/reports/tr35/">Unicode Locale Data Markup Language</a>
  80. */