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

feat: add base locale #1748

Merged
merged 15 commits into from
Mar 29, 2023
13 changes: 11 additions & 2 deletions docs/guide/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ If our built-in faker instances don't satisfy your needs, you can build your own

matthewmayer marked this conversation as resolved.
Show resolved Hide resolved
```ts
import type { LocaleDefinition } from '@faker-js/faker';
import { Faker, de_CH, de, en } from '@faker-js/faker';
import { Faker, de_CH, de, en, base } from '@faker-js/faker';

const customLocale: LocaleDefinition = {
title: 'My custom locale',
Expand All @@ -54,10 +54,18 @@ const customLocale: LocaleDefinition = {
};

export const customFaker = new Faker({
locale: [customLocale, de_CH, de, en, global],
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

<!-- LOCALES-AUTO-GENERATED-START -->
Expand All @@ -69,6 +77,7 @@ export const customFaker = new Faker({
| `af_ZA` | Afrikaans | `fakerAF_ZA` |
| `ar` | Arabic | `fakerAR` |
| `az` | Azerbaijani | `fakerAZ` |
| `base` | Base | `fakerBASE` |
| `cz` | Czech | `fakerCZ` |
| `de` | German | `fakerDE` |
| `de_AT` | German (Austria) | `fakerDE_AT` |
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ const b = customFaker.internet.emoji();
**New**

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

// same as fakerDE_CH
export const customFaker = new Faker({
// Now multiple fallbacks are supported
locale: [de_CH, de, en, global],
locale: [de_CH, de, en, base],
});
const a = customFaker.internet.email();
const b = customFaker.internet.emoji();
Expand Down
20 changes: 14 additions & 6 deletions scripts/generateLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ function generateLocaleFile(locale: string): void {
}
}

if (locales[locales.length - 1] !== 'en') {
// TODO christopher 2023-03-07: Remove 'en' fallback in a separate PR
if (locales[locales.length - 1] !== 'en' && locale !== 'base') {
locales.push('en');
}

if (locales[locales.length - 1] !== 'base') {
locales.push('base');
}

let content = `
${autoGeneratedCommentHeader}

Expand All @@ -148,8 +153,10 @@ function generateLocaleFile(locale: string): void {
writeFileSync(resolve(pathLocale, `${locale}.ts`), content);
}

function tryLoadLocalesMainIndexFile(pathModules: string): LocaleDefinition {
let localeDef: LocaleDefinition;
function tryLoadLocalesMainIndexFile(
pathModules: string
): LocaleDefinition | undefined {
let localeDef: LocaleDefinition | undefined;
// This call might fail, if the module setup is broken.
// Unfortunately, we try to fix it with this script
// Thats why have a fallback logic here, we only need the title anyway
Expand All @@ -165,9 +172,10 @@ function tryLoadLocalesMainIndexFile(pathModules: string): LocaleDefinition {
resolve(pathModules, 'index.ts'),
'utf-8'
);
localeDef = {
title: localeIndex.match(/title: '(.*)',/)[1],
};
const title = localeIndex.match(/title: '(.*)',/)?.[1];
if (title) {
localeDef = { title };
}
} catch {
console.error(`Failed to load ${pathModules} or manually parse it.`, e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/locale/af_ZA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import { Faker } from '../faker';
import af_ZA from '../locales/af_ZA';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: [af_ZA, en],
locale: [af_ZA, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import { Faker } from '../faker';
import ar from '../locales/ar';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: [ar, en],
locale: [ar, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/az.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import { Faker } from '../faker';
import az from '../locales/az';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: [az, en],
locale: [az, en, base],
});
11 changes: 11 additions & 0 deletions src/locale/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/

import { Faker } from '../faker';
import base from '../locales/base';

export const faker = new Faker({
locale: base,
});
3 changes: 2 additions & 1 deletion src/locale/cz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import cz from '../locales/cz';
import en from '../locales/en';

export const faker = new Faker({
locale: [cz, en],
locale: [cz, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import de from '../locales/de';
import en from '../locales/en';

export const faker = new Faker({
locale: [de, en],
locale: [de, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/de_AT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import de from '../locales/de';
import de_AT from '../locales/de_AT';
import en from '../locales/en';

export const faker = new Faker({
locale: [de_AT, de, en],
locale: [de_AT, de, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/de_CH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import de from '../locales/de';
import de_CH from '../locales/de_CH';
import en from '../locales/en';

export const faker = new Faker({
locale: [de_CH, de, en],
locale: [de_CH, de, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/dv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import dv from '../locales/dv';
import en from '../locales/en';

export const faker = new Faker({
locale: [dv, en],
locale: [dv, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/el.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import el from '../locales/el';
import en from '../locales/en';

export const faker = new Faker({
locale: [el, en],
locale: [el, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: en,
locale: [en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_AU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_AU from '../locales/en_AU';

export const faker = new Faker({
locale: [en_AU, en],
locale: [en_AU, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_AU_ocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_AU from '../locales/en_AU';
import en_AU_ocker from '../locales/en_AU_ocker';

export const faker = new Faker({
locale: [en_AU_ocker, en_AU, en],
locale: [en_AU_ocker, en_AU, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_BORK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_BORK from '../locales/en_BORK';

export const faker = new Faker({
locale: [en_BORK, en],
locale: [en_BORK, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_CA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_CA from '../locales/en_CA';

export const faker = new Faker({
locale: [en_CA, en],
locale: [en_CA, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_GB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_GB from '../locales/en_GB';

export const faker = new Faker({
locale: [en_GB, en],
locale: [en_GB, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_GH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_GH from '../locales/en_GH';

export const faker = new Faker({
locale: [en_GH, en],
locale: [en_GH, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_IE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_IE from '../locales/en_IE';

export const faker = new Faker({
locale: [en_IE, en],
locale: [en_IE, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_IN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_IN from '../locales/en_IN';

export const faker = new Faker({
locale: [en_IN, en],
locale: [en_IN, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_NG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_NG from '../locales/en_NG';

export const faker = new Faker({
locale: [en_NG, en],
locale: [en_NG, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_US from '../locales/en_US';

export const faker = new Faker({
locale: [en_US, en],
locale: [en_US, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_ZA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_ZA from '../locales/en_ZA';

export const faker = new Faker({
locale: [en_ZA, en],
locale: [en_ZA, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import es from '../locales/es';

export const faker = new Faker({
locale: [es, en],
locale: [es, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/es_MX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import es from '../locales/es';
import es_MX from '../locales/es_MX';

export const faker = new Faker({
locale: [es_MX, es, en],
locale: [es_MX, es, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import fa from '../locales/fa';

export const faker = new Faker({
locale: [fa, en],
locale: [fa, en, base],
});
Loading