## 24.1. Locale Support [24.1.1. Overview](locale.html#id-1.6.11.3.4) [24.1.2. Behavior](locale.html#id-1.6.11.3.5) [24.1.3. Problems](locale.html#id-1.6.11.3.6) [](<>) *Locale*support refers to an application respecting cultural preferences regarding alphabets, sorting, number formatting, etc. PostgreSQL uses the standard ISO C and POSIX locale facilities provided by the server operating system. For additional information refer to the documentation of your system. ### 24.1.1. Overview Locale support is automatically initialized when a database cluster is created using`initdb`.`initdb`will initialize the database cluster with the locale setting of its execution environment by default, so if your system is already set to use the locale that you want in your database cluster then there is nothing else you need to do. If you want to use a different locale (or you are not sure which locale your system is set to), you can instruct`initdb`exactly which locale to use by specifying the`--locale`option. For example: ``` initdb --locale=sv_SE ``` This example for Unix systems sets the locale to Swedish (`sv`) as spoken in Sweden (`SE`). Other possibilities might include`en_US`(U.S. English) and`fr_CA`(French Canadian). If more than one character set can be used for a locale then the specifications can take the form*`language_territory.codeset`*. For example,`fr_BE.UTF-8`represents the French language (fr) as spoken in Belgium (BE), with a UTF-8 character set encoding. What locales are available on your system under what names depends on what was provided by the operating system vendor and what was installed. On most Unix systems, the command`locale -a`will provide a list of available locales. Windows uses more verbose locale names, such as`German_Germany`or`Swedish_Sweden.1252`, but the principles are the same. Occasionally it is useful to mix rules from several locales, e.g., use English collation rules but Spanish messages. To support that, a set of locale subcategories exist that control only certain aspects of the localization rules: | `LC_COLLATE` | String sort order | | ------------ | ----------------- | | `LC_CTYPE` | Character classification (What is a letter? Its upper-case equivalent?) | | `LC_MESSAGES` | Language of messages | | `LC_MONETARY` | Formatting of currency amounts | | `LC_NUMERIC` | Formatting of numbers | | `LC_TIME` | Formatting of dates and times | The category names translate into names of`initdb`options to override the locale choice for a specific category. For instance, to set the locale to French Canadian, but use U.S. rules for formatting currency, use`initdb --locale=fr_CA --lc-monetary=en_US`. 如果您希望系统表现得好像没有语言环境支持,请使用特殊的语言环境名称`C`, 或等效地`POSIX`. 创建数据库时,某些语言环境类别的值必须固定。您可以对不同的数据库使用不同的设置,但是一旦创建了数据库,就不能再为该数据库更改它们。`LC_COLLATE`和`LC_CTYPE`是这些类别。它们会影响索引的排序顺序,因此它们必须保持固定,否则文本列上的索引会损坏。(但您可以使用排序规则来缓解此限制,如[第 24.2 节](collation.html).) 这些类别的默认值是在何时确定的`初始化数据库`运行,并且在创建新数据库时使用这些值,除非在`创建数据库`命令。 通过设置与语言环境类别同名的服务器配置参数,可以随时更改其他语言环境类别(请参阅[第 20.11.2 节](runtime-config-client.html#RUNTIME-CONFIG-CLIENT-FORMAT)详情)。选择的值`初始化数据库`实际上只是写入配置文件`postgresql.conf`在服务器启动时作为默认值。如果您从`postgresql.conf`然后服务器将从其执行环境中继承设置。 请注意,服务器的语言环境行为是由服务器看到的环境变量决定的,而不是由任何客户端的环境决定的。因此,在启动服务器之前,请注意配置正确的区域设置。这样做的结果是,如果客户端和服务器设置在不同的语言环境中,则消息可能会以不同的语言显示,具体取决于它们的来源。 ### 笔记 When we speak of inheriting the locale from the execution environment, this means the following on most operating systems: For a given locale category, say the collation, the following environment variables are consulted in this order until one is found to be set:`LC_ALL`,`LC_COLLATE`(or the variable corresponding to the respective category),`LANG`. If none of these environment variables are set then the locale defaults to`C`. Some message localization libraries also look at the environment variable`LANGUAGE`which overrides all other locale settings for the purpose of setting the language of messages. If in doubt, please refer to the documentation of your operating system, in particular the documentation about gettext. To enable messages to be translated to the user's preferred language, NLS must have been selected at build time (`configure --enable-nls`). All other locale support is built in automatically. ### 24.1.2. Behavior The locale settings influence the following SQL features: - Sort order in queries using`ORDER BY`or the standard comparison operators on textual data[](<>) - The`upper`,`lower`, and`initcap`functions[](<>) [](<>) - 模式匹配运算符 (`喜欢`,`相似`, 和 POSIX 风格的正则表达式);语言环境影响不区分大小写的匹配和字符类正则表达式的字符分类[](<>) [](<>) - 这`to_char`函数族[](<>) - 使用索引的能力`喜欢`条款 使用其他语言环境的缺点`C`要么`POSIX`在 PostgreSQL 中是它的性能影响。它会减慢字符处理速度并防止普通索引被`喜欢`.因此,仅当您确实需要它们时才使用语言环境。 作为允许 PostgreSQL 使用索引的解决方法`喜欢`在非 C 语言环境下的子句中,存在几个自定义运算符类。这些允许创建执行严格的逐个字符比较的索引,而忽略区域设置比较规则。参考[第 11.10 节](indexes-opclass.html)了解更多信息。另一种方法是使用`C`整理,如中所述[第 24.2 节](collation.html). ### 24.1.3. Problems If locale support doesn't work according to the explanation above, check that the locale support in your operating system is correctly configured. To check what locales are installed on your system, you can use the command`locale -a`if your operating system provides it. Check that PostgreSQL is actually using the locale that you think it is. The`LC_COLLATE`and`LC_CTYPE`settings are determined when a database is created, and cannot be changed except by creating a new database. Other locale settings including`LC_MESSAGES`and`LC_MONETARY`are initially determined by the environment the server is started in, but can be changed on-the-fly. You can check the active locale settings using the`SHOW`command. The directory`src/test/locale`in the source distribution contains a test suite for PostgreSQL's locale support. Client applications that handle server-side errors by parsing the text of the error message will obviously have problems when the server's messages are in a different language. Authors of such applications are advised to make use of the error code scheme instead. Maintaining catalogs of message translations requires the on-going efforts of many volunteers that want to see PostgreSQL speak their preferred language well. If messages in your language are currently not available or not fully translated, your assistance would be appreciated. If you want to help, refer to[Chapter 55](nls.html)or write to the developers' mailing list.