Module:Formatnum and Module:Formatnum/sandbox: Difference between pages
(Difference between pages)
imported>RexxS m Changed protection level for "Module:Formatnum": High-risk Lua module ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)) |
imported>Trigenibinion Wordify moved to Module:Wordify |
||
| Line 1: | Line 1: | ||
-- This module is intended to replace the functionality of Template:Formatnum and related templates. | -- This module is intended to replace the functionality of Template:Formatnum and related templates. | ||
local p = {} | local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules. | ||
--[[ | |||
Helper functions used to avoid redundant code. | |||
]] | |||
local function getArgs(frame) | |||
local args = {} | |||
for key, value in pairs(frame:getParent().args) do | |||
args[key] = value | |||
end | |||
for key, value in pairs(frame.args) do | |||
args[key] = value | |||
end | |||
return args | |||
end | |||
function p.main(frame) | function p.main(frame) | ||
local args = frame | local args = getArgs(frame) | ||
local prec = args.prec or '' | local prec = args.prec or '' | ||
local sep = args.sep or '' | local sep = args.sep or '' | ||
| Line 9: | Line 24: | ||
local lang = args[2] or args.lang or '' | local lang = args[2] or args.lang or '' | ||
-- validate the language parameter within MediaWiki's caller frame | -- validate the language parameter within MediaWiki's caller frame | ||
if lang == "arabic-indic" then -- only for back-compatibility ("arabic-indic" is not a SupportedLanguage) | if lang:lower() == "none" then | ||
-- no language, so do nothing | |||
elseif lang == "arabic-indic" then -- only for back-compatibility ("arabic-indic" is not a SupportedLanguage) | |||
lang = "fa" -- better support than "ks" | lang = "fa" -- better support than "ks" | ||
elseif lang == '' or not mw.language.isSupportedLanguage(lang) then | elseif lang == '' or not mw.language.isSupportedLanguage(lang) then | ||
| Line 20: | Line 37: | ||
-- instead of the local 'ContentLanguage' of the Wiki. | -- instead of the local 'ContentLanguage' of the Wiki. | ||
lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language | lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language | ||
if not mw.language.isSupportedLanguage(lang) then | |||
lang = mw.language.getContentLanguage().code | |||
end | |||
end | end | ||
return p.formatNum(number, lang, prec, sep ~= '') | return p.formatNum(number, lang, prec, sep ~= '') | ||
| Line 33: | Line 53: | ||
function p.formatNum(number, lang, prec, compact) | function p.formatNum(number, lang, prec, compact) | ||
-- Do not alter the specified value when it is not a valid number, return it as is | -- Do not alter the specified value when it is not a valid number, return it as is | ||
local value = tonumber(number) | local value = tonumber(number) | ||
| Line 77: | Line 96: | ||
prec = 0 -- don't alter the precision | prec = 0 -- don't alter the precision | ||
end | end | ||
if lang ~= nil and mw.language.isKnownLanguageTag(lang) == true then | if lang ~= nil and mw.language.isKnownLanguageTag(lang) == true then | ||
-- Convert number to localized digits, decimal separator, and group separators | -- Convert number to localized digits, decimal separator, and group separators | ||
| Line 94: | Line 113: | ||
number = mw.ustring.sub(number, 1, mw.ustring.len(number) + prec) | number = mw.ustring.sub(number, 1, mw.ustring.len(number) + prec) | ||
end | end | ||
-- Append the localized base-10 exponent without grouping separators (there's no reliable way to detect a localized leading symbol 'E') | -- Append the localized base-10 exponent without grouping separators (there's no reliable way to detect a localized leading symbol 'E') | ||
if exponent ~= '' then | if exponent ~= '' then | ||
| Line 107: | Line 125: | ||
number = mw.string.sub(number, 1, mw.string.len(number) + prec) | number = mw.string.sub(number, 1, mw.string.len(number) + prec) | ||
end | end | ||
-- Append the base-10 exponent | -- Append the base-10 exponent | ||
if exponent ~= '' then | if exponent ~= '' then | ||