「MediaWiki:Common.js」の版間の差分

提供:svg2wiki
ナビゲーションに移動 検索に移動
 
 
(同じ利用者による、間の4版が非表示)
1行目: 1行目:
 
/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */
 
/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */
  
−
/* Google 翻訳ウィジェットをサイドバーに追加 */
+
/* Google 翻訳ウィジェット(自動言語選択 + 原文明示バナー完全版) */
 
$( function () {
 
$( function () {
 
     if ( $( '#p-googletranslator' ).length ) {
 
     if ( $( '#p-googletranslator' ).length ) {
7行目: 7行目:
 
     }
 
     }
  
−
     // サイドバー用ブロックのHTML
+
    var supportedLangs = ['ja', 'ko', 'en', 'nl', 'fr', 'de', 'zh-CN', 'ru'];
 +
 
 +
     // 1. 記事本文の上に通知バナーを配置
 +
    if ( $( '#mw-content-text' ).length && !$( '.mw-translate-notice' ).length ) {
 +
        var $notice = $( '<div class="mw-translate-notice">⚠️ このページは、日本語の原文から機械翻訳(Google 翻訳)されたものです。</div>' );
 +
        $( '#mw-content-text' ).prepend( $notice );
 +
    }
 +
 
 +
    // 2. サイドバー用ブロックのHTML生成
 
     var boxHtml =  
 
     var boxHtml =  
 
         '<nav id="p-googletranslator" class="mw-portlet mw-portlet-googletranslator vector-menu vector-menu-portal portal" role="navigation">' +
 
         '<nav id="p-googletranslator" class="mw-portlet mw-portlet-googletranslator vector-menu vector-menu-portal portal" role="navigation">' +
18行目: 26行目:
 
         '</nav>';
 
         '</nav>';
  
−
    // サイドバーの末尾(またはツールボックスの後ろ)へ挿入
 
 
     if ( $( '#p-tb' ).length ) {
 
     if ( $( '#p-tb' ).length ) {
 
         $( '#p-tb' ).after( boxHtml );
 
         $( '#p-tb' ).after( boxHtml );
25行目: 32行目:
 
     }
 
     }
  
−
     // Google 翻訳のコールバックとスクリプト読み込み
+
     // 3. ブラウザ言語に応じた自動翻訳の発火処理
 +
    function triggerAutoTranslation() {
 +
        if ( sessionStorage.getItem( 'gt_auto_applied' ) || ( document.cookie.indexOf( 'googtrans=' ) !== -1 && document.cookie.indexOf( 'googtrans=/ja/ja' ) === -1 ) ) {
 +
            return;
 +
        }
 +
 
 +
        var userLang = ( navigator.language || navigator.userLanguage || '' ).toLowerCase();
 +
        var targetLang = '';
 +
 
 +
        if ( userLang.indexOf( 'zh' ) === 0 ) {
 +
            targetLang = 'zh-CN';
 +
        } else {
 +
            var shortLang = userLang.split( '-' )[0];
 +
            if ( shortLang !== 'ja' && supportedLangs.indexOf( shortLang ) !== -1 ) {
 +
                targetLang = shortLang;
 +
            }
 +
        }
 +
 
 +
        if ( !targetLang ) {
 +
            return;
 +
        }
 +
 
 +
        // セレクトボックスの生成とオプションの準備を監視
 +
        var checkCount = 0;
 +
        var timer = setInterval( function () {
 +
            checkCount++;
 +
            var select = document.querySelector( '.goog-te-combo' );
 +
 
 +
            if ( select && select.options && select.options.length > 1 ) {
 +
                clearInterval( timer );
 +
                sessionStorage.setItem( 'gt_auto_applied', 'true' );
 +
                select.value = targetLang;
 +
                select.dispatchEvent( new Event( 'change' ) );
 +
            }
 +
 
 +
            if ( checkCount > 60 ) {
 +
                clearInterval( timer );
 +
            }
 +
        }, 100 );
 +
    }
 +
 
 +
    // 4. Google 翻訳の初期化(標準セレクトボックス形式で生成)
 
     window.googleTranslateElementInit = function () {
 
     window.googleTranslateElementInit = function () {
 
         new google.translate.TranslateElement({
 
         new google.translate.TranslateElement({
 
             pageLanguage: 'ja',
 
             pageLanguage: 'ja',
−
             includedLanguages: 'ja,ko,en,nl,fr,de,zh-CN,ru',
+
             includedLanguages: supportedLangs.join( ',' )
−
            layout: google.translate.TranslateElement.InlineLayout.SIMPLE
 
 
         }, 'google_translate_element');
 
         }, 'google_translate_element');
 +
 +
        triggerAutoTranslation();
 
     };
 
     };
  
 
     mw.loader.load( '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit' );
 
     mw.loader.load( '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit' );
 
} );
 
} );

2026年10月23日 (金) 09:20時点における最新版

/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */

/* Google 翻訳ウィジェット(自動言語選択 + 原文明示バナー完全版) */
$( function () {
    if ( $( '#p-googletranslator' ).length ) {
        return;
    }

    var supportedLangs = ['ja', 'ko', 'en', 'nl', 'fr', 'de', 'zh-CN', 'ru'];

    // 1. 記事本文の上に通知バナーを配置
    if ( $( '#mw-content-text' ).length && !$( '.mw-translate-notice' ).length ) {
        var $notice = $( '<div class="mw-translate-notice">⚠️ このページは、日本語の原文から機械翻訳(Google 翻訳)されたものです。</div>' );
        $( '#mw-content-text' ).prepend( $notice );
    }

    // 2. サイドバー用ブロックのHTML生成
    var boxHtml = 
        '<nav id="p-googletranslator" class="mw-portlet mw-portlet-googletranslator vector-menu vector-menu-portal portal" role="navigation">' +
        '  <h3 class="vector-menu-heading"><span>翻訳 / Translate</span></h3>' +
        '  <div class="vector-menu-content body">' +
        '    <ul class="vector-menu-content-list" style="padding-top: 8px;">' +
        '      <li style="list-style: none;"><div id="google_translate_element"></div></li>' +
        '    </ul>' +
        '  </div>' +
        '</nav>';

    if ( $( '#p-tb' ).length ) {
        $( '#p-tb' ).after( boxHtml );
    } else {
        $( '#mw-panel' ).append( boxHtml );
    }

    // 3. ブラウザ言語に応じた自動翻訳の発火処理
    function triggerAutoTranslation() {
        if ( sessionStorage.getItem( 'gt_auto_applied' ) || ( document.cookie.indexOf( 'googtrans=' ) !== -1 && document.cookie.indexOf( 'googtrans=/ja/ja' ) === -1 ) ) {
            return;
        }

        var userLang = ( navigator.language || navigator.userLanguage || '' ).toLowerCase();
        var targetLang = '';

        if ( userLang.indexOf( 'zh' ) === 0 ) {
            targetLang = 'zh-CN';
        } else {
            var shortLang = userLang.split( '-' )[0];
            if ( shortLang !== 'ja' && supportedLangs.indexOf( shortLang ) !== -1 ) {
                targetLang = shortLang;
            }
        }

        if ( !targetLang ) {
            return;
        }

        // セレクトボックスの生成とオプションの準備を監視
        var checkCount = 0;
        var timer = setInterval( function () {
            checkCount++;
            var select = document.querySelector( '.goog-te-combo' );

            if ( select && select.options && select.options.length > 1 ) {
                clearInterval( timer );
                sessionStorage.setItem( 'gt_auto_applied', 'true' );
                select.value = targetLang;
                select.dispatchEvent( new Event( 'change' ) );
            }

            if ( checkCount > 60 ) {
                clearInterval( timer );
            }
        }, 100 );
    }

    // 4. Google 翻訳の初期化(標準セレクトボックス形式で生成)
    window.googleTranslateElementInit = function () {
        new google.translate.TranslateElement({
            pageLanguage: 'ja',
            includedLanguages: supportedLangs.join( ',' )
        }, 'google_translate_element');

        triggerAutoTranslation();
    };

    mw.loader.load( '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit' );
} );