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' ;
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' ;
2022-04-05 16:38:45 +02:00
import { getCurrentLocale , $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 ) {
2022-04-05 16:38:45 +02:00
message =
getOptions ( ) . handleMissingMessage ? . ( { locale , id , defaultValue } ) ? ?
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 ) ;