Skip to content

Commit c1f2b09

Browse files
authored
feat: add autocomplete support for locales (#248)
1 parent 6ba72a4 commit c1f2b09

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

src/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Hacker } from './hacker';
1212
import { Helpers } from './helpers';
1313
import { Image } from './image';
1414
import { Internet } from './internet';
15+
import type { KnownLocale } from './locales';
1516
import allLocales from './locales';
1617
import { Lorem } from './lorem';
1718
import { Mersenne } from './mersenne';
@@ -25,6 +26,9 @@ import { Unique } from './unique';
2526
import { Vehicle } from './vehicle';
2627
import { Word } from './word';
2728

29+
// https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
30+
type LiteralUnion<T extends U, U = string> = T | (U & { zz_IGNORE_ME?: never });
31+
2832
export interface LocaleDefinition {
2933
title: string;
3034
separator?: string;
@@ -186,12 +190,13 @@ export interface LocaleDefinition {
186190
[group: string]: any;
187191
}
188192

193+
export type UsableLocale = LiteralUnion<KnownLocale>;
194+
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;
195+
189196
export interface FakerOptions {
190-
locales?: {
191-
[locale: string]: LocaleDefinition;
192-
};
193-
locale?: string;
194-
localeFallback?: string;
197+
locales?: UsedLocales;
198+
locale?: UsableLocale;
199+
localeFallback?: UsableLocale;
195200
}
196201

197202
export interface DefinitionTypes {
@@ -216,11 +221,9 @@ export interface DefinitionTypes {
216221
}
217222

218223
export class Faker {
219-
locales: {
220-
[locale: string]: LocaleDefinition;
221-
};
222-
locale: string;
223-
localeFallback: string;
224+
locales: UsedLocales;
225+
locale: UsableLocale;
226+
localeFallback: UsableLocale;
224227

225228
// TODO @Shinigami92 2022-01-11: For now we loose types here
226229
// @ts-expect-error: will be lazy filled by constructor
@@ -432,7 +435,7 @@ export class Faker {
432435
*
433436
* @param locale
434437
*/
435-
setLocale(locale: string): void {
438+
setLocale(locale: UsableLocale): void {
436439
this.locale = locale;
437440
}
438441
}

src/locales/index.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,67 @@ import zh_CN from './zh_CN';
5656
import zh_TW from './zh_TW';
5757
import zu_ZA from './zu_ZA';
5858

59-
const locales: { [lang: string]: LocaleDefinition } = {
59+
export type KnownLocale =
60+
| 'af_ZA'
61+
| 'ar'
62+
| 'az'
63+
| 'cz'
64+
| 'de'
65+
| 'de_AT'
66+
| 'de_CH'
67+
| 'el'
68+
| 'en'
69+
| 'en_AU'
70+
| 'en_AU_ocker'
71+
| 'en_BORK'
72+
| 'en_CA'
73+
| 'en_GB'
74+
| 'en_GH'
75+
| 'en_IE'
76+
| 'en_IND'
77+
| 'en_NG'
78+
| 'en_US'
79+
| 'en_ZA'
80+
| 'es'
81+
| 'es_MX'
82+
| 'fa'
83+
| 'fi'
84+
| 'fr'
85+
| 'fr_BE'
86+
| 'fr_CA'
87+
| 'fr_CH'
88+
| 'ge'
89+
| 'he'
90+
| 'hr'
91+
| 'hy'
92+
| 'id_ID'
93+
| 'it'
94+
| 'ja'
95+
| 'ko'
96+
| 'lv'
97+
| 'mk'
98+
| 'nb_NO'
99+
| 'ne'
100+
| 'nl'
101+
| 'nl_BE'
102+
| 'pl'
103+
| 'pt_BR'
104+
| 'pt_PT'
105+
| 'ro'
106+
| 'ru'
107+
| 'sk'
108+
| 'sv'
109+
| 'tr'
110+
| 'uk'
111+
| 'ur'
112+
| 'vi'
113+
| 'zh_CN'
114+
| 'zh_TW'
115+
| 'zu_ZA';
116+
117+
export type KnownLocales = Record<KnownLocale, LocaleDefinition>;
118+
119+
const locales: KnownLocales = {
60120
af_ZA,
61121
ar,
62122
az,

0 commit comments

Comments
 (0)