Skip to content

Commit

Permalink
Enable overriding of language in client twinkle
Browse files Browse the repository at this point in the history
Per #4 (comment).

language is now part of `Twinkle` object (namespace).
  • Loading branch information
siddharthvp committed Oct 15, 2021
1 parent aa0bc17 commit e78e9ee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
13 changes: 6 additions & 7 deletions src/Api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Twinkle } from './twinkle';
import { language } from './messenger';

/**
* Wrapper around Morebits.wiki.api that preset the commonly used
Expand All @@ -12,8 +11,8 @@ export class Api extends Morebits.wiki.api {
action: 'query',
format: 'json',
formatversion: '2',
uselang: language,
errorlang: language,
uselang: Twinkle.language,
errorlang: Twinkle.language,
errorsuselocal: true,
// tags isn't applicable for all API actions, it gives a warning but that's harmless
tags: Twinkle.changeTags,
Expand All @@ -38,8 +37,8 @@ export class Api extends Morebits.wiki.api {
export let mwApi: mw.Api;

/**
* Called from init(). Can't initialise at top level, since values of language,
* Twinkle.changeTags and Twinkle.userAgent aren't final by that stage.
* Called from init(). Can't initialise at top level, since values of
* Twinkle.* aren't final by that stage.
* @private
*/
export function initialiseMwApi() {
Expand All @@ -48,8 +47,8 @@ export function initialiseMwApi() {
action: 'query',
format: 'json',
formatversion: '2',
uselang: language,
errorlang: language,
uselang: Twinkle.language,
errorlang: Twinkle.language,
errorsuselocal: true,
// tags isn't applicable for all API actions, it gives a warning but that's harmless
tags: Twinkle.changeTags,
Expand Down
20 changes: 9 additions & 11 deletions src/messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ import enMessages from '../i18n/en.json';
export let banana: Banana;

/**
* The language used for all messages in twinkle-core.
* This includes interface messages as well as edits made to the wiki.
* Ideally the interface messages should have been in wgUserLanguage
* and edits in wgContentLanguage, but this duality is not presently
* supported.
* @deprecated - use {@link Twinkle.language} instead.
*/
// I believe this is the only use of mw.* at the top level
export let language = urlParamValue('uselang') || mw.config.get('wgContentLanguage');

let qqxMode: boolean;
Expand Down Expand Up @@ -94,7 +89,7 @@ const i18nParserPlugins = {
* @param messages
*/
export function addMessages(messages: Messages) {
banana.load(messages, language);
banana.load(messages, Twinkle.language);
}

/**
Expand Down Expand Up @@ -124,7 +119,7 @@ let EnglishMessagesAvailable = typeof EXCLUDE_ENGLISH_MESSAGES === 'undefined' |
* Initialize the message store. Called from init.ts.
*/
export function initMessaging() {
banana = new Banana(language);
banana = new Banana(Twinkle.language);

// Register plugins
obj_entries(i18nParserPlugins).forEach(([name, plugin]) => {
Expand All @@ -136,7 +131,7 @@ export function initMessaging() {

// QQX is a dummy "language" for documenting messages
// No need to load anything when in qqxMode
qqxMode = language === 'qqx';
qqxMode = Twinkle.language === 'qqx';

if (qqxMode) {
return Promise.resolve();
Expand All @@ -146,7 +141,10 @@ export function initMessaging() {
banana.load(enMessages, 'en');
}
const mwMessageList = coreMwMessages.concat(Twinkle.extraMwMessages);
return Promise.all([loadMediaWikiMessages(mwMessageList, language), loadTwinkleCoreMessages(language)])
return Promise.all([
loadMediaWikiMessages(mwMessageList, Twinkle.language),
loadTwinkleCoreMessages(Twinkle.language),
])
.catch((e) => {
mw.notify('Failed to load messages needed for Twinkle', { type: 'error' });
})
Expand Down Expand Up @@ -260,5 +258,5 @@ function initBanana(json) {
* @deprecated
*/
export function loadAdditionalMediaWikiMessages(messageList: string[]) {
return loadMediaWikiMessages(messageList, language);
return loadMediaWikiMessages(messageList, Twinkle.language);
}
10 changes: 10 additions & 0 deletions src/twinkle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TwinkleModule } from './twinkleModule';
import { urlParamValue } from './utils';

/**
* Defined as a namespace: anything that's exported from here (such as
Expand All @@ -12,6 +13,15 @@ export namespace Twinkle {
*/
export let scriptName = 'Twinkle';

/**
* The language used for all messages in twinkle-core.
* This includes interface messages as well as edits made to the wiki.
* Ideally the interface messages should have been in wgUserLanguage
* and edits in wgContentLanguage, but this duality is not presently
* supported.
*/
export let language = urlParamValue('uselang') || mw.config.get('wgContentLanguage');

/**
* User agent
* @see https://meta.wikimedia.org/wiki/User-Agent_policy
Expand Down

0 comments on commit e78e9ee

Please sign in to comment.