2020-09-20 15:14:26 +02:00
import { derived } from 'svelte/store' ;
2020-01-16 01:47:43 +01:00
2020-12-03 19:08:34 +01:00
import type {
2020-01-16 01:47:43 +01:00
MessageFormatter ,
MessageObject ,
TimeFormatter ,
DateFormatter ,
NumberFormatter ,
2020-11-23 14:50:58 +01:00
JSONGetter ,
2020-09-20 15:14:26 +02:00
} from '../types' ;
import { lookup } from '../includes/lookup' ;
import { hasLocaleQueue } from '../includes/loaderQueue' ;
2020-01-16 01:47:43 +01:00
import {
getMessageFormatter ,
getTimeFormatter ,
getDateFormatter ,
getNumberFormatter ,
2020-09-20 15:14:26 +02:00
} from '../includes/formatters' ;
import { getOptions } from '../configs' ;
import { $dictionary } from './dictionary' ;
2021-03-27 17:16:28 +01:00
import { getCurrentLocale , getPossibleLocales , $locale } from './locale' ;
2020-01-16 01:47:43 +01:00
const formatMessage : MessageFormatter = ( id , options = { } ) = > {
2021-08-24 14:08:37 +02:00
let messageObj = options as MessageObject ;
2020-01-16 01:47:43 +01:00
if ( typeof id === 'object' ) {
2021-08-24 14:08:37 +02:00
messageObj = id as MessageObject ;
id = messageObj . id ;
2020-01-16 01:47:43 +01:00
}
2020-09-20 15:14:26 +02:00
const {
values ,
locale = getCurrentLocale ( ) ,
default : defaultValue ,
2021-08-24 14:08:37 +02:00
} = messageObj ;
2020-01-16 01:47:43 +01:00
if ( locale == null ) {
throw new Error (
2020-09-20 15:14:26 +02:00
'[svelte-i18n] Cannot format a message without first setting the initial locale.' ,
) ;
2020-01-16 01:47:43 +01:00
}
2020-11-05 21:52:22 +01:00
let message = lookup ( id , locale ) ;
2020-01-16 01:47:43 +01:00
if ( ! message ) {
if ( getOptions ( ) . warnOnMissingMessages ) {
// istanbul ignore next
console . warn (
2021-03-27 17:16:28 +01:00
` [svelte-i18n] The message " ${ id } " was not found in " ${ getPossibleLocales (
2020-09-20 15:14:26 +02:00
locale ,
2020-01-16 01:47:43 +01:00
) . join ( '", "' ) } " . $ {
2021-08-24 14:08:37 +02:00
hasLocaleQueue ( getCurrentLocale ( ) )
2020-01-16 01:47:43 +01:00
? ` \ n \ nNote: there are at least one loader still registered to this locale that wasn't executed. `
: ''
2020-09-20 15:14:26 +02:00
} ` ,
) ;
2020-01-16 01:47:43 +01:00
}
2021-09-30 16:06:23 +02:00
message = defaultValue ? ? id ;
2020-11-23 14:50:58 +01:00
} else if ( typeof message !== 'string' ) {
2020-11-24 14:48:44 +01:00
console . warn (
2020-11-23 14:50:58 +01:00
` [svelte-i18n] Message with id " ${ id } " must be of type "string", found: " ${ typeof message } ". Gettin its value through the " $ format" method is deprecated; use the "json" method instead. ` ,
) ;
return message ;
2020-01-16 01:47:43 +01:00
}
2020-11-23 14:50:58 +01:00
if ( ! values ) {
return message ;
}
2020-09-20 15:14:26 +02:00
2021-02-15 17:36:28 +01:00
let result = message ;
try {
result = getMessageFormatter ( message , locale ) . format ( values ) as string ;
} catch ( e ) {
2021-02-15 17:32:20 +01:00
console . warn ( ` [svelte-i18n] Message " ${ id } " has syntax error: ` , e . message ) ;
2021-02-15 17:36:28 +01:00
}
return result ;
2020-09-20 15:14:26 +02:00
} ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
const formatTime : TimeFormatter = ( t , options ) = > {
return getTimeFormatter ( options ) . format ( t ) ;
} ;
const formatDate : DateFormatter = ( d , options ) = > {
return getDateFormatter ( options ) . format ( d ) ;
} ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
const formatNumber : NumberFormatter = ( n , options ) = > {
return getNumberFormatter ( options ) . format ( n ) ;
} ;
2020-01-16 01:47:43 +01:00
2021-08-24 14:08:37 +02:00
const getJSON : JSONGetter = < T = any > (
id : string ,
locale = getCurrentLocale ( ) ,
) = > {
return lookup ( id , locale ) as T ;
2020-11-23 14:50:58 +01:00
} ;
2020-01-16 01:47:43 +01:00
2020-09-20 15:14:26 +02:00
export const $format = derived ( [ $locale , $dictionary ] , ( ) = > formatMessage ) ;
export const $formatTime = derived ( [ $locale ] , ( ) = > formatTime ) ;
export const $formatDate = derived ( [ $locale ] , ( ) = > formatDate ) ;
export const $formatNumber = derived ( [ $locale ] , ( ) = > formatNumber ) ;
2020-11-23 15:00:59 +01:00
export const $getJSON = derived ( [ $locale , $dictionary ] , ( ) = > getJSON ) ;