When to use the locale name and when the language code in Django?
Asked Answered
C

1

9

According to Django Documentation:

locale name

A locale name, either a language specification of the form ll or a combined language and country specification of the form ll_CC. Examples: it, de_AT, es, pt_BR. The language part is always in lower case and the country part in upper case. The separator is an underscore.

language code

Represents the name of a language. Browsers send the names of the languages they accept in the Accept-Language HTTP header using this format. Examples: it, de-at, es, pt-br. Language codes are generally represented in lower-case, but the HTTP Accept-Language header is case-insensitive. The separator is a dash.

Questions:

  1. When I see it or es in someone's code, how can I tell whether it's a locale name or a language code?
  2. When should we use locale code, and when to use language code?
Chaetopod answered 12/5, 2017 at 10:4 Comment(0)
I
1

Locale codes are understood by the setlocale(3) call and configure localization of several well-known formats, such as dates, times and currency as well as the language for error messages. The available locales are platform and system dependent.

A language code is built upon the locale, but used in network communication. In practice, you should be dealing with language codes at the request and response layer and locale code within the application, but the distinction is not strict as they solve the same problem: localization and internationalization.

Case in point: Django sets the language based on the Accept-Language header, which use the language code format and then sets the locale accordingly for the application, thus selecting the locale code corresponding to the language code.

Therefore it's safe to say that "language codes are a serialization format of locale codes":

nl_NL.ISO-8859-15 is serialized to Accept-language: nl-NL + Accept-Charset: iso-8859-15. The important part is to use the correct form under the right circumstances, but the meaning of es is always Spanish, no matter the origin.

Impure answered 1/12, 2020 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.