MediaWiki:Common.js: Difference between revisions

From koreapedia
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
/* Google Translate widget (shows English via pageLanguage:auto) */
/* Google Translate widget for Chameleon skin (selected languages only) */
 
(function () {
(function () {
   var LANGS = 'en,ko,ja,es,de,zh-CN,zh-TW,fr,ar,hi,pt,ru';
   var LANGS = 'ko,ja,es,de,zh-CN,zh-TW,fr,ar,hi,pt,ru'; // note: no 'en' (it's pageLanguage)
   var CONTAINER_ID = 'google_translate_element';
   var CONTAINER_ID = 'google_translate_element';


  // Ensure container exists before init
   function ensureContainer() {
   function ensureContainer() {
     if (!document.getElementById(CONTAINER_ID)) {
     if (!document.getElementById(CONTAINER_ID)) {
Line 15: Line 17:
   }
   }


   // New callback name to avoid stale inits
   // NEW callback name to bust any stale init
   window.koreapediaGTEInit = function () {
   window.koreapediaGTEInit = function () {
     ensureContainer();
     ensureContainer();
     new google.translate.TranslateElement({
     new google.translate.TranslateElement({
       pageLanguage: 'auto', // 👈 allows English to appear
       pageLanguage: 'en',
       includedLanguages: LANGS,
       includedLanguages: LANGS,
       layout: google.translate.TranslateElement.InlineLayout.SIMPLE
       layout: google.translate.TranslateElement.InlineLayout.SIMPLE
Line 25: Line 27:
   };
   };


  // Load script only if needed; otherwise call init directly
   function loadOrInit() {
   function loadOrInit() {
    // If library is already there, just init with our options
     if (window.google && google.translate && google.translate.TranslateElement) {
     if (window.google && google.translate && google.translate.TranslateElement) {
       window.koreapediaGTEInit();
       window.koreapediaGTEInit();
       return;
       return;
     }
     }
    // Avoid adding duplicate script tags
     if (!document.querySelector('script[src*="translate_a/element.js"]')) {
     if (!document.querySelector('script[src*="translate_a/element.js"]')) {
       var s = document.createElement('script');
       var s = document.createElement('script');

Revision as of 15:56, 15 October 2025

/* Any JavaScript here will be loaded for all users on every page load. */
/* Google Translate widget for Chameleon skin (selected languages only) */

(function () {
  var LANGS = 'ko,ja,es,de,zh-CN,zh-TW,fr,ar,hi,pt,ru'; // note: no 'en' (it's pageLanguage)
  var CONTAINER_ID = 'google_translate_element';

  // Ensure container exists before init
  function ensureContainer() {
    if (!document.getElementById(CONTAINER_ID)) {
      var host = document.querySelector('.navbar-collapse') || document.body;
      var el = document.createElement('div');
      el.id = CONTAINER_ID;
      el.style.marginLeft = '15px';
      host.appendChild(el);
    }
  }

  // NEW callback name to bust any stale init
  window.koreapediaGTEInit = function () {
    ensureContainer();
    new google.translate.TranslateElement({
      pageLanguage: 'en',
      includedLanguages: LANGS,
      layout: google.translate.TranslateElement.InlineLayout.SIMPLE
    }, CONTAINER_ID);
  };

  // Load script only if needed; otherwise call init directly
  function loadOrInit() {
    // If library is already there, just init with our options
    if (window.google && google.translate && google.translate.TranslateElement) {
      window.koreapediaGTEInit();
      return;
    }
    // Avoid adding duplicate script tags
    if (!document.querySelector('script[src*="translate_a/element.js"]')) {
      var s = document.createElement('script');
      s.src = '//translate.google.com/translate_a/element.js?cb=koreapediaGTEInit';
      document.head.appendChild(s);
    }
  }

  mw.loader.using('mediawiki.util', function () {
    $(function () {
      ensureContainer();
      loadOrInit();
    });
  });
})();