Module:Linguistic: Difference between revisions

From TDWG Collection Description
Jump to navigation Jump to search
(Created page with "-- some simple internationalization that can be called by other modules local p = {} local f = require('Module:Fallback') local i18n = mw.loadData('Module:I18n/linguistic')...")
 
No edit summary
 
Line 1: Line 1:
-- some simple internationalization that can be called by other modules
--[[
  __  __          _      _        _    _                  _    _  _     
|  \/  | ___  __| |_  _| | ___ _| |  (_)_ __  __ _ _  _(_)___| |_(_) ___
| |\/| |/ _ \ / _` | | | | |/ _ (_) |  | | '_ \ / _` | | | | / __| __| |/ __|
| |  | | (_) | (_| | |_| | |  __/_| |___| | | | | (_| | |_| | \__ \ |_| | (__
|_|  |_|\___/ \__,_|\__,_|_|\___(_)_____|_|_| |_|\__, |\__,_|_|___/\__|_|\___|
                                                  |___/                       
Simple internationalization functions that can be called by other modules.
This Module was copied from Wikimedia Commons, so please request changes there.


local p = {}
Maintainers:
local f = require('Module:Fallback')
* Zolo - original version
local i18n = mw.loadData('Module:I18n/linguistic')
* Jarekt


local function getText(msg, lang)
Dependencies:
return f._langSwitch(i18n[msg], lang)
* Module:I18n/or
end
* Module:Delink
* Module:Yesno
]]


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


local function wordor(lang)
local function langSwitch(list,lang)
return getText('or_conj', lang)
local langList = mw.language.getFallbacksFor(lang)
table.insert(langList,1,lang)
for i,language in ipairs(langList) do
if list[language] then
return list[language]
end
end
return nil
end
end


local function comma(lang)
-- ==================================================
return getText("comma", lang)
-- === External functions ===========================
end
-- ==================================================
 
local p = {}
local function citation_comma(lang)
return getText("citation_comma", lang)
end
 
function p.fullstop(lang)
return getText("full_stop", lang)
end


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


function p.inparentheses(str, lang)
function p.inparentheses(str, lang)
--todo: define language with exotic parentheses
--todo: define language with exotic parentheses
if str == '' then
if (not str) or (str == "") then
return str
return str
else
else
Line 86: Line 60:
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 then
if not raw or mw.text.trim(raw) == "" then  
raw = word
local args = {}
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  
local yesno = require('Module:Yesno')
if lang == 'fr' then
determiner = yesno(determiner,false)
if number == 'plural' then
 
return 'des ' .. word
if determiner then
elseif p.vowelfirst(raw) then
if string.sub(number or '',1,1)=='p' then -- number == 'plural'
return 'de l\'' .. word
return 'des ' .. word
elseif gender == 'feminine' then
elseif p.vowelfirst(raw) then
return 'de la ' .. word
return 'de l’' .. word
elseif derterminer then
elseif string.sub(gender or '',1,1)=='f' then -- gender == 'feminine'
return 'du ' .. word
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
end
end
end
end
return raw
 
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


function p.noungroup(noun, adj, lang)
function p.noungroup(noun, adj, lang)
if not noun or noun == '' then
if not noun or noun == '' then  
return nil -- not '' so that it is not counted as a string by mw.listToText
return nil -- not '' so that it is not counted as a string by mw.listToText
end
end
if not adj or adj == '' then
if not adj or adj == ''
return noun
then return noun
end
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
local wordsep = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
return adj .. wordsep(lang) .. noun
local LUT = { de=1, en=1, pl=1, zh=1, fr=2, es=2, it=2}
-- adjective after the noun
local case = langSwitch(LUT, lang)
elseif langisin('fr fr-ca es it') then
if case==1 then     -- adjective before the noun
return noun .. wordsep(lang) .. adj
return adj .. wordsep .. noun
else
elseif case==2 then -- adjective after the noun
return noun .. p.inparentheses(adj, lang)
return noun .. wordsep .. adj
else               -- order unknown
return noun ' (' .. adj .. ')'
end
end
end
end


function p.conj(args, lang, conjtype)
function p.conj(args, lang, conjtype)
if not args then
local comma    = mw.message.new( "comma-separator"):inLanguage(lang):plain()
return nil
local wordsep  = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
local andtable = { ar=' و', he=' ו', ja='および', pl=' i '} -- languages with a problem with the MediaWiki:And
if conjtype == 'comma' then
return mw.text.listToText(args, comma, comma)
elseif conjtype == 'or' then
local wordor = langSwitch(require('Module:I18n/or'), 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
local wordor = langSwitch(require('Module:I18n/or'), lang)
return mw.text.listToText(args, wordor .. wordsep, wordor  .. wordsep)
elseif conjtype and conjtype ~= 'and' and conjtype ~= '' then
return mw.text.listToText(args, conjtype, conjtype)
elseif andtable[lang] then
return mw.text.listToText(args, comma, andtable[lang])
else
local wordand = mw.message.new( "and" ):inLanguage(lang):plain()
return mw.text.listToText(args, comma, wordand .. wordsep)
end
end
local newargs = {}
end
for i, j in pairs(args) do
 
if type(j) ~= 'nil' then
function p.conjfromWiki(frame)
table.insert(newargs, j)
args = frame.args
end
if not args or not args[1] then
args = frame:getParent().args
end
end
args = newargs
local lang = args.lang
if #args == 0 then
if not lang or mw.text.trim(lang) == '' then
return nil
lang = frame:callParserFunction( "int", "lang" )
end
end
if conjtype == 'comma' then
newargs = {}  -- transform args metatable into a table so it can be concetenated
return mw.text.listToText(args, comma(lang), comma(lang))
for i, j in pairs(args) do
elseif conjtype == 'citation_comma' then
if type(i) == 'number' then
return mw.text.listToText(args, citation_comma(lang), citation_comma(lang))
j = mw.text.trim(j)
elseif conjtype == 'or' then
if j ~= '' then
return mw.text.listToText(args, comma(lang), wordor(lang) .. wordsep(lang))
table.insert(newargs, j)
elseif conjtype == 'explicit or' then -- adds "or" between all words when the context can be confusing
end
return mw.text.listToText(args, wordor(lang) .. wordsep(lang), wordor(lang) .. wordsep(lang))
else
elseif conjtype then
if i ~= 'type' and i ~= 'lang' then  
return mw.text.listToText(args, conjtype, conjtype)
return 'error: bad parameter in template:Conj: ' .. i .. '[[Category:Pages with incorrect template usage/Conj|A]]'
else
end
return mw.text.listToText(args, comma(lang), wordand(lang) .. wordsep(lang))
end
end
end
return p.conj(newargs, lang, args.type)
end
end


return p
return p

Latest revision as of 07:53, 13 May 2020

Documentation for this module may be created at Module:Linguistic/doc

--[[
  __  __           _       _        _     _                   _     _   _      
 |  \/  | ___   __| |_   _| | ___ _| |   (_)_ __   __ _ _   _(_)___| |_(_) ___ 
 | |\/| |/ _ \ / _` | | | | |/ _ (_) |   | | '_ \ / _` | | | | / __| __| |/ __|
 | |  | | (_) | (_| | |_| | |  __/_| |___| | | | | (_| | |_| | \__ \ |_| | (__ 
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)_____|_|_| |_|\__, |\__,_|_|___/\__|_|\___|
                                                  |___/                        
Simple internationalization functions that can be called by other modules.
This Module was copied from Wikimedia Commons, so please request changes there.

Maintainers:
* Zolo - original version
* Jarekt

Dependencies:
* Module:I18n/or
* Module:Delink
* Module:Yesno
]]

-- ==================================================
-- === Internal functions ===========================
-- ==================================================

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

-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}

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

function p.inparentheses(str, lang)
	--todo: define language with exotic parentheses
	if (not str) or (str == "") then
		return str
	else
		return ' (' .. str .. ')'
		-- needs internationalization.
		-- Needs leading space in English 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 or mw.text.trim(raw) == "" then 
		local args = {}
		local Delink = require('Module:Delink')._delink
		args[1] = word
		raw = mw.ustring.lower(Delink(args))
	end

	-- 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 ?
	
	if lang == 'fr' then 
		local yesno = require('Module:Yesno')
		determiner = yesno(determiner,false)

		if determiner then
			if string.sub(number or '',1,1)=='p' then -- number == 'plural'
				return 'des ' .. word
			elseif p.vowelfirst(raw) then
				return 'de l’' .. 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
			return 'de ' .. word
		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

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
	
	local wordsep = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
	local LUT = { de=1, en=1, pl=1, zh=1, fr=2, es=2, it=2}
	local case = langSwitch(LUT, lang)
	if case==1 then     -- adjective before the noun
		return adj .. wordsep .. noun
	elseif case==2 then -- adjective after the noun
		return noun .. wordsep .. adj
	else                -- order unknown
		return noun ' (' .. adj .. ')'
	end
end

function p.conj(args, lang, conjtype)
	local comma    = mw.message.new( "comma-separator"):inLanguage(lang):plain()
	local wordsep  = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
	local andtable = { ar=' و', he=' ו', ja='および', pl=' i '} -- languages with a problem with the MediaWiki:And
	if conjtype == 'comma' then
		return mw.text.listToText(args, comma, comma)
	elseif conjtype == 'or' then 
		local wordor = 	langSwitch(require('Module:I18n/or'), 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
		local wordor = 	langSwitch(require('Module:I18n/or'), lang)
		return mw.text.listToText(args, wordor .. wordsep, wordor  .. wordsep)
	elseif conjtype and conjtype ~= 'and' and conjtype ~= '' then
		return mw.text.listToText(args, conjtype, conjtype)
	elseif andtable[lang] then 
		return mw.text.listToText(args, comma, andtable[lang])
	else
		local wordand = mw.message.new( "and" ):inLanguage(lang):plain()
		return mw.text.listToText(args, comma, wordand .. wordsep)
	end
end

function p.conjfromWiki(frame)
	args = frame.args
	if not args or not args[1] then
		args = frame:getParent().args
	end
	local lang = args.lang
	if not lang or mw.text.trim(lang) == '' then
		lang = frame:callParserFunction( "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
	return p.conj(newargs, lang, args.type)
end

return p