@@ -62,12 +62,12 @@ function format(id, parameters, locale) {
6262}
6363function getPluralizationRule ( number , locale ) {
6464 number = Math . abs ( number ) ;
65- let _locale2 = locale ;
65+ let _locale = locale ;
6666 if ( locale === "pt_BR" || locale === "en_US_POSIX" ) {
6767 return 0 ;
6868 }
69- _locale2 = _locale2 . length > 3 ? _locale2 . substring ( 0 , _locale2 . indexOf ( "_" ) ) : _locale2 ;
70- switch ( _locale2 ) {
69+ _locale = _locale . length > 3 ? _locale . substring ( 0 , _locale . indexOf ( "_" ) ) : _locale ;
70+ switch ( _locale ) {
7171 case "af" :
7272 case "bn" :
7373 case "bg" :
@@ -189,71 +189,78 @@ function formatIntl(id, parameters, locale) {
189189}
190190
191191// src/translator_controller.ts
192- var _locale = null ;
193- var _localeFallbacks = { } ;
194- var _throwWhenNotFound = false ;
195- function setLocale ( locale ) {
196- _locale = locale ;
197- }
198- function getLocale ( ) {
199- return _locale || document . documentElement . getAttribute ( "data-symfony-ux-translator-locale" ) || // <html data-symfony-ux-translator-locale="en_US">
192+ function getDefaultLocale ( ) {
193+ return document . documentElement . getAttribute ( "data-symfony-ux-translator-locale" ) || // <html data-symfony-ux-translator-locale="en_US">
200194 ( document . documentElement . lang ? document . documentElement . lang . replace ( "-" , "_" ) : null ) || // <html lang="en-US">
201195 "en" ;
202196}
203- function throwWhenNotFound ( enabled ) {
204- _throwWhenNotFound = enabled ;
205- }
206- function setLocaleFallbacks ( localeFallbacks ) {
207- _localeFallbacks = localeFallbacks ;
208- }
209- function getLocaleFallbacks ( ) {
210- return _localeFallbacks ;
211- }
212- function trans ( message , parameters = { } , domain = "messages" , locale = null ) {
213- if ( typeof domain === "undefined" ) {
214- domain = "messages" ;
197+ function createTranslator ( {
198+ messages ,
199+ locale = getDefaultLocale ( ) ,
200+ localeFallbacks = { } ,
201+ throwWhenNotFound = false
202+ } ) {
203+ const _messages = messages ;
204+ const _localeFallbacks = localeFallbacks ;
205+ let _locale = locale ;
206+ let _throwWhenNotFound = throwWhenNotFound ;
207+ function setLocale ( locale2 ) {
208+ _locale = locale2 ;
215209 }
216- if ( typeof locale === "undefined" || null === locale ) {
217- locale = getLocale ( ) ;
210+ function getLocale ( ) {
211+ return _locale ;
218212 }
219- if ( typeof message . translations === "undefined" ) {
220- return message . id ;
213+ function setThrowWhenNotFound ( throwWhenNotFound2 ) {
214+ _throwWhenNotFound = throwWhenNotFound2 ;
221215 }
222- const localesFallbacks = getLocaleFallbacks ( ) ;
223- const translationsIntl = message . translations [ `${ domain } +intl-icu` ] ;
224- if ( typeof translationsIntl !== "undefined" ) {
225- while ( typeof translationsIntl [ locale ] === "undefined" ) {
226- locale = localesFallbacks [ locale ] ;
227- if ( ! locale ) {
228- break ;
229- }
216+ function trans ( id , parameters = { } , domain = "messages" , locale2 = null ) {
217+ if ( typeof domain === "undefined" ) {
218+ domain = "messages" ;
230219 }
231- if ( locale ) {
232- return formatIntl ( translationsIntl [ locale ] , parameters , locale ) ;
220+ if ( typeof locale2 === "undefined" || null === locale2 ) {
221+ locale2 = _locale ;
233222 }
234- }
235- const translations = message . translations [ domain ] ;
236- if ( typeof translations !== "undefined" ) {
237- while ( typeof translations [ locale ] === "undefined" ) {
238- locale = localesFallbacks [ locale ] ;
239- if ( ! locale ) {
240- break ;
223+ const message = _messages [ id ] ?? null ;
224+ if ( message === null ) {
225+ return id ;
226+ }
227+ const translationsIntl = message . translations [ `${ domain } +intl-icu` ] ?? void 0 ;
228+ if ( typeof translationsIntl !== "undefined" ) {
229+ while ( typeof translationsIntl [ locale2 ] === "undefined" ) {
230+ locale2 = _localeFallbacks [ locale2 ] ;
231+ if ( ! locale2 ) {
232+ break ;
233+ }
234+ }
235+ if ( locale2 ) {
236+ return formatIntl ( translationsIntl [ locale2 ] , parameters , locale2 ) ;
241237 }
242238 }
243- if ( locale ) {
244- return format ( translations [ locale ] , parameters , locale ) ;
239+ const translations = message . translations [ domain ] ?? void 0 ;
240+ if ( typeof translations !== "undefined" ) {
241+ while ( typeof translations [ locale2 ] === "undefined" ) {
242+ locale2 = _localeFallbacks [ locale2 ] ;
243+ if ( ! locale2 ) {
244+ break ;
245+ }
246+ }
247+ if ( locale2 ) {
248+ return format ( translations [ locale2 ] , parameters , locale2 ) ;
249+ }
245250 }
251+ if ( _throwWhenNotFound ) {
252+ throw new Error ( `No translation message found with id "${ id } ".` ) ;
253+ }
254+ return id ;
246255 }
247- if ( _throwWhenNotFound ) {
248- throw new Error ( `No translation message found with id "${ message . id } ".` ) ;
249- }
250- return message . id ;
256+ return {
257+ setLocale,
258+ getLocale,
259+ setThrowWhenNotFound,
260+ trans
261+ } ;
251262}
252263export {
253- getLocale ,
254- getLocaleFallbacks ,
255- setLocale ,
256- setLocaleFallbacks ,
257- throwWhenNotFound ,
258- trans
264+ createTranslator ,
265+ getDefaultLocale
259266} ;
0 commit comments