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' ;
import { getCurrentLocale , getRelatedLocalesOf , $locale } from './locale' ;
2020-01-16 01:47:43 +01:00
const formatMessage : MessageFormatter = ( id , options = { } ) = > {
if ( typeof id === 'object' ) {
2020-09-20 15:14:26 +02:00
options = id as MessageObject ;
id = options . id ;
2020-01-16 01:47:43 +01:00
}
2020-09-20 15:14:26 +02:00
const {
values ,
locale = getCurrentLocale ( ) ,
default : defaultValue ,
} = options ;
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 (
` [svelte-i18n] The message " ${ id } " was not found in " ${ getRelatedLocalesOf (
2020-09-20 15:14:26 +02:00
locale ,
2020-01-16 01:47:43 +01:00
) . join ( '", "' ) } " . $ {
hasLocaleQueue ( getCurrentLocale ( ) )
? ` \ 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
}
2020-11-05 21:52:22 +01: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
2020-11-23 11:56:58 +01:00
return getMessageFormatter ( message , locale ) . format ( values ) as string ;
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-02-11 01:22:48 +01:00
const getJSON : JSONGetter = < T = any > ( id : string , locale = getCurrentLocale ( ) ) = > {
2020-11-23 14:50:58 +01:00
return lookup ( id , locale ) as T ;
} ;
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 ) ;