Appearance
本地化
¥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 withCH
(Switzerland) data.de
是通用的de
(德语)语言环境定义。¥
de
is a genericde
(German) locale definition.en
是一个通用的en
(英语)语言环境定义。这是我们最完整的语言环境,因此我们添加它来填补一些空白。根据你的需要,你可能希望或不希望将其作为后备。¥
en
is a genericen
(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'
处理不适用数据错误
¥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