@ -1,3 +1,4 @@
@@ -1,3 +1,4 @@
import { MOUNT_CLASS_TO } from "../config/debug" ;
import { LangPackString } from "../layer" ;
import apiManager from "./mtproto/mtprotoworker" ;
@ -31,8 +32,26 @@ export const langPack: {[actionType: string]: string} = {
@@ -31,8 +32,26 @@ export const langPack: {[actionType: string]: string} = {
"messageActionBotAllowed" : "You allowed this bot to message you when logged in {}"
} ;
export namespace Internationalization {
let strings : { [ key : string ] : LangPackString } = { } ;
namespace Strings {
export type Bio = 'Bio.Description' ;
export type LoginRegister = 'Login.Register.FirstName.Placeholder' | 'Login.Register.LastName.Placeholder' ;
export type EditAccount = 'EditAccount.Logout' | 'EditAccount.Title' | 'EditAccount.Title' | 'EditAccount.Username' ;
export type AccountSettings = 'AccountSettings.Filters' | 'AccountSettings.Notifications' | 'AccountSettings.PrivacyAndSecurity' | 'AccountSettings.Language' | 'AccountSettings.Bio' ;
export type Telegram = 'Telegram.GeneralSettingsViewController' ;
export type ChatFilters = 'ChatList.Filter.Header' | 'ChatList.Filter.NewTitle' | 'ChatList.Filter.List.Header' | 'ChatList.Filter.Recommended.Header' | 'ChatList.Filter.Recommended.Add' | 'ChatList.Filter.List.Title' ;
export type LangPackKey = AccountSettings | EditAccount | Telegram | ChatFilters | LoginRegister | Bio | string ;
}
export type LangPackKey = Strings . LangPackKey ;
namespace I18n {
let strings : Partial < { [ key in LangPackKey ] : LangPackString } > = { } ;
export function getLangPack ( langCode : string ) {
return apiManager . invokeApi ( 'langpack.getLangPack' , {
@ -41,14 +60,86 @@ export namespace Internationalization {
@@ -41,14 +60,86 @@ export namespace Internationalization {
} ) . then ( langPack = > {
strings = { } ;
for ( const string of langPack . strings ) {
strings [ string . key ] = string ;
strings [ string . key as LangPackKey ] = string ;
}
const elements = Array . from ( document . querySelectorAll ( ` .i18n ` ) ) as HTMLElement [ ] ;
elements . forEach ( element = > {
const instance = weakMap . get ( element ) ;
if ( instance ) {
instance . update ( ) ;
}
} ) ;
} ) ;
}
export function _ ( key : keyof typeof strings , . . . args : any [ ] ) {
let str = strings [ key ] ;
export function getString ( key : LangPackKey , args? : any [ ] ) {
const str = strings [ key ] ;
let out = '' ;
if ( str ) {
if ( str . _ === 'langPackStringPluralized' ) {
out = str . one_value ;
} else if ( str . _ === 'langPackString' ) {
out = str . value ;
} else {
out = '[' + key + ']' ;
}
} else {
out = '[' + key + ']' ;
}
return out ;
}
const weakMap : WeakMap < HTMLElement , IntlElement > = new WeakMap ( ) ;
export type IntlElementOptions = {
element? : HTMLElement ,
property ? : 'innerHTML' | 'placeholder'
key : LangPackKey ,
args? : any [ ]
} ;
export class IntlElement {
public element : IntlElementOptions [ 'element' ] ;
public key : IntlElementOptions [ 'key' ] ;
public args : IntlElementOptions [ 'args' ] ;
public property : IntlElementOptions [ 'property' ] = 'innerHTML' ;
constructor ( options : IntlElementOptions ) {
this . element = options . element || document . createElement ( 'span' ) ;
this . element . classList . add ( 'i18n' ) ;
this . update ( options ) ;
weakMap . set ( this . element , this ) ;
}
public update ( options? : IntlElementOptions ) {
if ( options ) {
Object . assign ( this , options ) ;
}
( this . element as any ) [ this . property ] = getString ( this . key , this . args ) ;
}
}
export function i18n ( key : LangPackKey , args? : any [ ] ) {
return new IntlElement ( { key , args } ) . element ;
}
return str ;
export function i18n_ ( options : IntlElementOptions ) {
return new IntlElement ( options ) . element ;
}
}
export { I18n } ;
export default I18n ;
const i18n = I18n . i18n ;
export { i18n } ;
const i18n_ = I18n . i18n_ ;
export { i18n_ } ;
MOUNT_CLASS_TO && ( MOUNT_CLASS_TO . I18n = I18n ) ;