import { EventApi } from "../event/EventApi" ; import { ENABLE_CHANGE_LANGUAGE , USING_LAN_KEY } from "./Defines" ; import { Manager } from "../Framework" ; const LANG_KEY = "using_language" ; export interface LanguageData { language : string ; } export interface LanguageDelegate { data ( language ) : LanguageData ; } export class Language { private static _instance : Language = null ; public static Instance ( ) { return this . _instance || ( this . _instance = new Language ( ) ) ; } private _data : LanguageData = { language : "unknown" } ; private _delegate : LanguageDelegate ; public set delegate ( value ) { this . _delegate = value ; this . change ( this . getLanguage ( ) ) ; } public get delegate ( ) { return this . _delegate ; } description param language public change ( language ) { if ( ! this . delegate ) { return ; } if ( this . _data && this . _data . language == language ) { return ; } if ( ENABLE_CHANGE_LANGUAGE ) { this . _data = this . delegate . data ( language ) ; dispatch ( EventApi . CHANGE_LANGUAGE , language ) ; } else { this . _data = this . delegate . data ( this . getLanguage ( ) ) ; } Manager . localStorage . setItem ( LANG_KEY , this . _data . language ) ; } public get ( args ) { let result = "" ; do { if ( ! ! ! args ) break ; if ( args . length < 1 ) break ; let keyString = args [ 0 ] ; if ( typeof keyString != "string" ) { cc . error ( "key error" ) ; break ; } if ( keyString . indexOf ( USING_LAN_KEY ) > - 1 ) { let keys = keyString . split ( "." ) ; if ( keys . length < 2 ) { cc . error ( "key error" ) ; break ; } keys . shift ( ) ; args . shift ( ) ; result = this . _data [ keys [ 0 ] ] ; if ( ! result ) { cc . error ( `语言包不存在 : ${ keyString }` ) ; break ; } let i = 1 ; for ( ; i < keys . length ; i ++ ) { if ( result [ keys [ i ] ] == undefined ) { break ; } result = result [ keys [ i ] ] ; } if ( i != keys . length ) { cc . error ( `语言包不存在 : ${ keyString }` ) ; } result = String . format ( result , args ) ; } else { keyString = args . shift ( ) ; return String . format ( keyString , args ) ; } } while ( 0 ) ; return result ; } description public getLanguage ( ) { return Manager . localStorage . getItem ( LANG_KEY , cc . sys . LANGUAGE_CHINESE ) ; } }