import { FC , Fragment , ReactNode } from 'react' ; import isEmpty from 'lodash/isEmpty' ; import isFunction from 'lodash/isFunction' ; const fallbackLanguage = 'en' ; const repository = { } ; type LanguageCode = 'en' | 'de' | 'el' | 'es' | 'fi' | 'fr' | 'ht' | 'hu' | 'id' | 'it' | 'ja' | 'nl' | 'no' | 'pl' | 'pt' | 'ru' | 'ko' | 'tr' | 'vi' | 'zhcn' | 'zhtw' ; interface ILanguageData { code : LanguageCode ; data : { [ translationName ] : string ; } ; } const languages = [ require ( './lang/en.json' ) ] ; languages . forEach ( ( l ) => { repository [ l . code ] = l . data ; } ) ; export function getTranslators ( ) { return [ 'TranslatorName_1' , 'TranslatorName_2' , 'TranslatorName_3' , 'TranslatorName_4' , 'TranslatorName_5' ] . filter ( ( x ) => { const translated = translateRaw ( x ) ; return ! ! translated ; } ) ; } export function translateRaw ( key , variables ? ) { const settings = { language : 'en' } ; const language = settings . language || fallbackLanguage ; const translatedString = ( repository [ language ] && repository [ language ] [ key ] ) || repository [ fallbackLanguage ] [ key ] || key ; desc if ( variables ) { try { let str = translatedString . replace ( /\$/g , '__' ) ; Object . keys ( variables ) . forEach ( ( variable ) => { const singleWordVariable = variable . replace ( /\$/g , '__' ) ; const re = new RegExp ( `\\b${ singleWordVariable }\\b` , 'g' ) ; const escaped = variables [ variable ] . replace ( /\$/g , '$$$$' ) ; str = str . replace ( re , escaped ) ; } ) ; return str ; } catch ( err ) { console . error ( err ) ; return key ; } } return translatedString ; } interface Props { id : string ; variables ? : { [ name ] : ( ) => string | ReactNode ; } ; } export const Trans = ( { id , variables } ) => { const settings = { language : 'en' } ; const language = settings . language || fallbackLanguage ; let tString = ( repository [ language ] && repository [ language ] [ id ] ) || repository [ fallbackLanguage ] [ id ] || id ; const uniqueId = ( ( counter ) => ( ) => `${ ++ counter }` ) ( 0 ) ; if ( ! variables ) { return < > { tString } < />; } else { const keys = Object . keys ( variables ) ; const variablesComponents = [ ] ; /**
         * Replace all variables with tags. I.E. $variable1 with <T1>
         */ keys . forEach ( ( key , index ) => { const tStringSplit = tString . split ( new RegExp ( `\\${ key }` ) ) ; if ( tStringSplit . length ) { tString = tStringSplit . join ( `<T${ index }/>` ) ; const value = variables [ keys [ index ] ] ; variablesComponents . push ( value ) ; } } ) ; const nextTagMatch = new RegExp ( '<T\\d+/>' ) ; param rest param components const resolveComponents = ( rest , components = [ ] ) : ReactNode [ ] => { if ( isEmpty ( rest ) ) { return components ; } const nextMatch = rest . match ( nextTagMatch ) ; if ( nextMatch && nextMatch . length ) { const resolvedComponentIndex = nextMatch [ 0 ] . replace ( '<T' , '' ) . replace ( '/>' , '' ) ; const resolvedComponentIndexNumber = parseInt ( resolvedComponentIndex , 10 ) ; const matchSplit = [ rest . substring ( 0 , nextMatch . index ! ) , rest . substring ( nextMatch . index ! + 4 + resolvedComponentIndex . length ) ] ; return resolveComponents ( matchSplit [ 1 ] , [ ... components , < Fragment key = { uniqueId ( ) } > { matchSplit [ 0 ] } < /Fragment>, < Fragment key = { uniqueId ( ) } > { isFunction ( variablesComponents [ resolvedComponentIndexNumber ] ) ? ( variablesComponents [ resolvedComponentIndexNumber ] as ( ) => any ) ( ) : variablesComponents [ resolvedComponentIndexNumber ] } < /Fragment> ] ) ; } return resolveComponents ( '' , [ ... components , < Fragment key = { uniqueId ( ) } > { rest } < /Fragment>]); } ; return < > { resolveComponents ( tString ) } < />; } } ; /**
 * Wrap string to export it in json language file
 */ export const translateMarker = ( s ) => s ;