Skip to content

Full list of fallbacks from MediaWiki message*.php#94

Open
In1quity wants to merge 2 commits intojwbth:mainfrom
In1quity:patch-10
Open

Full list of fallbacks from MediaWiki message*.php#94
In1quity wants to merge 2 commits intojwbth:mainfrom
In1quity:patch-10

Conversation

@In1quity
Copy link
Copy Markdown
Contributor

No description provided.

@In1quity In1quity changed the title Full list of callbacks from MediaWiki message*.php Full list of fallbacks from MediaWiki message*.php Jul 17, 2025
@jwbth
Copy link
Copy Markdown
Owner

jwbth commented Jul 18, 2025

Cool. What's the best way to update it when an update is needed next time?

@In1quity
Copy link
Copy Markdown
Contributor Author

In1quity commented Jul 18, 2025

I haven't really figured out how to do this. I think I'll just manually adjust the list from time to time, or use something like this (but this script forgets the quotes.):

const fs = require('fs');
const path = require('path');

const messagesDir = path.join(__dirname, 'messages'); //https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/HEAD/languages/messages
const outputFile = path.join(__dirname, 'data/languageFallbacks.mediawiki.json');
const output = {};

const fallbackRegex = /\$fallback\s*=\s*([^;]+);/;

fs.readdirSync(messagesDir).forEach(file => {
  if (!file.startsWith('Messages') || !file.endsWith('.php')) return;
  const code = file
    .replace(/^Messages/, '')
    .replace(/_/g, '-')
    .replace(/\.php$/, '')
    .toLowerCase();

  const content = fs.readFileSync(path.join(messagesDir, file), 'utf8');
  const match = content.match(fallbackRegex);

  if (match) {
    let value = match[1].trim();
    if (value.startsWith('[')) {
      // Array fallback: [ 'skr', 'ur' ]
      value = value
        .replace(/\[|\]|'|\s/g, '')
        .split(',')
        .filter(Boolean);
    } else if (value.startsWith("'")) {
      // Single fallback: 'ru'
      value = [value.replace(/'/g, '')];
    } else if (value.includes(',')) {
      // Comma-separated string: 'skr, ur, en'. NEEDTOFIX: forgot quotes
      value = value
        .replace(/'/g, '')
        .split(',')
        .map(s => s.trim())
        .filter(Boolean);
    } else {
      value = [];
    }
    output[code] = value;
  } else {
    output[code] = [];
  }
});

if (!fs.existsSync(path.join(__dirname, 'data'))) {
  fs.mkdirSync(path.join(__dirname, 'data'));
}

fs.writeFileSync(outputFile, JSON.stringify(output, null, 2), 'utf8');
console.log('languageFallbacks.mediawiki.json generated!');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants