Jump to content

Module:Language

Unchecked
From Wikipedia

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

-- @brief
--  Language object.
-- 
-- @author
--  [[meta:User:Danny B.]]
local Language = {}
----------------------------------------
 
 
local Languages = require( "Module:Languages" )
 
 
-- @brief
--  Parses and splits the language tag into appropriate parts.
-- 
-- @details
--  Very trivial implementation
-- 
-- @param
--  String langTag Language tag
-- 
-- @return
--  table Parts of parsed language tag
--  nil If language tag is invalid or any of its part is unknown
function Language._parseLanguageTag( langTag )
 
	local parsedLangTag = {}
	local subtagStartPos
	local subtagPartPos
	local _
 
	_, _, parsedLangTag.lang, subtagStartPos = langTag:find( "^(%l%l%l?)()" )
	_, _, parsedLangTag.region, subtagPartPos = langTag:find( "^%-(%u%u)()", subtagStartPos )
	_, _, parsedLangTag.script, subtagPartPos = langTag:find( "^%-(%u%l%l%l)()", subtagPartPos or subtagStartPos )
 
	return next( parsedLangTag ) and parsedLangTag
 
end
 
function Language._getLanguageFromLanguageTag( langTag )
	local parsedLangTag = Language._parseLanguageTag( langTag )
	return parsedLangTag and parsedLangTag.lang
end
 
-- @brief
--  Gets the language direction by language tag.
-- 
-- @param
--  String langTag Language tag
-- 
-- @return
--  String Language direction
--  nil If language tag is invalid or any of its language part is unknown
function Language:getDirection( langTag )
 
	local lang = self._getLanguageFromLanguageTag( langTag )
 
	return Languages[lang] and Languages[lang].dir or nil
 
end
 
 
-- @brief
--  Gets the language name by language tag.
-- 
-- @param
--  String langTag Language tag
-- 
-- @return
--  String Language name
--  nil If language tag is invalid or any of its language part is unknown
function Language:getName( langTag )
 
	local lang = self._getLanguageFromLanguageTag( langTag )
 
	return Languages[lang] and Languages[lang].name or nil
 
end
 
 
-- @brief
--  Gets the full language name (including script and region) by language tag.
-- 
-- @param
--  String langTag Language tag
-- 
-- @return
--  String Full language name
--  nil If language tag is invalid or any of its parts is unknown
function Language:getFullName( langTag )
 
	local output
	local parsedLangTag = self._parseLanguageTag( langTag )
 
	if parsedLangTag then
 
		local lang = Languages[parsedLangTag.lang] and Languages[parsedLangTag.lang].name
		local region = ""
		local script = ""
 
		if lang then
 
			output = lang
 
			if parsedLangTag.region then
				region = Languages[parsedLangTag.lang].regions and Languages[parsedLangTag.lang].regions[parsedLangTag.region]
			end
 
			if parsedLangTag.script then
				script = Languages[parsedLangTag.lang].scripts and Languages[parsedLangTag.lang].scripts[parsedLangTag.script]
			end
 
			if parsedLangTag.region or parsedLangTag.script then
				if region and script then
					if ( region ~= "" and script ~= "" ) then
						output = output .. " (" .. region .. ", " .. script .. ")"
					else
						output = output .. " (" .. ( region ~= "" and region or script ) .. ")"
					end
				else
					output = nil
				end
			end
 
		end
 
	end
 
	return output
 
end
 
 
----------------------------------------
return Language