Модул:Linguistic — разлика између измена

Iz Vojne Enciklopedije
Пређи на навигацију Пређи на претрагу
мНема описа измене
 
м (1 измена увезена)
Ред 1: Ред 1:
-- some simple internationalization that can be called by other modules
-- some simple internationalization that can be called by other modules  


-- ==================================================
local p = {}
-- === Internal functions ===========================
local f = require('Module:Fallback')
-- ==================================================
local i18n = mw.loadData('Module:I18n/linguistic')
 
local function getText(msg, lang)
return f._langSwitch(i18n[msg], lang)
end
 
local vowels = 'aeiouyąăẵằẳặȃắâẫấầẩậãäǟāáàȁǎảẚåǻḁạǡæǣǽĕȇêễếềểệḙẽḛëēḕéḗèȅěẻẹęȩḝǝĭȋîĩḭïḯīíìȉǐỉịįıŏȏôỗốồổộõṏṍöōṑóṓòȍǒỏọǫǭơỡớờởợøǿŭȗûṷũṻṹṵüǖǘǜǚṳūúùȕǔủůụųưữứừửựŷỹÿȳýỳỷẙỵ'
 
local function wordor(lang)
return getText('or_conj', lang)
end
 
local function comma(lang)
return getText("comma", lang)
end
 
function p.fullstop(lang)
return getText("full_stop", lang)
end
 
function p.colon(lang)
getText("colon", lang)
end
 
local function wordand(lang)
return getText("and_conj", lang)
end
 
local function wordsep(lang) -- default separator between words
return getText("word_separator", lang)
end


local function langSwitch(list,lang)
local function isin(str, pattern)
local langList = mw.language.getFallbacksFor(lang)
if str and pattern and mw.ustring.find(str, pattern, 1, true ) then
table.insert(langList,1,lang)
return true
for i,language in ipairs(langList) do
if list[language] then
return list[language]
end
end
end
return nil
end
end


local function wordand(lang)
local function langisin(str, lang)
local wordsep = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
return isin(str, lang .. ' ') -- space is necessary to avoid false positives like zh in zh-hans
local andtable = { -- languages with a problem with the MediaWiki:And
end
no = ' og',
 
pl = ' i',
local function processgender(str)
zh = '',
local t = {
ja = 'および'
f = 'feminine',
fem = 'feminine',
feminine = 'feminine',
n = 'neutral',
neutral = 'neutral',
m = 'masculine',
masc = 'masculine',
masculine = 'masculine',
}
}
if andtable[lang] then
return t[str] or 'masculine'
return andtable[lang] .. wordsep
end
elseif lang == 'he' then
 
return ' ו' -- no wordsep
local function processnumber(str)
else
if (str == 'p') or (str == 'plural') then
return mw.message.new( "and" ):inLanguage(lang):plain() .. wordsep
return 'plural'
else  
return 'singular'
end
end
end
end
-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}


function p.vowelfirst (str)
function p.vowelfirst (str)
if str then  
if str then return isin(vowels, str[1]) end
local vowels = 'aeiouyąăẵằẳặȃắâẫấầẩậãäǟāáàȁǎảẚåǻḁạǡæǣǽĕȇêễếềểệḙẽḛëēḕéḗèȅěẻẹęȩḝǝĭȋîĩḭïḯīíìȉǐỉịįıŏȏôỗốồổộõṏṍöōṑóṓòȍǒỏọǫǭơỡớờởợøǿŭȗûṷũṻṹṵüǖǘǜǚṳūúùȕǔủůụųưữứừửựŷỹÿȳýỳỷẙỵ'
str = mw.ustring.lower(mw.ustring.sub(str,1,1))
return mw.ustring.find(vowels, str, 1, true )  
end
end
end


function p.inparentehses(str)
function p.inparentheses(str, lang)
if (not str) or (str == "") then
--todo: define language with exotic parentheses
return nil
if str == '' then
return str
else
return ' (' .. str .. ')'
-- needs internationalization.
--Needs leading space in Enlgish because as some languages do not use it, it is part of the formatting
end
end
return "(".. str .. ")" -- same in all languages  ?
end
end


function p.of(word, lang, raw, gender, number, determiner) -- rough translation of "of" in various languages
function p.of(word, lang, raw, gender, number, determiner) -- rough translation of "of" in various languages
-- note that the cases when on "of" is employed varies a lot among languages, so it is more prudent to call this from lang specific function only
-- note that the cases when on "of" is employed varies a lot among languages, so it is more prudent to call this from lang specific function only
if not raw or mw.text.trim(raw) == "" then  
if not raw then  
local args = {}
raw = word
local Delink = require('Module:Delink')._delink
args[1] = word
raw = mw.ustring.lower(Delink(args))
end
end
 
gender = processgender(gender)
number = processnumber(number)
-- raw is the string without the Wikiformatting so that it correctly analyses the string that is [[:fr:Italie|Italie]] -> 'italie'
-- raw is the string without the Wikiformatting so that it correctly analyses the string that is [[:fr:Italie|Italie]] -> 'italie'
-- any way to automate this ?
-- any way to automate this ?
-- todo: ca to replace Template:Of/ca
if lang == 'fr' then  
if lang == 'fr' then  
local yesno = require('Module:Yesno')
if number == 'plural' then
determiner = yesno(determiner,false)
return 'des ' .. word
 
elseif p.vowelfirst(raw) then
if determiner then
return 'de l\'' .. word
if string.sub(number or '',1,1)=='p' then -- number == 'plural'
elseif gender == 'feminine' then
return 'des ' .. word
return 'de la ' .. word
elseif p.vowelfirst(raw) then
elseif derterminer then
return 'de l’' .. word
return 'du ' .. word
elseif string.sub(gender or '',1,1)=='f' then -- gender == 'feminine'
return 'de la ' .. word
else
return 'du ' .. word
end
else
if p.vowelfirst(raw) then
return 'd’' .. word
else
return 'de ' .. word
end
end
elseif lang == 'ca' then
-- implement [[Template:Of/ca]] or  https://ca.wikipedia.org/wiki/Plantilla:Deod%27/base for case where "{{{context}}}" is "en" (default on Commons)
if ( p.vowelfirst(raw) and not mw.ustring.find( 'ia|ià|ie|io|iu|ua|ue|ui|uí|uï|uo|ya|ye|yi|yo|yu|', mw.ustring.sub(raw,1,2) .. '|')) then
return 'd\'' .. word
else
else
return 'de ' .. word
return 'de ' .. word
Ред 95: Ред 108:
end
end


end
function p.offromwiki(frame)
args = frame.args
return p.of(args.word, args.lang, args.raw, args.gender, args.number, args.determiner)
end
end


Ред 109: Ред 117:
then return noun
then return noun
end
end
-- adjective before the noun
local wordsep = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
if langisin('cs de de-at de-ch en en-ca en-gb pl sk zh zh-cn zh-hans zh-hant zh-my zh-sg zh-tw ', lang) then
local LUT = { de=1, en=1, pl=1, zh=1, fr=2, es=2, it=2}
return adj .. wordsep(lang) .. noun
local case = langSwitch(LUT, lang)
-- adjective after the noun
if case==1 then     -- adjective before the noun
elseif langisin('fr fr-ca es it') then
return adj .. wordsep .. noun
return noun .. wordsep(lang) .. adj
elseif case==2 then -- adjective after the noun
else
return noun .. wordsep .. adj
else               -- order unknown
return noun ' (' .. adj .. ')'
return noun ' (' .. adj .. ')'
end
end
Ред 123: Ред 129:


function p.conj(args, lang, conjtype)
function p.conj(args, lang, conjtype)
local comma  = mw.message.new( "comma-separator"):inLanguage(lang):plain()
if (not args) then
local wordsep = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
return nil
end
local newargs = {}
for i, j in pairs(args) do
if type(j) ~= 'nil' then
table.insert(newargs, j)
end
end
args = newargs
if #args == 0 then
return nil
end
if conjtype == 'comma' then
if conjtype == 'comma' then
return mw.text.listToText(args, comma, comma)
return mw.text.listToText(args, comma(lang), comma(lang))
elseif conjtype == 'or' then  
elseif conjtype == 'or' then  
local wordor = langSwitch(require('Module:I18n/or'), lang)
return mw.text.listToText(args, comma(lang), wordor(lang) .. wordsep(lang))
return mw.text.listToText(args, comma, wordor  .. wordsep)
elseif conjtype == 'explicit or' then -- adds "or" betwen all words when the context can be confusing
elseif conjtype == 'explicit or' then -- adds "or" betwen all words when the context can be confusing
local wordor = langSwitch(require('Module:I18n/or'), lang)
return mw.text.listToText(args, wordor(lang) .. wordsep(lang), wordor(lang) .. wordsep(lang))
return mw.text.listToText(args, wordor .. wordsep, wordor  .. wordsep)
elseif conjtype then
elseif conjtype and conjtype ~= 'and' and conjtype ~= '' then
return mw.text.listToText(args, conjtype, conjtype)
return mw.text.listToText(args, conjtype, conjtype)
else  
else  
return mw.text.listToText(args, comma, wordand(lang))
return mw.text.listToText(args, comma(lang), wordand(lang) .. wordsep(lang))
end
end
 
function p.conjfromWiki(frame)
args = frame.args
if not args or not args[1] then
args = mw.getCurrentFrame():getParent().args
end
local conjtype = args.type
local lang = args.lang
if not lang or mw.text.trim(lang) == '' then
lang = frame:preprocess( "{{int:lang}}" )
end
newargs = {}  -- transform args metatable into a table so it can be concetenated
for i, j in pairs(args) do
if type(i) == 'number' then
j = mw.text.trim(j)
if j ~= '' then
table.insert(newargs, j)
end
else
if i ~= 'type' and i ~= 'lang' then
return 'error: bad parameter in template:Conj: ' .. i .. '[[Category:Pages with incorrect template usage/Conj|A]]'
end
end
end
end
return p.conj(newargs, lang, conjtype)
end
end


return p
return p

Верзија на датум 1. новембар 2020. у 13:49

Документацију овог модула можете да направите на страници Модул:Linguistic/док

-- some simple internationalization that can be called by other modules 

local p = {}
local f = require('Module:Fallback')
local i18n = mw.loadData('Module:I18n/linguistic')

local function getText(msg, lang)
	return f._langSwitch(i18n[msg], lang)
end

local vowels = 'aeiouyąăẵằẳặȃắâẫấầẩậãäǟāáàȁǎảẚåǻḁạǡæǣǽĕȇêễếềểệḙẽḛëēḕéḗèȅěẻẹęȩḝǝĭȋîĩḭïḯīíìȉǐỉịįıŏȏôỗốồổộõṏṍöōṑóṓòȍǒỏọǫǭơỡớờởợøǿŭȗûṷũṻṹṵüǖǘǜǚṳūúùȕǔủůụųưữứừửựŷỹÿȳýỳỷẙỵ'

local function wordor(lang) 
	return getText('or_conj', lang)
end

local function comma(lang)
	return getText("comma", lang)
end

function p.fullstop(lang)
	return getText("full_stop", lang)
end

function p.colon(lang)
	getText("colon", lang)
end

local function wordand(lang)
	return getText("and_conj", lang)
end

local function wordsep(lang) -- default separator between words
	return getText("word_separator", lang)
end	

local function isin(str, pattern)
	if str and pattern and mw.ustring.find(str, pattern, 1, true ) then
		return true
	end
end

local function langisin(str, lang)
	return isin(str, lang .. ' ') -- space is necessary to avoid false positives like zh in zh-hans
end

local function processgender(str)
	local t = {	
		f = 'feminine',
		fem = 'feminine',
		feminine = 'feminine',
		n = 'neutral',
		neutral = 'neutral',
		m = 'masculine',
		masc = 'masculine',
		masculine = 'masculine',
	}
	return t[str] or 'masculine'
end

local function processnumber(str)
	if (str == 'p') or (str == 'plural') then
		return 'plural'
	else 
		return 'singular'
	end
end

function p.vowelfirst (str)
	if str then return isin(vowels, str[1]) end
end

function p.inparentheses(str, lang)
	--todo: define language with exotic parentheses
	if str == '' then
		return str
	else 
		return ' (' .. str .. ')' 
		-- needs internationalization. 
		--Needs leading space in Enlgish because as some languages do not use it, it is part of the formatting
	end
end

function p.of(word, lang, raw, gender, number, determiner) -- rough translation of "of" in various languages
-- note that the cases when on "of" is employed varies a lot among languages, so it is more prudent to call this from lang specific function only
	if not raw then 
		raw = word
	end
	gender = processgender(gender)
	number = processnumber(number)
	-- raw is the string without the Wikiformatting so that it correctly analyses the string that is [[:fr:Italie|Italie]] -> 'italie'
	-- any way to automate this ?
	
	-- todo: ca to replace Template:Of/ca
	
	if lang == 'fr' then 
		if number == 'plural' then
			return 'des ' .. word
		elseif p.vowelfirst(raw) then
			return 'de l\'' .. word
		elseif gender == 'feminine' then
			return 'de la ' .. word
		elseif derterminer then
			return 'du ' .. word
		else
			return 'de ' .. word
		end
	end	

end

function p.noungroup(noun, adj, lang)
	if not noun or noun == '' then 
		return nil -- not '' so that it is not counted as a string by mw.listToText
	end
	if not adj or adj == ''
		then return noun
	end
	-- adjective before the noun
	if langisin('cs de de-at de-ch en en-ca en-gb pl sk zh zh-cn zh-hans zh-hant zh-my zh-sg zh-tw ', lang) then
		return adj .. wordsep(lang) .. noun
	-- adjective after the noun
	elseif langisin('fr fr-ca es it') then
		return noun .. wordsep(lang) .. adj
	else
		return noun ' (' .. adj .. ')'
	end
end

function p.conj(args, lang, conjtype)
	if (not args) then
		return nil
	end
	local newargs = {}
	for i, j in pairs(args) do
		if type(j) ~= 'nil' then
			table.insert(newargs, j)
		end
	end
	args = newargs
	if #args == 0 then
		return nil
	end
	if conjtype == 'comma' then
		return mw.text.listToText(args, comma(lang), comma(lang))
	elseif conjtype == 'or' then 
		return mw.text.listToText(args, comma(lang), wordor(lang)  .. wordsep(lang))
	elseif conjtype == 'explicit or' then -- adds "or" betwen all words when the context can be confusing
		return mw.text.listToText(args, wordor(lang) .. wordsep(lang), wordor(lang)  .. wordsep(lang))
	elseif conjtype then
		return mw.text.listToText(args, conjtype, conjtype)
	else 
		return mw.text.listToText(args, comma(lang), wordand(lang) .. wordsep(lang))
	end
end

return p