import { OptionManager } from './OptionManager' ; import { ErrorMessage } from './ErrorMessage' ; /**
 * This class enables to translate Tableaunoir in other languages (french for instance)
 */ export class Translation { private static readonly englishDict = { } ; /**
     * initialization
     */ static init ( ) { try { setTimeout ( ( ) => OptionManager . string ( { name : "language" , defaultValue : "en" , onChange : ( s ) => { Translation . translate ( s ) ; } } ) , 1000 ) ; } catch ( e ) { ErrorMessage . show ( e ) ; } } returns static fetchDictionary ( language ) { if ( language == null || language == "en" ) return new Promise ( ( ) => { } ) ; return fetch ( language + ".json" ) . then ( txt => txt . json ( ) ) ; } param element param dict description static translateElement ( element , dict ) { if ( element . children == undefined ) return ; if ( ( < HTMLElement > element ) . title != undefined ) { if ( dict [ ( < HTMLElement > element ) . title ] ) { Translation . setEnglishDictEntry ( dict [ ( < HTMLElement > element ) . title ] , ( < HTMLElement > element ) . title ) ; ( < HTMLElement > element ) . title = dict [ ( < HTMLElement > element ) . title ] ; } } if ( element . children . length == 0 ) { if ( dict [ element . innerHTML ] ) { Translation . setEnglishDictEntry ( dict [ element . innerHTML ] , element . innerHTML ) ; element . innerHTML = dict [ element . innerHTML ] ; } } else { for ( const i in element . children ) Translation . translateElement ( element . children [ i ] , dict ) ; } } param key param text description static setEnglishDictEntry ( key , text ) { if ( Translation . englishDict [ key ] == undefined ) Translation . englishDict [ key ] = text ; } param dict description static translateFromIDs ( dict ) { for ( const key in dict ) { if ( key . startsWith ( '#' ) ) { const element = document . getElementById ( key . substr ( 1 ) ) ; if ( element == undefined ) console . log ( `Element ${ key } not found. I cannot translate..` ) ; else { if ( element . children . length > 0 ) console . log ( `I refuse to translate because element ${ key }  has some children.` ) ; Translation . setEnglishDictEntry ( key , element . innerHTML ) ; element . innerHTML = dict [ key ] ; } } } } param dict description static translateFromIDsTitle ( dict ) { for ( const key in dict ) { if ( key . startsWith ( '#' ) && key . endsWith ( ".title" ) ) { const id = key . substr ( 1 , key . length - 7 ) ; const element = document . getElementById ( id ) ; if ( element == undefined ) console . log ( `Element of id ${ id } not found. I cannot translate..` ) ; else { Translation . setEnglishDictEntry ( key , element . title ) ; element . title = dict [ key ] ; } } } } param dict static translateD ( dict ) { Translation . translateElement ( document . getElementById ( "controls" ) , dict ) ; Translation . translateElement ( document . getElementById ( "menu" ) , dict ) ; Translation . translateFromIDs ( dict ) ; Translation . translateFromIDsTitle ( dict ) ; } /**
     * big translation procedure
     */ static translate ( language ) { Translation . translateD ( Translation . englishDict ) ; if ( language != "en" ) Translation . fetchDictionary ( language ) . then ( Translation . translateD ) ; } }