Module:Ordinal: Διαφορά μεταξύ των αναθεωρήσεων

Περιεχόμενο που διαγράφηκε Περιεχόμενο που προστέθηκε
Χωρίς σύνοψη επεξεργασίας
RexxS (συζήτηση | συνεισφορές)
update from https://commons.wikimedia.org/w/index.php?title=Module:Ordinal&oldid=358324630
 
Γραμμή 10:
]]
 
local p = {}
-- =======================================
-- === Dependencies ======================
-- =======================================
local i18n = require('Module:I18n/ordinal') -- get localized translations of ordinals
local LangSwitch = require('Module:LangSwitch') -- get LangSwitch function
local yesno = require('Module:Yesno') -- boolean value interpretation
local formatnum = require('Module:Formatnum') -- number formatting
local roman = require('Module:Roman') -- roman numeral conversion (primarily for French)
 
-- =======================================
local i18n = require('Module:I18n/ordinal') -- get localized translations of ordinals
-- === Private Functions =================
local fallback = require('Module:Fallback') -- get fallback functions
-- =======================================
local yesno = require('Module:Yesno') -- boolean value interpretation
 
local formatnum = require('Module:Formatnum') -- number formatting
--[[
local roman = require('Module:Roman') -- roman numeral conversion (primarily for French)
Helper function to generate superscripted content
]]
local function Superscript( str, superscript, nosup, period )
if superscript and (not nosup) and (str ~= '') then
return period .. '<sup>' .. str .. '</sup>'
else
return str
end
end
 
--[[
Helper function to call Formatnum.
]]
local function FormatNum( value, lang )
if lang == 'roman' then
return roman._Numeral(value)
else
return formatnum.formatNum(value, lang)
end
end
--[[
Helper function to add append a category to a message.
]]
local function output_cat( message, category )
return message .. '[[Category:' .. category .. ']]'
end
 
--[[
Helper function to handle error messages.
]]
local function output_error( error_str, value )
error_str = '<strong class="error"><span title="Error: ' .. error_str .. '">' .. value .. '</span></strong>'
return output_cat(error_str, 'Errors reported by Module Ordinal');
end
 
--[[
This function is the core functionality for adding the appropriate ordinal suffix to a given integer.
]]
local function OrdinalCore( value, lang, style, gender, nosup )
-- Just in case someone breaks the internationalization code, fix the english scheme
if i18n.SchemeFromLang['en'] == nil then
i18n.SchemeFromLang['en'] = 'en-scheme'
end
if i18n.Scheme['en-scheme'] == nil then
i18n.Scheme['en-scheme'] = {rules = 'skip-tens', superscript = true, suffix = 'th', suffix_1 = 'st', suffix_2 = 'nd', suffix_3 = 'rd'}
end
-- Add the default scheme (i.e. "<value>.")
if i18n.SchemeFromLang['default'] == nil then
i18n.SchemeFromLang['default'] = 'period-scheme'
end
if i18n.Scheme['period-scheme'] == nil then
i18n.Scheme['period-scheme'] = {rules = 'suffix', suffix = '.'}
end
-- which scheme should we use to format the ordinal value?
-- Use Fallback module to handle languages groups that map to a supported language
local schemeSpecifier = LangSwitch._langSwitch(i18n.SchemeFromLang, lang)
-- Look up scheme based on scheme specifier (and possibly style)
local scheme = i18n.Scheme[schemeSpecifier .. '/' .. style] or i18n.Scheme[schemeSpecifier]
-- process scheme by applying rules identified by Scheme
local output = ''
local period = (scheme.period and '.') or ''
local rules = scheme.rules
if rules == 'skip-tens' then
local suffix
local mod100 = math.floor(math.abs(value)) % 100
if (mod100 >= 10) and (mod100 <= 19) then
suffix = scheme.suffix or ''
else
local mod10 = math.floor(math.abs(value)) % 10
suffix = scheme['suffix_'..mod10] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'suffix' then
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( scheme.suffix or '', scheme.superscript, nosup, period)
elseif rules == 'prefix' then
output = (scheme.prefix or '') .. FormatNum(value, scheme.formatlang or lang)
elseif rules == 'mod10-suffix' then
local index = math.floor(math.abs(value)) % 10
local suffix = scheme['suffix_'..index] or scheme.suffix or ''
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'gendered-suffix' then
local suffix = scheme['suffix_'..gender] or scheme.suffix or ''
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'gendered-suffix-one' then
local suffix
if value == 1 then
suffix = scheme['suffix_1_'..gender] or scheme['suffix_1'] or scheme.suffix or ''
else
suffix = scheme['suffix_'..gender] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'gendered-suffix-n' then
local suffix
if value <= 9 then
suffix = scheme['suffix_'..value..'_'..gender] or scheme['suffix_'..value] or scheme['suffix_'..gender] or scheme.suffix or ''
else
suffix = scheme['suffix_'..gender] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'suffix-one' then
local prefix, suffix
if value == 1 then
prefix = scheme['prefix_1'] or scheme.prefix or ''
suffix = scheme['suffix_1'] or scheme.suffix or ''
else
prefix = scheme.prefix or ''
suffix = scheme.suffix or ''
end
output = prefix .. FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'mod10-gendered-suffix-skip-tens' then
local suffix
local mod100 = math.floor(math.abs(value)) % 100
if (mod100 >= 10) and (mod100 <= 19) then
suffix = scheme['suffix_'..gender] or scheme.suffix or ''
else
local mod10 = math.floor(math.abs(value)) % 10
suffix = scheme['suffix_'..mod10..'_'..gender] or scheme['suffix_'..mod10] or scheme['suffix_'..gender] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'uk-rules' then
local suffix
local mod100 = math.floor(math.abs(value)) % 100
local mod1000 = math.floor(math.abs(value)) % 1000
if (mod1000 == 0) then
suffix = scheme['suffix_1000_'..gender] or scheme.suffix or ''
elseif (mod100 == 40) then
suffix = scheme['suffix_40_'..gender] or scheme.suffix or ''
elseif (mod100 >= 10) and (mod100 <= 19) then
suffix = scheme['suffix_'..gender] or scheme.suffix or ''
else
local mod10 = math.floor(math.abs(value)) % 10
suffix = scheme['suffix_'..mod10..'_'..gender] or scheme['suffix_'..mod10] or scheme['suffix_'..gender] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
else
output = FormatNum(value, lang)
end
return output
end
 
-- =======================================
Γραμμή 27 ⟶ 176 :
-- =======================================
 
local p = {}
--[[
Ordinal
Γραμμή 49 ⟶ 199 :
]]
function p.Ordinal( frame )
-- if no argument provided than check parent template/module args
local args = frame.args
if args[1]==nil then
args = frame:getParent().args
end
return p._ordinal(args)
end
 
function p._ordinal(args)
-- if no argument provided than check parent template/module args
 
-- if we don't have a specified language, attempt to use the user's language
local lang = args.lang
if not lang or lang == '' or not mw.language.isValidCode( lang ) then
lang = mw.getCurrentFrameframe:preprocess('{{int:lang}}')
end
Γραμμή 141 ⟶ 287 :
return output
end
 
-- =======================================
-- === Private Functions =================
-- =======================================
 
--[[
This function is the core functionality for adding the appropriate ordinal suffix to a given integer.
]]
function OrdinalCore( value, lang, style, gender, nosup )
-- Just in case someone breaks the internationalization code, fix the english scheme
if i18n.SchemeFromLang['en'] == nil then
i18n.SchemeFromLang['en'] = 'en-scheme'
end
if i18n.Scheme['en-scheme'] == nil then
i18n.Scheme['en-scheme'] = {rules = 'skip-tens', superscript = true, suffix = 'th', suffix_1 = 'st', suffix_2 = 'nd', suffix_3 = 'rd'}
end
-- Add the default scheme (i.e. "<value>.")
if i18n.SchemeFromLang['default'] == nil then
i18n.SchemeFromLang['default'] = 'period-scheme'
end
if i18n.Scheme['period-scheme'] == nil then
i18n.Scheme['period-scheme'] = {rules = 'suffix', suffix = '.'}
end
-- which scheme should we use to format the ordinal value?
-- Use Fallback module to handle languages groups that map to a supported language
local schemeSpecifier = fallback._langSwitch(i18n.SchemeFromLang, lang)
-- Look up scheme based on scheme specifier (and possibly style)
local scheme = i18n.Scheme[schemeSpecifier .. '/' .. style] or i18n.Scheme[schemeSpecifier]
-- process scheme by applying rules identified by Scheme
local output = ''
local period = (scheme.period and '.') or ''
local rules = scheme.rules
if rules == 'skip-tens' then
local suffix
local mod100 = math.floor(math.abs(value)) % 100
if (mod100 >= 10) and (mod100 <= 19) then
suffix = scheme.suffix or ''
else
local mod10 = math.floor(math.abs(value)) % 10
suffix = scheme['suffix_'..mod10] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'suffix' then
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( scheme.suffix or '', scheme.superscript, nosup, period)
elseif rules == 'prefix' then
output = (scheme.prefix or '') .. FormatNum(value, scheme.formatlang or lang)
elseif rules == 'mod10-suffix' then
local index = math.floor(math.abs(value)) % 10
local suffix = scheme['suffix_'..index] or scheme.suffix or ''
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'gendered-suffix' then
local suffix = scheme['suffix_'..gender] or scheme.suffix or ''
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'gendered-suffix-one' then
local suffix
if value == 1 then
suffix = scheme['suffix_1_'..gender] or scheme['suffix_1'] or scheme.suffix or ''
else
suffix = scheme['suffix_'..gender] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'suffix-one' then
local suffix
if value == 1 then
suffix = scheme['suffix_1'] or scheme.suffix or ''
else
suffix = scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
elseif rules == 'mod10-gendered-suffix-skip-tens' then
local suffix
local mod100 = math.floor(math.abs(value)) % 100
if (mod100 >= 10) and (mod100 <= 19) then
suffix = scheme['suffix_'..gender] or scheme.suffix or ''
else
local mod10 = math.floor(math.abs(value)) % 10
suffix = scheme['suffix_'..mod10..'_'..gender] or scheme['suffix_'..mod10] or scheme['suffix_'..gender] or scheme.suffix or ''
end
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)
else
output = FormatNum(value, lang)
end
return output
end
 
 
--[[
Helper function to generate superscripted content
]]
function Superscript( str, superscript, nosup, period )
if superscript and (not nosup) then
return period .. '<sup>' .. str .. '</sup>'
else
return str
end
end
 
--[[
Helper function to call Formatnum.
]]
function FormatNum( value, lang )
if lang == 'roman' then
return roman._Numeral(value)
else
return formatnum.formatNum(value, lang)
end
end
 
--[[
Helper function to handle error messages.
]]
function output_error( error_str, value )
error_str = '<strong class="error"><span title="Error: ' .. error_str .. '">' .. value .. '</span></strong>'
return output_cat(error_str, 'Errors reported by Module Ordinal');
end
 
 
--[[
Helper function to add append a category to a message.
]]
function output_cat( message, category )
return message .. '[[Category:' .. category .. ']]'
end
 
 
return p
Ανακτήθηκε από "https://el.wikipedia.org/wiki/Module:Ordinal"