মিডিয়াউইকি:Gadget-LanguageUtils.js

উইকিঅভিধান, মুক্ত অভিধান থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
// {{documentation}}
// implicit dependencies : ext.gadget.StorageUtils
/* jshint maxerr:1048576, strict:true, undef:true, latedef:true, es5:true, sub:true */

/* global mw, $ */

// <nowiki>

// usage:
// var lutils = new LanguageUtilsAsync();
// lutils.GetWiktionaryCodeByCanonicalName("Georgian").then(langcode => console.log(langcode));
// lutils.GetCanonicalNameByWiktionaryCode("ka").then(langname => console.log(langname));

window.LanguageUtilsAsync = function(){
	this.langNameToLangCode = new mw.Api().get({
		"action": "expandtemplates",
		"format": "json",
		"text": "{{#invoke:languages/javascript-interface|AllCanonicalToCode}}",
		"prop": "wikitext"
	}).then(function (response) {
		return JSON.parse(response.expandtemplates.wikitext);
	});

	this.langCodeToLangName = this.langNameToLangCode.then(function(name2code){
		var code2name = {};
		for (var name in name2code) 
		{
			code2name[name2code[name]] = name;
		}
		return code2name;
	});
	
	
	this.GetWiktionaryCodeByCanonicalName = function(canonicalName){
		return this.langNameToLangCode.then(function(r){ return r[canonicalName];});
	};
	this.GetCanonicalNameByWiktionaryCode = function(langcode) {
		return this.langCodeToLangName.then(function(r){ return r[langcode];});
	};
};


window.LanguageUtils = function(){
	this.automaticTranslitLanguages = function(){
		return new mw.Api().get({
			"action": "expandtemplates",
			"format": "json",
			"text": "{{#invoke:languages/javascript-interface|GetLanguagesWithAutomaticTransliteration}}",
			"prop": "wikitext"
		}).then(function (response) {
			return JSON.parse(response.expandtemplates.wikitext);
		});
	};
	this.wiktionaryCodeToWikimediaCode = function(){
		return new mw.Api().get({
			"action": "expandtemplates",
			"format": "json",
			"text": "{{#invoke:languages/javascript-interface|AllWiktionaryCodeToWikimediaCode}}",
			"prop": "wikitext"
		}).then(function (response) {
			return JSON.parse(response.expandtemplates.wikitext);
		});
	};
	
	this.automaticTranslitCacheableStorage = new CacheableStorage("LanguageUtils-AutomaticTransliteration", this.automaticTranslitLanguages, 7, 1);
	this.wikimediaCodesCacheableStorage = new CacheableStorage("LanguageUtils-WikimediaCodes", this.wiktionaryCodeToWikimediaCode, 7, 1);
	
	this.HasAutomaticTransliteration = function(langcode) {
		var langs = this.automaticTranslitCacheableStorage.GetItem();
		if (langs){
			return langs[langcode] || false;
		}
		return false;
	};
	this.GetWikimediaCodeByWiktionaryCode = function(langcode) {
		var wikimediaCodes = this.wikimediaCodesCacheableStorage.GetItem();
		if (wikimediaCodes && wikimediaCodes[langcode]){
			return wikimediaCodes[langcode][0];
		}
		return null;
	};
};


window.ScriptUtils = function(){	
	this.getAllDataPromise = function(){
		return new mw.Api().get({
			"action": "expandtemplates",
			"format": "json",
			"text": "{{#invoke:languages/javascript-interface|AllLangcodeToScripts}}",
			"prop": "wikitext"
		}).then(function(r){return JSON.parse(r.expandtemplates.wikitext);});
	};
	
	this.cacheableStorage = new CacheableStorage("ScriptUtils", this.getAllDataPromise, 7, 1);
	this.GetScriptsByLangCode = function(langcode){
		var allData = this.cacheableStorage.GetItem();
		if (allData){
			return allData[langcode];
		}
		return [];
	};
};

// </nowiki>