Skip to content

本地化

¥Localization

切换语言环境

¥Switching locales

你知道 Faker 支持许多不同的语言环境吗?当使用我们的默认实例 import { faker } from '@faker-js/faker' 时,你将获得英文数据。但是,我们还为 超过 70 个可用语言环境 提供了预构建的实例。

¥Did you know Faker supports many different locales?\ When using our default instance import { faker } from '@faker-js/faker' you get English data. However, we also provide pre-built instances for more than 70 available locales.

例如,你可以导入德语语言环境:

¥For example, you can import the German locale:

import { fakerDE as faker } from '@faker-js/faker'

注释

你还可以使用自定义语言环境/覆盖构建自己的 Faker 实例。

¥You can also build your own Faker instances, with custom locales/overwrites.

自定义语言环境和后备

¥Custom locales and fallbacks

如果我们内置的 faker 实例不能满足你的需求,你可以构建自己的:

¥If our built-in faker instances don't satisfy your needs, you can build your own:

ts
import type { LocaleDefinition } from '@faker-js/faker';
import { base, de, de_CH, en, Faker } from '@faker-js/faker';

const customLocale: LocaleDefinition = {
  title: 'My custom locale',
  internet: {
    domainSuffix: ['test'],
  },
};

export const customFaker = new Faker({
  locale: [customLocale, de_CH, de, en, base],
});

在此示例中,有 5 个语言环境。按顺序检查每个语言环境,并使用包含请求数据的第一个语言环境:

¥In this example there are 5 locales. Each of these is checked in order, and the first locale which contains the requested data will be used:

  • customLocale 是你的自定义语言环境定义,它将覆盖所有其他后备定义。

    ¥customLocale is your custom locale definition which will override all other fallback definitions.

  • de_CH 是特定的语言环境定义,它使用 CH(瑞士)数据覆盖一些德语定义。

    ¥de_CH is a specific locale definition that overrides some German definitions with CH (Switzerland) data.

  • de 是通用的 de(德语)语言环境定义。

    ¥de is a generic de (German) locale definition.

  • en 是一个通用的 en(英语)语言环境定义。这是我们最完整的语言环境,因此我们添加它来填补一些空白。根据你的需要,你可能希望或不希望将其作为后备。

    ¥en is a generic en (English) locale definition. This is our most complete locale, so we add it to fill some gaps. Depending on your needs, you might want or not want to have it as a fallback.

  • base 是基本语言环境定义,其中包含可用于每种语言的定义(例如表情符号)。

    ¥base is the base locale definition which contains definitions that can be used in every language (e.g. emojis).

可用语言环境

¥Available locales

语言环境名称Faker
af_ZA南非荷兰语fakerAF_ZA
ar阿拉伯语fakerAR
az阿塞拜疆语fakerAZ
base基础fakerBASE
bn_BD孟加拉语(孟加拉国)fakerBN_BD
cs_CZ捷克语(捷克)fakerCS_CZ
cy威尔士语fakerCY
da丹麦语fakerDA
de德语fakerDE
de_AT德语(奥地利)fakerDE_AT
de_CH德语(瑞士)fakerDE_CH
dv马尔代夫语fakerDV
el希腊语fakerEL
en英语fakerEN
en_AU英语(澳大利亚)fakerEN_AU
en_AU_ocker英语(澳大利亚 Ocker)fakerEN_AU_ocker
en_BORK英语(博克)fakerEN_BORK
en_CA英语(加拿大)fakerEN_CA
en_GB英语(英国)fakerEN_GB
en_GH英语(加纳)fakerEN_GH
en_HK英语(香港)fakerEN_HK
en_IE英语(爱尔兰)fakerEN_IE
en_IN英语(印度)fakerEN_IN
en_NG英语(尼日利亚)fakerEN_NG
en_US英语(美国)fakerEN_US
en_ZA英语(南非)fakerEN_ZA
eo世界语fakerEO
es西班牙语fakerES
es_MX西班牙语(墨西哥)fakerES_MX
fa波斯语fakerFA
fi芬兰语fakerFI
fr法语fakerFR
fr_BE法语(比利时)fakerFR_BE
fr_CA法语(加拿大)fakerFR_CA
fr_CH法语(瑞士)fakerFR_CH
fr_LU法语(卢森堡)fakerFR_LU
fr_SN法语(塞内加尔)fakerFR_SN
he希伯来语fakerHE
hr克罗地亚语fakerHR
hu匈牙利语fakerHU
hy亚美尼亚语fakerHY
id_ID印度尼西亚语(印度尼西亚)fakerID_ID
it意大利语fakerIT
ja日语fakerJA
ka_GE格鲁吉亚语(格鲁吉亚)fakerKA_GE
ko韩语fakerKO
lv拉脱维亚语fakerLV
mk马其顿语fakerMK
nb_NO挪威语(挪威)fakerNB_NO
ne尼泊尔语fakerNE
nl荷兰语fakerNL
nl_BE荷兰语(比利时)fakerNL_BE
pl波兰语fakerPL
pt_BR葡萄牙语(巴西)fakerPT_BR
pt_PT葡萄牙语(葡萄牙)fakerPT_PT
ro罗马尼亚语fakerRO
ro_MD罗马尼亚语(摩尔多瓦)fakerRO_MD
ru俄语fakerRU
sk斯洛伐克语fakerSK
sr_RS_latin塞尔维亚语(塞尔维亚,拉丁语)fakerSR_RS_latin
sv瑞典语fakerSV
ta_IN泰米尔语(印度)fakerTA_IN
th泰语fakerTH
tr土耳其语fakerTR
uk乌克兰语fakerUK
ur乌尔都语fakerUR
uz_UZ_latin乌兹别克语(乌兹别克斯坦,拉丁语)fakerUZ_UZ_latin
vi越南语fakerVI
yo_NG约鲁巴语(尼日利亚)fakerYO_NG
zh_CN中文(中国)fakerZH_CN
zh_TW中文(台湾)fakerZH_TW
zu_ZA祖鲁语(南非)fakerZU_ZA

Locale(数据)和 Faker 列引用相应的 import 名称:

¥The Locale (data) and Faker columns refer to the respective import names:

ts
import { de, fakerDE } from '@faker-js/faker';

注释

某些语言环境的覆盖范围有限,并且更依赖英语语言环境作为它们当前没有的功能的来源。但是,在大多数情况下,使用特定的语言环境从长远来看是有益的,因为指定语言环境可以减少启动所需的时间,这对测试每次执行时重新加载导入的框架具有复合效应。

¥Some locales have limited coverage and rely more heavily on the English locale as the source for features they currently do not have. However, in most cases, using a specific locale will be beneficial in the long term as specifying a locale reduces the time necessary for startup, which has a compounding effect on testing frameworks that reload the imports every execution.

区域设置代码

¥Locale codes

语言环境以系统的方式命名。前两个字符是 ISO 639-1 标准 后面的小写语言代码,例如 ar 代表阿拉伯语,en 代表英语。

¥Locales are named in a systematic way. The first two characters are a lowercase language code following the ISO 639-1 standard for example ar for Arabic or en for English.

不同国家/地区可能使用同一种语言,地址、调用号码等的模式也不同。可选择在下划线后(ISO 3166-1 alpha-2 标准 后)添加两个字母的大写国家/地区代码,例如 en_US 代表英语(美国),en_AU 代表英语(澳大利亚)。

¥The same language may be spoken in different countries, with different patterns for addresses, phone numbers etc. Optionally a two-letter uppercase country code can be added after an underscore, following the ISO 3166-1 alpha-2 standard, for example en_US represents English (United States) and en_AU represents English (Australia).

极少数情况下,可能需要额外的变体来完全表示语言环境的重音变体,或者用于可以用不同脚本编写的语言。这附加在另一个下划线之后,例如 en_AU_ocker(澳大利亚的英语,"Ocker" 方言)或 sr_RS_latin(塞尔维亚的塞尔维亚语,拉丁字母)。

¥Rarely, an additional variant may be needed to fully represent an accented variant of the locale, or for languages which can be written in different scripts. This is appended after another underscore, for example en_AU_ocker (English in Australia in "Ocker" dialect) or sr_RS_latin (Serbian in Serbia in Latin script).

访问 Faker 实例的推荐方法是使用如上所示的单个导入之一。如果需要,你可以通过以语言环境代码为键的对象访问所有预构建的 Faker 实例或所有语言环境定义:

¥The recommended way to access Faker instances is by using one of the individual imports as shown above. If needed you can access all prebuilt Faker instances or all locale definitions via an object where the locale codes are the keys:

ts
import { allFakers, allLocales } from '@faker-js/faker';

console.dir(allFakers['de_AT']); // the prebuilt Faker instance for de_AT
console.dir(allLocales['de_AT']); // the raw locale definitions for de_AT

如果你想枚举所有语言环境,这可能会很有用,例如:

¥This could be useful if you want to enumerate all locales, for example:

ts
import { allFakers } from '@faker-js/faker';

for (let key of Object.keys(allFakers)) {
  try {
    console.log(
      `In locale ${key}, a sample name is ${allFakers[key].person.fullName()}`
    );
  } catch (e) {
    console.log(`In locale ${key}, an error occurred: ${e}`);
  }
}

处理缺失数据错误

¥Handling Missing Data Errors

txt
[Error]: The locale data for 'category.entry' are missing in this locale.
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
For more information see https://faker.nodejs.cn/guide/localization.html

如果你收到此错误,则表示你正在使用尚未具有该方法相关数据的区域设置(Faker 实例)。请考虑贡献缺失的数据,以便其他人将来也可以使用它们。

¥If you receive this error, this means you are using a locale (Faker instance) that does not have the relevant data for that method yet. Please consider contributing the missing data, so that others can use them in the future as well.

作为一种解决方法,你可以为你的 Faker 实例提供额外的后备:

¥As a workaround, you can provide additional fallbacks to your Faker instance:

ts
import { Faker, el } from '@faker-js/faker'; 
import { Faker, base, el, en } from '@faker-js/faker'; 

const faker = new Faker({
  locale: [el], 
  locale: [el, en, base], 
});
console.log(faker.location.country()); // 'Belgium'

注释

当然,你也可以为此使用 自定义语言环境和后备

¥Of course, you can use Custom Locales and Fallbacks for this as well.

处理不适用数据错误

¥Handling Not-Applicable Data Errors

txt
[Error]: The locale data for 'category.entry' aren't applicable to this locale.
If you think this is a bug, please report it at: https://github.com/faker-js/faker

如果你收到此错误,则表示当前语言环境无法为该方法提供合理的值。例如,香港没有邮政编码,因此 en_HK 语言环境无法提供这些数据。其他语言环境和方法也是如此。

¥If you receive this error, this means the current locale is unable to provide reasonable values for that method. For example, there are no zip codes in Hongkong, so for that reason the en_HK locale is unable to provide these data. The same applies to other locales and methods.

ts
import { fakerEN_HK } from '@faker-js/faker';

console.log(fakerEN_HK.location.zipCode()); // Error

对于这些情况,我们明确将数据设置为 null 以澄清我们已经考虑过它,但没有有效的值可以放在那里。我们可以使用一个空数组 [],但一些地区数据存储为对象 {},因此 null 适用于它们两者,而无需自定义下游处理缺失数据。

¥For these cases, we explicitly set the data to null to clarify, that we have thought about it, but there are no valid values to put there. We could have used an empty array [], but some locale data are stored as objects {}, so null works for both of them without custom downstream handling of missing data.

注释

我们远非所有提供的语言/国家/地区的专家,因此如果你认为这对你的地区来说是一个错误,请创建一个问题并考虑贡献相关数据。

¥We are by far no experts in all provided languages/countries/locales, so if you think this is an error for your locale, please create an issue and consider contributing the relevant data.

如果你想改用其他后备数据,可以像这样定义它们:

¥If you want to use other fallback data instead, you can define them like this:

ts
import { Faker, en, en_HK } from '@faker-js/faker';

const faker = new Faker({
  locale: [{ location: { postcode: en.location.postcode } }, en_HK],
});
console.log(faker.location.zipCode()); // '17551-0348'

警告

由于 null 被视为当前数据,因此它不会为此使用任何后备。因此以下代码不起作用:

¥Since null is considered present data, it will not use any fallbacks for that. So the following code does not work:

ts
import { Faker, en, en_HK } from '@faker-js/faker';

const faker = new Faker({
  locale: [en_HK, { location: { postcode: en.location.postcode } }], 
});
console.log(faker.location.zipCode()); // Error

另请参阅:自定义语言环境和后备

¥See also: Custom Locales and Fallbacks

Faker v9.7 中文网 - 粤ICP备13048890号.