Skip to content

Localization

Switching locales

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 60 available locales.

For example, you can import the German locale:

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

Note

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

Individual localized packages

Currently, the imports from the main package have a bug and always cause the entire Faker lib to be imported. This might result in loading around 5 MB of data into memory and slow down startup times.

But we got your back!
When encountering such a problem in a test or production environment, you can use the individual localized packages.

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

This will then just load the German locales with additional English locales as fallback. The fallback is required due to not all locales containing data for all features. If you encounter a missing locale entry in your selected language, feel free to open a Pull Request fixing that issue.

Info

The English locales are around 600 KB in size.
All locales together are around 5 MB in size.

Note

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.

Custom locales and fallbacks

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],
});

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 is your custom locale definition which will override all other fallback definitions.
  • de_CH is a specific locale definition that overrides some German definitions with CH (Switzerland) data.
  • de is a generic de (German) locale definition.
  • 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 is the base locale definition which contains definitions that can be used in every language (e.g. emojis).

Available locales

LocaleNameFaker
af_ZAAfrikaans (South Africa)fakerAF_ZA
arArabicfakerAR
azAzerbaijanifakerAZ
baseBasefakerBASE
cs_CZCzech (Czechia)fakerCS_CZ
daDanishfakerDA
deGermanfakerDE
de_ATGerman (Austria)fakerDE_AT
de_CHGerman (Switzerland)fakerDE_CH
dvMaldivianfakerDV
elGreekfakerEL
enEnglishfakerEN
en_AUEnglish (Australia)fakerEN_AU
en_AU_ockerEnglish (Australia Ocker)fakerEN_AU_ocker
en_BORKEnglish (Bork)fakerEN_BORK
en_CAEnglish (Canada)fakerEN_CA
en_GBEnglish (Great Britain)fakerEN_GB
en_GHEnglish (Ghana)fakerEN_GH
en_HKEnglish (Hong Kong)fakerEN_HK
en_IEEnglish (Ireland)fakerEN_IE
en_INEnglish (India)fakerEN_IN
en_NGEnglish (Nigeria)fakerEN_NG
en_USEnglish (United States)fakerEN_US
en_ZAEnglish (South Africa)fakerEN_ZA
eoEsperantofakerEO
esSpanishfakerES
es_MXSpanish (Mexico)fakerES_MX
faFarsi/PersianfakerFA
fiFinnishfakerFI
frFrenchfakerFR
fr_BEFrench (Belgium)fakerFR_BE
fr_CAFrench (Canada)fakerFR_CA
fr_CHFrench (Switzerland)fakerFR_CH
fr_LUFrench (Luxembourg)fakerFR_LU
fr_SNFrench (Senegal)fakerFR_SN
heHebrewfakerHE
hrCroatianfakerHR
huHungarianfakerHU
hyArmenianfakerHY
id_IDIndonesian (Indonesia)fakerID_ID
itItalianfakerIT
jaJapanesefakerJA
ka_GEGeorgian (Georgia)fakerKA_GE
koKoreanfakerKO
lvLatvianfakerLV
mkMacedonianfakerMK
nb_NONorwegian (Norway)fakerNB_NO
neNepalifakerNE
nlDutchfakerNL
nl_BEDutch (Belgium)fakerNL_BE
plPolishfakerPL
pt_BRPortuguese (Brazil)fakerPT_BR
pt_PTPortuguese (Portugal)fakerPT_PT
roRomanianfakerRO
ro_MDRomanian (Moldova)fakerRO_MD
ruRussianfakerRU
skSlovakfakerSK
sr_RS_latinSerbian (Serbia, Latin)fakerSR_RS_latin
svSwedishfakerSV
thThaifakerTH
trTurkishfakerTR
ukUkrainianfakerUK
urUrdufakerUR
viVietnamesefakerVI
yo_NGYoruba (Nigeria)fakerYO_NG
zh_CNChinese (China)fakerZH_CN
zh_TWChinese (Taiwan)fakerZH_TW
zu_ZAZulu (South Africa)fakerZU_ZA

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

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

Locale codes

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.

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).

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).

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}`);
  }
}

Released under the MIT License.