mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 17:57:04 +08:00
Some minor JS cleanup (#2846)
- Change index files to ts - Remove deprecated EditUserModal from forum compat - Remove b14-specific error - Remove transChoice method (ICU should be used instead) - Translator to TypeScript - Small JS => TS cleanup * Trivial js => ts change Co-authored-by: David Wheatley <hi@davwheat.dev>
This commit is contained in:
parent
60dea59815
commit
90cfd36e30
|
@ -3,35 +3,46 @@ import { pluralTypeHandler, selectTypeHandler } from '@ultraq/icu-message-format
|
|||
import username from './helpers/username';
|
||||
import extract from './utils/extract';
|
||||
|
||||
export default class Translator {
|
||||
constructor() {
|
||||
/**
|
||||
* A map of translation keys to their translated values.
|
||||
*
|
||||
* @type {Object}
|
||||
* @public
|
||||
*/
|
||||
this.translations = {};
|
||||
type Translations = Record<string, string>;
|
||||
type TranslatorParameters = Record<string, unknown>;
|
||||
|
||||
this.formatter = new RichMessageFormatter(null, this.formatterTypeHandlers(), mithrilRichHandler);
|
||||
export default class Translator {
|
||||
/**
|
||||
* A map of translation keys to their translated values.
|
||||
*/
|
||||
translations: Translations = {};
|
||||
|
||||
/**
|
||||
* The underlying ICU MessageFormatter util.
|
||||
*/
|
||||
protected formatter = new RichMessageFormatter(null, this.formatterTypeHandlers(), mithrilRichHandler);
|
||||
|
||||
setLocale(locale: string) {
|
||||
this.formatter.locale = locale;
|
||||
}
|
||||
|
||||
formatterTypeHandlers() {
|
||||
addTranslations(translations: Translations) {
|
||||
Object.assign(this.translations, translations);
|
||||
}
|
||||
|
||||
/**
|
||||
* An extensible entrypoint for extenders to register type handlers for translations.
|
||||
*/
|
||||
protected formatterTypeHandlers() {
|
||||
return {
|
||||
plural: pluralTypeHandler,
|
||||
select: selectTypeHandler,
|
||||
};
|
||||
}
|
||||
|
||||
setLocale(locale) {
|
||||
this.formatter.locale = locale;
|
||||
}
|
||||
|
||||
addTranslations(translations) {
|
||||
Object.assign(this.translations, translations);
|
||||
}
|
||||
|
||||
preprocessParameters(parameters) {
|
||||
/**
|
||||
* A temporary system to preprocess parameters.
|
||||
* Should not be used by extensions.
|
||||
* TODO: An extender will be added in v1.x.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected preprocessParameters(parameters: TranslatorParameters) {
|
||||
// If we've been given a user model as one of the input parameters, then
|
||||
// we'll extract the username and use that for the translation. In the
|
||||
// future there should be a hook here to inspect the user and change the
|
||||
|
@ -45,21 +56,14 @@ export default class Translator {
|
|||
return parameters;
|
||||
}
|
||||
|
||||
trans(id, parameters) {
|
||||
trans(id: string, parameters: TranslatorParameters = {}) {
|
||||
const translation = this.translations[id];
|
||||
|
||||
if (translation) {
|
||||
parameters = this.preprocessParameters(parameters || {});
|
||||
parameters = this.preprocessParameters(parameters);
|
||||
return this.formatter.rich(translation, parameters);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated, remove before stable
|
||||
*/
|
||||
transChoice(id, number, parameters) {
|
||||
return this.trans(id, parameters);
|
||||
}
|
||||
}
|
|
@ -11,22 +11,12 @@ export default class ModalManagerState {
|
|||
* @public
|
||||
*/
|
||||
show(componentClass, attrs) {
|
||||
// Breaking Change Compliance Warning, Remove in Beta 15.
|
||||
if (!(componentClass.prototype instanceof Modal)) {
|
||||
// This is duplicated so that if the error is caught, an error message still shows up in the debug console.
|
||||
console.error('The ModalManager can only show Modals');
|
||||
throw new Error('The ModalManager can only show Modals');
|
||||
const invalidModalWarning = 'The ModalManager can only show Modals.';
|
||||
console.error(invalidModalWarning);
|
||||
throw new Error(invalidModalWarning);
|
||||
}
|
||||
if (componentClass.init) {
|
||||
// This is duplicated so that if the error is caught, an error message still shows up in the debug console.
|
||||
console.error(
|
||||
'The componentClass parameter must be a modal class, not a modal instance. Whichever extension triggered this modal should be updated to comply with beta 14.'
|
||||
);
|
||||
throw new Error(
|
||||
'The componentClass parameter must be a modal class, not a modal instance. Whichever extension triggered this modal should be updated to comply with beta 14.'
|
||||
);
|
||||
}
|
||||
// End Change Compliance Warning, Remove in Beta 15
|
||||
|
||||
clearTimeout(this.closeTimeout);
|
||||
|
||||
|
|
|
@ -69,10 +69,6 @@ import Search from './components/Search';
|
|||
import DiscussionListItem from './components/DiscussionListItem';
|
||||
import LoadingPost from './components/LoadingPost';
|
||||
import PostsUserPage from './components/PostsUserPage';
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
import EditUserModal from '../common/components/EditUserModal';
|
||||
import DiscussionPageResolver from './resolvers/DiscussionPageResolver';
|
||||
import BasicEditorDriver from '../common/utils/BasicEditorDriver';
|
||||
import routes from './routes';
|
||||
|
@ -131,10 +127,6 @@ export default Object.assign(compat, {
|
|||
'components/EventPost': EventPost,
|
||||
'components/DiscussionHero': DiscussionHero,
|
||||
'components/PostMeta': PostMeta,
|
||||
/**
|
||||
* @deprecated Used for backwards compatibility now that the EditUserModal has moved to common. Remove in beta 17.
|
||||
*/
|
||||
'components/EditUserModal': EditUserModal,
|
||||
'components/SearchSource': SearchSource,
|
||||
'components/DiscussionRenamedPost': DiscussionRenamedPost,
|
||||
'components/DiscussionComposer': DiscussionComposer,
|
||||
|
|
|
@ -6,10 +6,6 @@ import app from './app';
|
|||
|
||||
export { app };
|
||||
|
||||
// Export public API
|
||||
// export { default as Extend } from './Extend';
|
||||
// export { IndexPage, DicsussionList } from './components';
|
||||
|
||||
// Export compat API
|
||||
import compatObj from './compat';
|
||||
import proxifyCompat from '../common/utils/proxifyCompat';
|
Loading…
Reference in New Issue
Block a user