Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: remove dynamic locale switching support #1735

Merged
merged 48 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
74fa8eb
refactor!: remove locale changing
ST-DDT Jan 13, 2023
8ff9c61
chore: remove broken code from tests
ST-DDT Jan 13, 2023
c1c86cd
chore: more test fixes
ST-DDT Jan 14, 2023
6113065
chore: cleanup
ST-DDT Jan 14, 2023
fa9d4d8
chore: remove unused code
ST-DDT Jan 15, 2023
174f7aa
chore: add lost comment again
ST-DDT Jan 15, 2023
b0c4e83
chore: improve email test
ST-DDT Jan 15, 2023
49bb957
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Jan 17, 2023
74dbb5f
chore: Faker class backwards compatibility
ST-DDT Jan 17, 2023
a8d1664
chore: export FakerOptions again
ST-DDT Jan 17, 2023
8948d66
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Jan 18, 2023
6880bd1
chore: explicitly throw for faker.random.locale
ST-DDT Jan 19, 2023
95fdc56
docs: handle private methods
ST-DDT Jan 19, 2023
1dc20a0
chore: cleanup
ST-DDT Jan 19, 2023
bcb10b2
chore: more cleanup
ST-DDT Jan 19, 2023
245ee98
chore: stricter types in all_functional test
ST-DDT Jan 19, 2023
78e866f
chore: merge main
ST-DDT Jan 23, 2023
3b67585
chore: merge next
ST-DDT Jan 26, 2023
179858a
chore: merge next
ST-DDT Jan 29, 2023
e6f36dd
chore: merge next
ST-DDT Jan 30, 2023
ea631fe
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Feb 1, 2023
9a3fae2
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Feb 1, 2023
02b40aa
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Feb 13, 2023
3dc29e2
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Feb 14, 2023
f5bf6b7
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Feb 15, 2023
7332e1b
docs: update documentation
ST-DDT Feb 18, 2023
23823ff
docs: add upgrade guide
ST-DDT Feb 18, 2023
2623110
Merge remote-tracking branch 'origin/next' into refactor/remove-local…
ST-DDT Feb 18, 2023
cf5fed6
chore: commit missing files
ST-DDT Feb 18, 2023
1e3c16f
docs: link to issue directly
ST-DDT Feb 18, 2023
dffc753
chore: apply suggestions and some fixes
ST-DDT Feb 18, 2023
6387d52
chore: fix typo
ST-DDT Feb 18, 2023
858020c
chore: apply suggestions
ST-DDT Feb 19, 2023
c5a4d00
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Feb 22, 2023
af9f201
chore: apply suggetions
ST-DDT Feb 22, 2023
d5a1b56
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Feb 28, 2023
aae21d7
chore: apply team decisions
ST-DDT Feb 28, 2023
349dcfe
chore: repeat test
ST-DDT Mar 1, 2023
00a07b3
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Mar 3, 2023
d404b3b
chore: apply suggestions
ST-DDT Mar 4, 2023
032ea64
Merge remote-tracking branch 'origin/next' into refactor/remove-local…
ST-DDT Mar 4, 2023
45570c0
chore: apply suggestions
ST-DDT Mar 4, 2023
ad9acac
chore: apply suggestions
ST-DDT Mar 4, 2023
e0c6663
chore: apply suggestions
ST-DDT Mar 5, 2023
ed7a76e
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Mar 5, 2023
62e599e
chore: remove debug code
ST-DDT Mar 5, 2023
b166fff
chore: really revert debug code
ST-DDT Mar 5, 2023
f3b1484
Merge branch 'next' into refactor/remove-locale-changing
ST-DDT Mar 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ npm install --save-dev @faker-js/faker
## 🪄 Usage

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

// CJS
const { faker } = require('@faker-js/faker');

export function createRandomUser(): User {
return {
Expand Down Expand Up @@ -108,7 +111,6 @@ The API covers the following modules:
| Music | `faker.music.genre()` | R&B |
| Person | `faker.person.firstName()` | Cameron |
| Phone | `faker.phone.phoneNumber()` | +1 291-299-0192 |
| Random | `faker.random.locale()` | fr_CA |
| Science | `faker.science.unit()` | `{ name: 'meter', symbol: 'm' }` |
| System | `faker.system.directoryPath()` | /root |
| Vehicle | `faker.vehicle.vehicle()` | Lamborghini Camry |
Expand All @@ -130,18 +132,30 @@ console.log(

Faker has support for multiple locales.

The default language locale is set to English.

Setting a new locale is simple:
The main `faker` instance uses the English locale.
But you can also import instances using other locales.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

```ts
// sets locale to de
faker.locale = 'de';
// ESM
import { fakerDE as faker } from '@faker-js/faker';
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

// CJS
const { fakerDE: faker } = require('@faker-js/faker');
```

See our documentation for a list of [provided languages](https://fakerjs.dev/guide/localization.html#available-locales)
See our documentation for a list of [provided languages](https://fakerjs.dev/guide/localization.html#available-locales).

Please note: Not every locale provides data for every module. In our pre-made faker instances,
we fall back to English in such a case as this is the most complete and most commonly used language.
If you don't want that or prefer a different fallback, you can also build your own instances.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

Please note: not every locale provides data for every module. In our pre-made locales, we fallback to English in such a case as this is the most complete and most commonly used language.
```ts
import { Faker, de, de_CH } from '@faker-js/faker';

export const faker = new Faker({
locale: [de_CH, de],
});
```

## ⚙️ Setting a randomness seed

Expand Down
161 changes: 96 additions & 65 deletions docs/guide/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
## Switching locales

Did you know Faker supports many different locales?
By default, when using `import { faker } from '@faker-js/faker'` actually every available locale that is supported by Faker will be loaded and you can switch the locale at runtime with `faker.setLocale('de')`.
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 50 other locales.

::: tip
Alternatively you can also just use `faker.locale = 'de'` instead to switch the locale.
`import { fakerDE as faker } from '@faker-js/faker'`

See below for a list of available locales.

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

## Individual localized packages

By default, Faker will include **all** locale data.
Currently, the imports from the main package have a [bug](https://github.com/faker-js/faker/issues/1791) 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!_
Expand All @@ -33,72 +38,98 @@ Some locales have limited coverage and rely more heavily on the English locale a
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 { Faker, de_CH, de, en } 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, global],
});
```

## Available locales

<!-- LOCALES-AUTO-GENERATED-START -->

<!-- Run 'pnpm run generate:locales' to update. -->

| Locale | Name |
| :---------- | :------------------------ |
| af_ZA | Afrikaans |
| ar | Arabic |
| az | Azerbaijani |
| cz | Czech |
| de | German |
| de_AT | German (Austria) |
| de_CH | German (Switzerland) |
| dv | Dhivehi |
| el | Greek |
| en | English |
| en_AU | English (Australia) |
| en_AU_ocker | English (Australia Ocker) |
| en_BORK | English (Bork) |
| en_CA | English (Canada) |
| en_GB | English (Great Britain) |
| en_GH | English (Ghana) |
| en_IE | English (Ireland) |
| en_IN | English (India) |
| en_NG | Nigeria (English) |
| en_US | English (United States) |
| en_ZA | English (South Africa) |
| es | Spanish |
| es_MX | Spanish (Mexico) |
| fa | Farsi |
| fi | Finnish |
| fr | French |
| fr_BE | Français (Belgique) |
| fr_CA | French (Canada) |
| fr_CH | French (Switzerland) |
| fr_LU | French (Luxembourg) |
| ge | Georgian |
| he | Hebrew |
| hr | Hrvatski |
| hu | Hungarian |
| hy | Armenian |
| id_ID | Indonesian |
| it | Italian |
| ja | Japanese |
| ko | Korean |
| lv | Latvian |
| mk | Macedonian |
| nb_NO | Norwegian |
| ne | Nepalese |
| nl | Dutch |
| nl_BE | Dutch (Belgium) |
| pl | Polish |
| pt_BR | Portuguese (Brazil) |
| pt_PT | Portuguese (Portugal) |
| ro | Romanian |
| ru | Russian |
| sk | Slovakian |
| sv | Swedish |
| tr | Turkish |
| uk | Ukrainian |
| ur | Urdu |
| vi | Vietnamese |
| zh_CN | Chinese |
| zh_TW | Chinese (Taiwan) |
| zu_ZA | Zulu (South Africa) |
| Locale | Name | Faker |
| :------------ | :------------------------ | :----------------- |
| `af_ZA` | Afrikaans | `fakerAF_ZA` |
| `ar` | Arabic | `fakerAR` |
| `az` | Azerbaijani | `fakerAZ` |
| `cz` | Czech | `fakerCZ` |
| `de` | German | `fakerDE` |
| `de_AT` | German (Austria) | `fakerDE_AT` |
| `de_CH` | German (Switzerland) | `fakerDE_CH` |
| `dv` | Dhivehi | `fakerDV` |
| `el` | Greek | `fakerEL` |
| `en` | English | `fakerEN` |
| `en_AU` | English (Australia) | `fakerEN_AU` |
| `en_AU_ocker` | English (Australia Ocker) | `fakerEN_AU_ocker` |
| `en_BORK` | English (Bork) | `fakerEN_BORK` |
| `en_CA` | English (Canada) | `fakerEN_CA` |
| `en_GB` | English (Great Britain) | `fakerEN_GB` |
| `en_GH` | English (Ghana) | `fakerEN_GH` |
| `en_IE` | English (Ireland) | `fakerEN_IE` |
| `en_IN` | English (India) | `fakerEN_IN` |
| `en_NG` | Nigeria (English) | `fakerEN_NG` |
| `en_US` | English (United States) | `fakerEN_US` |
| `en_ZA` | English (South Africa) | `fakerEN_ZA` |
| `es` | Spanish | `fakerES` |
| `es_MX` | Spanish (Mexico) | `fakerES_MX` |
| `fa` | Farsi | `fakerFA` |
| `fi` | Finnish | `fakerFI` |
| `fr` | French | `fakerFR` |
| `fr_BE` | Français (Belgique) | `fakerFR_BE` |
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
| `fr_CA` | French (Canada) | `fakerFR_CA` |
| `fr_CH` | French (Switzerland) | `fakerFR_CH` |
| `fr_LU` | French (Luxembourg) | `fakerFR_LU` |
| `ge` | Georgian | `fakerGE` |
| `he` | Hebrew | `fakerHE` |
| `hr` | Hrvatski | `fakerHR` |
| `hu` | Hungarian | `fakerHU` |
| `hy` | Armenian | `fakerHY` |
| `id_ID` | Indonesian | `fakerID_ID` |
| `it` | Italian | `fakerIT` |
| `ja` | Japanese | `fakerJA` |
| `ko` | Korean | `fakerKO` |
| `lv` | Latvian | `fakerLV` |
| `mk` | Macedonian | `fakerMK` |
| `nb_NO` | Norwegian | `fakerNB_NO` |
| `ne` | Nepalese | `fakerNE` |
| `nl` | Dutch | `fakerNL` |
| `nl_BE` | Dutch (Belgium) | `fakerNL_BE` |
| `pl` | Polish | `fakerPL` |
| `pt_BR` | Portuguese (Brazil) | `fakerPT_BR` |
| `pt_PT` | Portuguese (Portugal) | `fakerPT_PT` |
| `ro` | Romanian | `fakerRO` |
| `ru` | Russian | `fakerRU` |
| `sk` | Slovakian | `fakerSK` |
| `sv` | Swedish | `fakerSV` |
| `tr` | Turkish | `fakerTR` |
| `uk` | Ukrainian | `fakerUK` |
| `ur` | Urdu | `fakerUR` |
| `vi` | Vietnamese | `fakerVI` |
| `zh_CN` | Chinese | `fakerZH_CN` |
| `zh_TW` | Chinese (Taiwan) | `fakerZH_TW` |
| `zu_ZA` | Zulu (South Africa) | `fakerZU_ZA` |

<!-- LOCALES-AUTO-GENERATED-END -->

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

```ts
import { de, fakerDE } from '@faker-js/faker';
```
84 changes: 84 additions & 0 deletions docs/guide/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,90 @@ Not the version you are looking for?

## Breaking changes

### Removed ability to change the locale on existing `Faker` instances

:::tip NOTE
If you are using only the default (`en`) locale, then you don't have to change anything.
:::

In order to facilitate better and easier locale fallback mechanics, we removed the methods to change the locales on existing `Faker` instances.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
Now, we expose specific faker instances for each locale that you can use:

**Old**

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

faker.setLocale('de_CH');
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
// or
faker.locale = 'de_CH';
faker.fallbackLocale = 'en';
```

ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
**New**

```ts
import { fakerDE_CH as faker } from '@faker-js/faker';
```

This also fixes issues where more than two locales are required:

**Old**

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

const customFaker = new Faker({
locale: 'de_CH', // the expected locale
fallbackLocale: 'de', // ensure we have a German fallbacks for addresses
locales: { de_CH, de, en },
});
const a = customFaker.internet.email();
customFaker.locale = 'en'; // neither 'de_CH' nor 'de' have emojis
const b = customFaker.internet.emoji();
```

**New**

```ts
import { Faker, de_CH, de, en, global } from '@faker-js/faker';

// same as fakerDE_CH
export const customFaker = new Faker({
// Now multiple fallbacks are supported
locale: [de_CH, de, en, global],
});
const a = customFaker.internet.email();
const b = customFaker.internet.emoji();
```

If you wish to create entries for multiple locales, you can still do so:

**Old**

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

for (let user of users) {
const lang = faker.helpers.arrayElement(['de', 'en', 'fr']);
faker.locale = lang;
user.email = faker.internet.email();
}
```

**New**

```ts
import { faker, fakerDE, fakerEN, fakerFR } from '@faker-js/faker';

for (let user of users) {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
const currentFaker = faker.helpers.arrayElement([fakerDE, fakerEN, fakerFR]);
user.email = currentFaker.internet.email();
}
```

For more information refer to our [Localization Guide](localization).

### `faker.mersenne` and `faker.helpers.repeatString` removed

`faker.mersenne` and `faker.helpers.repeatString` were only ever intended for internal use, and are no longer available.
Expand Down
12 changes: 4 additions & 8 deletions docs/guide/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@ Using Faker is as easy as importing it from `@faker-js/faker`.

```js
import { faker } from '@faker-js/faker';
// or, if desiring only a specific locale
// import { faker } from '@faker-js/faker/locale/de'
// or, if desiring a different locale
// import { fakerDE as faker } from '@faker-js/faker'
const randomName = faker.person.fullName(); // Rowan Nikolaus
const randomEmail = faker.internet.email(); // [email protected]
```

:::tip Note
Using the first import statement will load every locale into memory.
As such, start-up times and performance may be slow.
Thus, by declaring a locale in the import, one can increase performance and reduce the time on start-up.
:::

Or if you're using CommonJS:

```js
Expand All @@ -27,6 +21,8 @@ const randomName = faker.person.fullName(); // Rowan Nikolaus
const randomEmail = faker.internet.email(); // [email protected]
```

For more information about changing and customizing the locales, please refer to our [Localization Guide](localization).

## Browser

```html
Expand Down
4 changes: 3 additions & 1 deletion scripts/apidoc/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export function selectApiModules(
export function selectApiMethods(
module: DeclarationReflection
): DeclarationReflection[] {
return module.getChildrenByKind(ReflectionKind.Method);
return module
.getChildrenByKind(ReflectionKind.Method)
.filter((method) => !method.flags.isPrivate);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Loading