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

docs: localization #1364

Merged
merged 12 commits into from Sep 11, 2022
1 change: 0 additions & 1 deletion docs/.vitepress/api-pages.ts
Expand Up @@ -16,7 +16,6 @@ export const apiPages = [
{ text: 'Helpers', link: '/api/helpers.html' },
{ text: 'Image', link: '/api/image.html' },
{ text: 'Internet', link: '/api/internet.html' },
{ text: 'Localization', link: '/api/localization.html' },
{ text: 'Lorem', link: '/api/lorem.html' },
{ text: 'Mersenne', link: '/api/mersenne.html' },
{ text: 'Music', link: '/api/music.html' },
Expand Down
4 changes: 4 additions & 0 deletions docs/.vitepress/config.ts
Expand Up @@ -130,6 +130,10 @@ export default defineConfig({
text: 'Getting Started',
link: '/guide/',
},
{
text: 'Localization',
link: '/guide/localization',
},
],
},
{
Expand Down
46 changes: 25 additions & 21 deletions docs/api/localization.md → docs/guide/localization.md
@@ -1,18 +1,35 @@
# Localization

As of version `v2.0.0` Faker has support for multiple localities.
## Switching locales

The default language locale is set to English.
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')`.

Setting a new locale is simple:
::: tip
Alternatively you can also just use `faker.locale = 'de'` instead to switch the locale.
:::

```js
// sets locale to de
faker.setLocale('de');
// or
faker.locale = 'de';
## Individual localized packages

By default, Faker will include **all** locale data.
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 contain 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.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved

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

## Available locales

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

<!-- Run 'pnpm run generate:locales' to update. -->
Expand Down Expand Up @@ -78,16 +95,3 @@ faker.locale = 'de';
| zu_ZA | Zulu (South Africa) |

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

## Individual Localization Packages

As of version `v3.0.0` Faker supports incremental loading of locales.

By default, requiring `faker` will include _all_ locale data.

In a production environment, you may only want to include the locale data for a specific set of locales.

```js
// loads only de locale
const { faker } = require('@faker-js/faker/locale/de');
```
1 change: 0 additions & 1 deletion scripts/apidoc.ts
Expand Up @@ -34,7 +34,6 @@ async function build(): Promise<void> {
patchProject(project);

const modulesPages: PageIndex = [];
modulesPages.push({ text: 'Localization', link: '/api/localization.html' });
modulesPages.push(...processModuleMethods(project));
modulesPages.push(...processDirectMethods(project));
writeApiPagesIndex(modulesPages);
Expand Down
2 changes: 1 addition & 1 deletion scripts/generateLocales.ts
Expand Up @@ -28,7 +28,7 @@ const pathLocalesIndex = resolve(pathLocales, 'index.ts');
const pathDocsApiLocalization = resolve(
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
pathRoot,
'docs',
'api',
'guide',
'localization.md'
);

Expand Down