--[=[ Convert (translate) text between bn, en.
---In an article or template---
translate2bn and translate2en can be used directly with #invoke, or
can be called from a template that uses #invoke.
Examples using #invoke directly:
{{#invoke:numeral converter|translate2bn|1234}} → ১২৩৪
{{#invoke:numeral converter|translate2en|১২৩৪}} → 1234
Examples using two templates:
Template:2bn contains
{{#invoke:numeral converter|translate2bn}}
Template:2en contains
{{#invoke:numeral converter|translate2en}}
{{2bn|1234}} → ১২৩৪
{{2en|১২৩৪}} → 1234
---Using convert_template in an article or template---
{{#invoke:numeral converter|convert_template|bn|1234}} → ১২৩৪
{{#invoke:numeral converter|convert_template|en|১২৩৪}} → 1234
Template:Convert contains
{{#invoke:numeral converter|convert_template}}
{{convert|bn|1234}} → ১২৩৪
{{convert|en|১২৩৪}} → 1234
---In another module---
local convert = require('মডিউল:Numeral converter').convert
convert('bn', '1234') -- gives ১২৩৪
convert('en', '১২৩৪') -- gives 1234
local translate2bn = require('মডিউল:Numeral converter')._translate2bn
translate2bn('1234') -- gives ১২৩৪
local translate2en = require('মডিউল:Numeral converter')._translate2en
translate2en('1234') -- gives ১২৩৪
]=]
local p = {}
function p._translate2bn(text)
if type(text) == 'string' then
text = text:gsub('%d', {
['0'] = '০',
['1'] = '১',
['2'] = '২',
['3'] = '৩',
['4'] = '৪',
['5'] = '৫',
['6'] = '৬',
['7'] = '৭',
['8'] = '৮',
['9'] = '৯',
})
end
return text
end
function p._translate2en(text)
if type(text) == 'string' then
text = mw.ustring.gsub(text, '%d', {
['০'] = '0',
['১'] = '1',
['২'] = '2',
['৩'] = '3',
['৪'] = '4',
['৫'] = '5',
['৬'] = '6',
['৭'] = '7',
['৮'] = '8',
['৯'] = '9',
})
end
return text
end
function p.convert(lang, text)
if lang == 'bn' then
return p._translate2bn(text)
end
if lang == 'en' then
return p._translate2en(text)
end
return text
end
function p.translate2bn(frame)
local text = frame.args[1] or frame:getParent().args[1]
return p._translate2bn(text)
end
function p.translate2en(frame)
local text = frame.args[1] or frame:getParent().args[1]
return p._translate2en(text)
end
function p.convert_template(frame)
local args = frame.args
local pargs = frame:getParent().args
local lang = args[1] or pargs[1]
local text = args[2] or pargs[2]
return p.convert(lang, text)
end
return p