framework/js/lib/utils/app.js
Toby Zerner 8b162344cd Lay the groundwork for translation & refactor asset compilation
Ditched the idea of having language packs as extensions. Reasoning:

1. Because we use machine keys for translations (rather than English
keys), extensions need to be able to define default translations. If
English translations are to be included in extensions and not in a
language pack extension, then it doesn’t make sense to have other
languages as language pack extensions. Inconsistency → complexity.

2. Translations should maintain version parity with their respective
extensions. There’s no way to do this if extension translations are
external to the extension.

Instead, localisation will be a core effort, as well as a per-extension
effort. Translators will be encouraged to send PRs to core + extensions.

In core, each locale has a directory containing three files:
- translations.yml
- config.js: contains pluralisation logic for the JS app, as well as
moment.js localisation if necessary
- config.php: contains pluralisation logic for the PHP app

Extensions can use the Flarum\Extend\Locale extender to add/override
translations/config to a locale.

Asset compilation has been completely refactored with a better
architecture. Translations + config.js are compiled and cached for the
currently active locale.
2015-06-10 14:23:56 +09:30

67 lines
1.8 KiB
JavaScript

import ItemList from 'flarum/utils/item-list';
import Alert from 'flarum/components/alert';
import ServerError from 'flarum/utils/server-error';
import Translator from 'flarum/utils/translator';
class App {
constructor() {
this.initializers = new ItemList();
this.translator = new Translator();
this.cache = {};
this.serverError = null;
}
boot() {
this.initializers.toArray().forEach((initializer) => initializer(this));
}
setTitle(title) {
document.title = (title ? title+' - ' : '')+this.config['forum_title'];
}
request(options) {
var extract = options.extract;
options.extract = function(xhr, xhrOptions) {
if (xhr.status === 500) {
throw new ServerError;
}
return extract ? extract(xhr.responseText) : (xhr.responseText.length === 0 ? null : xhr.responseText);
};
return m.request(options).then(response => {
this.alerts.dismiss(this.serverError);
return response;
}, response => {
this.alerts.dismiss(this.serverError);
if (response instanceof ServerError) {
this.alerts.show(this.serverError = new Alert({ type: 'warning', message: 'Oops! Something went wrong on the server. Please try again.' }))
}
throw response;
});
}
handleApiErrors(response) {
this.alerts.clear();
response.errors.forEach(error =>
this.alerts.show(new Alert({ type: 'warning', message: error.detail }))
);
}
route(name, params) {
var url = this.routes[name][0].replace(/:([^\/]+)/g, function(m, t) {
var value = params[t];
delete params[t];
return value;
});
var queryString = m.route.buildQueryString(params);
return url+(queryString ? '?'+queryString : '');
}
translate(key, input) {
return this.translator.translate(key, input);
}
}
export default App;