@@ -26,7 +26,7 @@ import { SportModule } from './modules/sport'
2626import { HackerModule } from './modules/hacker'
2727import { SystemModule } from './modules/system'
2828import { WordModule } from './modules/word'
29- import { locales } from './locales '
29+ import { LocaleLoader } from './locale-loader '
3030
3131/**
3232 * Main Faker class - the entry point for generating fake data
@@ -75,7 +75,17 @@ export class Faker {
7575 const locale = options ?. locale ?? 'en'
7676 const seed = options ?. seed
7777
78- this . _locale = locales [ locale ] ?? locales . en
78+ // For non-English locales, warn about async loading
79+ if ( locale !== 'en' && ! LocaleLoader . isCached ( locale ) ) {
80+ console . warn (
81+ `[nanofaker] Locale '${ locale } ' requires async loading. Use 'await Faker.create({ locale: "${ locale } " })' for better performance. Falling back to English.` ,
82+ )
83+ this . _locale = LocaleLoader . loadSync ( 'en' )
84+ }
85+ else {
86+ this . _locale = LocaleLoader . loadSync ( locale )
87+ }
88+
7989 this . _random = new Random ( seed )
8090
8191 // Initialize all modules
@@ -107,6 +117,21 @@ export class Faker {
107117 this . vehicle = new VehicleModule ( this . _random , this . _locale )
108118 }
109119
120+ /**
121+ * Create a Faker instance with async locale loading
122+ * @example await Faker.create({ locale: 'es' })
123+ */
124+ static async create ( options ?: FakerOptions ) : Promise < Faker > {
125+ const locale = options ?. locale ?? 'en'
126+
127+ // Preload the locale if needed
128+ if ( ! LocaleLoader . isCached ( locale ) ) {
129+ await LocaleLoader . load ( locale )
130+ }
131+
132+ return new Faker ( options )
133+ }
134+
110135 /**
111136 * Set a seed for reproducible results
112137 * @example faker.seed(12345)
@@ -146,13 +171,50 @@ export class Faker {
146171 }
147172
148173 /**
149- * Set the locale
150- * @example faker.setLocale('es')
174+ * Set the locale asynchronously (recommended for non-English locales)
175+ * @example await faker.setLocale('es')
151176 */
152- setLocale ( locale : string ) : this {
153- this . _locale = locales [ locale ] ?? locales . en
177+ async setLocale ( locale : string ) : Promise < this> {
178+ this . _locale = await LocaleLoader . load ( locale )
154179
155180 // Reinitialize modules that depend on locale
181+ this . reinitializeLocaleModules ( )
182+
183+ return this
184+ }
185+
186+ /**
187+ * Set the locale synchronously (only works for cached locales)
188+ * @deprecated Use async setLocale() instead
189+ * @example faker.setLocaleSync('es') // Only works if 'es' is already loaded
190+ */
191+ setLocaleSync ( locale : string ) : this {
192+ if ( ! LocaleLoader . isCached ( locale ) && locale !== 'en' ) {
193+ console . warn (
194+ `[nanofaker] Locale '${ locale } ' is not loaded. Use 'await faker.setLocale("${ locale } ")' or preload it first.` ,
195+ )
196+ return this
197+ }
198+
199+ this . _locale = LocaleLoader . loadSync ( locale )
200+ this . reinitializeLocaleModules ( )
201+
202+ return this
203+ }
204+
205+ /**
206+ * Preload locales for offline usage
207+ * @example await faker.preloadLocales(['es', 'fr', 'de'])
208+ */
209+ async preloadLocales ( locales : string [ ] ) : Promise < this> {
210+ await LocaleLoader . preload ( locales )
211+ return this
212+ }
213+
214+ /**
215+ * Reinitialize all modules that depend on locale
216+ */
217+ private reinitializeLocaleModules ( ) : void {
156218 Object . assign ( this . person , new PersonModule ( this . _random , this . _locale ) )
157219 Object . assign ( this . address , new AddressModule ( this . _random , this . _locale ) )
158220 Object . assign ( this . internet , new InternetModule ( this . _random , this . _locale ) )
@@ -170,8 +232,6 @@ export class Faker {
170232 Object . assign ( this . system , new SystemModule ( this . _random , this . _locale ) )
171233 Object . assign ( this . color , new ColorModule ( this . _random , this . _locale ) )
172234 Object . assign ( this . science , new ScienceModule ( this . _random , this . _locale ) )
173-
174- return this
175235 }
176236
177237 /**
@@ -184,8 +244,23 @@ export class Faker {
184244 /**
185245 * Get available locales
186246 */
187- static get availableLocales ( ) : string [ ] {
188- return Object . keys ( locales )
247+ static get availableLocales ( ) : readonly string [ ] {
248+ return LocaleLoader . getAvailableLocales ( )
249+ }
250+
251+ /**
252+ * Check if a locale is currently loaded/cached
253+ */
254+ static isLocaleLoaded ( locale : string ) : boolean {
255+ return LocaleLoader . isCached ( locale )
256+ }
257+
258+ /**
259+ * Preload multiple locales statically
260+ * @example await Faker.preloadLocales(['es', 'fr', 'de'])
261+ */
262+ static async preloadLocales ( locales : string [ ] ) : Promise < void > {
263+ await LocaleLoader . preload ( locales )
189264 }
190265
191266 /**
0 commit comments