|
| 1 | +# Standardized codes |
| 2 | + |
| 3 | +Many common concepts, such as spoken languages, countries, currency, and so on, |
| 4 | +have common codes (usually formalized by the [International Organization for |
| 5 | +Standardization][iso]) that are used in data communication and processing. |
| 6 | +These codes address the issue that there are often different ways to express |
| 7 | +the same concept in written language (for example, "United States" and "USA", |
| 8 | +or "Español" and "Spanish"). |
| 9 | + |
| 10 | +## Guidance |
| 11 | + |
| 12 | +For concepts where a standardized code exists and is in common use, fields |
| 13 | +representing these concepts **should** use the standardized code for both input |
| 14 | +and output. |
| 15 | + |
| 16 | +```typescript |
| 17 | +// A message representing a book. |
| 18 | +interface Book { |
| 19 | + // Other fields... |
| 20 | + |
| 21 | + // The IETF BCP-47 language code representing the language in which |
| 22 | + // the book was originally written. |
| 23 | + // https://en.wikipedia.org/wiki/IETF_language_tag |
| 24 | + languageCode: string; |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +- Fields representing standardized concepts **must** use the appropriate data |
| 29 | + type for the standard code (usually `string`). |
| 30 | + - Fields representing standardized concepts **should not** use enums, even if |
| 31 | + they only allow a small subset of possible values. Using enums in this |
| 32 | + situation often leads to frustrating lookup tables when using multiple APIs |
| 33 | + together. |
| 34 | + - Fields representing standardized concepts **must** indicate which standard |
| 35 | + they follow, preferably with a link (either to the standard itself, the |
| 36 | + Wikipedia description, or something similar). |
| 37 | +- The field name **should** end in `_code` or `_type` unless the concept has an |
| 38 | + obviously clearer suffix. |
| 39 | +- When accepting values provided by users, validation **should** be |
| 40 | + case-insensitive unless this would introduce ambiguity (for example, accept |
| 41 | + both `en-gb` and `en-GB`). When providing values to users, APIs **should** |
| 42 | + use the canonical case (in the example above, `en-GB`). |
| 43 | + |
| 44 | +### Content types |
| 45 | + |
| 46 | +Fields representing a content or media type **must** use [IANA media types][]. |
| 47 | +For legacy reasons, the field **should** be called `mime_type`. |
| 48 | + |
| 49 | +### Countries and regions |
| 50 | + |
| 51 | +Fields representing individual countries or nations **must** use the [Unicode |
| 52 | +CLDR region codes][cldr] ([list][]), such as `US` or `CH`, and the field |
| 53 | +**must** be called `region_code`. |
| 54 | + |
| 55 | +**Important:** We use `region_code` and not `country_code` to include regions |
| 56 | +distinct from any country, and avoid political disputes over whether or not |
| 57 | +some regions are countries. |
| 58 | + |
| 59 | +### Currency |
| 60 | + |
| 61 | +Fields representing currency **must** use [ISO-4217 currency codes][iso-4217], |
| 62 | +such as `USD` or `CHF`, and the field **must** be called `currency_code`. |
| 63 | + |
| 64 | +**Note:** For representing an amount of money in a particular currency, rather |
| 65 | +than the currency code itself, use [`google.protobuf.Money`][money]. |
| 66 | + |
| 67 | +### Language |
| 68 | + |
| 69 | +Fields representing spoken languages **must** use [IETF BCP-47 language |
| 70 | +codes][bcp-47] ([list][]), such as `en-US` or `de-CH`, and the field **must** |
| 71 | +be called `language_code`. |
| 72 | + |
| 73 | +### Time zones |
| 74 | + |
| 75 | +Fields representing a time zone **should** use the [IANA TZ][] codes, and the |
| 76 | +field **must** be called `time_zone`. |
| 77 | + |
| 78 | +Fields also **may** represent a UTC offset rather than a time zone (note that |
| 79 | +these are subtly different). In this case, the field **must** use the [ISO-8601 |
| 80 | +format][] to represent this, and the field **must** be named `utc_offset`. |
| 81 | + |
| 82 | +## Changelog |
| 83 | + |
| 84 | +- **2020-05-12**: Replaced `country_code` guidance with `region_code`, |
| 85 | + correcting an original error. |
| 86 | + |
| 87 | +<!-- prettier-ignore-start --> |
| 88 | +[bcp-47]: https://en.wikipedia.org/wiki/IETF_language_tag |
| 89 | +[cldr]: http://cldr.unicode.org/ |
| 90 | +[iana media types]: https://www.iana.org/assignments/media-types/media-types.xhtml |
| 91 | +[iana tz]: http://www.iana.org/time-zones |
| 92 | +[iso]: https://www.iso.org/ |
| 93 | +[iso-4217]: https://en.wikipedia.org/wiki/ISO_4217 |
| 94 | +[iso-8601 format]: https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC |
| 95 | +[list]: https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry |
| 96 | +[money]: https://github.com/googleapis/api-common-protos/blob/master/google/type/money.proto |
| 97 | +<!-- prettier-ignore-end --> |
0 commit comments