From e78e9eef0a6e1bfcd238896de72f1b0f49b2be04 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Fri, 15 Oct 2021 09:49:37 +0530 Subject: [PATCH] Enable overriding of language in client twinkle Per https://github.com/wikimedia-gadgets/twinkle-core/issues/4#issuecomment-943449831. language is now part of `Twinkle` object (namespace). --- src/Api.ts | 13 ++++++------- src/messenger.ts | 20 +++++++++----------- src/twinkle.ts | 10 ++++++++++ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Api.ts b/src/Api.ts index 5e23e4e..0b023a8 100644 --- a/src/Api.ts +++ b/src/Api.ts @@ -1,5 +1,4 @@ import { Twinkle } from './twinkle'; -import { language } from './messenger'; /** * Wrapper around Morebits.wiki.api that preset the commonly used @@ -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, @@ -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() { @@ -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, diff --git a/src/messenger.ts b/src/messenger.ts index ecbb96f..de2a853 100644 --- a/src/messenger.ts +++ b/src/messenger.ts @@ -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; @@ -94,7 +89,7 @@ const i18nParserPlugins = { * @param messages */ export function addMessages(messages: Messages) { - banana.load(messages, language); + banana.load(messages, Twinkle.language); } /** @@ -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]) => { @@ -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(); @@ -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' }); }) @@ -260,5 +258,5 @@ function initBanana(json) { * @deprecated */ export function loadAdditionalMediaWikiMessages(messageList: string[]) { - return loadMediaWikiMessages(messageList, language); + return loadMediaWikiMessages(messageList, Twinkle.language); } diff --git a/src/twinkle.ts b/src/twinkle.ts index 57d780a..b926e71 100644 --- a/src/twinkle.ts +++ b/src/twinkle.ts @@ -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 @@ -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