-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract i18n to class module
- Loading branch information
Showing
10 changed files
with
90 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { FluentEditor } from '../fluent-editor' | ||
import { CHANGE_LANGUAGE_EVENT, defaultLanguage, LANG_CONF } from '../config' | ||
import { isUndefined } from '../utils/is' | ||
|
||
export interface I18NOptions { | ||
lang: string | ||
langText: Record<string, string> | ||
} | ||
|
||
export class I18N { | ||
isFullscreen: boolean = false | ||
options: I18NOptions = { | ||
lang: 'en-US', | ||
langText: LANG_CONF['en-US'], | ||
} | ||
|
||
constructor(public quill: FluentEditor, options: Partial<I18NOptions>) { | ||
this.options = Object.assign({}, options, this.resolveLanguageOption(options || {})) | ||
this.changeLanguage(this.options) | ||
} | ||
|
||
resolveLanguageOption(options: Partial<I18NOptions>): I18NOptions { | ||
if (isUndefined(options.lang)) { | ||
options.lang = defaultLanguage | ||
} | ||
if (!(options.lang in LANG_CONF)) { | ||
console.warn(`The language ${options.lang} is not supported. Use the default language: ${defaultLanguage}`) | ||
options.lang = defaultLanguage | ||
} | ||
return { | ||
lang: options.lang, | ||
langText: Object.assign({}, LANG_CONF[options.lang], options.langText || {}), | ||
} | ||
} | ||
|
||
setLangTextToInstance() { | ||
this.quill.langText = this.options.langText | ||
this.quill.lang = this.options.lang | ||
} | ||
|
||
changeLanguage(options: Partial<I18NOptions>) { | ||
const langOps = this.resolveLanguageOption(options) | ||
if (langOps.lang === this.quill.lang) return | ||
this.options.lang = langOps.lang | ||
this.options.langText = langOps.langText | ||
this.setLangTextToInstance() | ||
this.quill.emitter.emit(CHANGE_LANGUAGE_EVENT, this.options.lang, this.options.langText) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters