|
|
1行目: |
1行目: |
| local p = {}
| |
|
| |
| local langlist = require('Module:Fallbacklist')
| |
|
| |
|
| |
| --[[ | | --[[ |
| _langSwitch
| | __ __ _ _ _____ _ _ _ _ |
| | | \/ | ___ __| |_ _| | ___ _| ___|_ _| | | |__ __ _ ___| | __ |
| | | |\/| |/ _ \ / _` | | | | |/ _ (_) |_ / _` | | | '_ \ / _` |/ __| |/ / |
| | | | | | (_) | (_| | |_| | | __/_| _| (_| | | | |_) | (_| | (__| < |
| | |_| |_|\___/ \__,_|\__,_|_|\___(_)_| \__,_|_|_|_.__/ \__,_|\___|_|\_\ |
| | | |
| This function is the core part of the LangSwitch template.
| | Authors and maintainers: |
| | | * User:Zolo - original version |
| Example usage from Lua:
| | * User:Jarekt |
| text = _langSwitch({en='text in english', pl='tekst po polsku'}, lang)
| |
|
| |
| Parameters:
| |
| args - table with translations by language
| |
| lang - desired language (often user's native language) | |
| | |
| Error Handling:
| |
| | |
| ]] | | ]] |
|
| |
|
| function p._langSwitch(args, lang) -- args: table of translations
| | local p = {} |
| -- Return error if there is not default and no english version
| |
| if not args.en and not args.default then
| |
| if args.nocat == '1' then
| |
| return '<strong class="error">LangSwitch Error: no default</strong>'
| |
| else
| |
| return '<strong class="error">LangSwitch Error: no default</strong>[[Category:LangSwitch template without default version]]'
| |
| end
| |
| end
| |
| -- get language (either stated one or user's default language)
| |
| if not lang then
| |
| return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error
| |
| end
| |
| -- get the list of accepetable language (lang + those in lang's fallback chain) and check their content
| |
| local langList = mw.language.getFallbacksFor(lang)
| |
| table.insert(langList,1,lang)
| |
| table.insert(langList, 'default')
| |
| for _, language in ipairs(langList) do
| |
| if args[language ] == '~' then
| |
| return ''
| |
| elseif args[language] and args[language] ~= '' then
| |
| return args[language]
| |
| end
| |
| end
| |
| end
| |
| | |
| --[[
| |
| langSwitch
| |
|
| |
| This function is the core part of the LangSwitch template.
| |
|
| |
| Example Usage from a template:
| |
| {{#invoke:fallback|langSwitch|en=text in english|pl=tekst po polsku|lang={{int:lang}} }}
| |
|
| |
| Parameters:
| |
| frame.args - table with translations by language
| |
| frame.args.lang - desired language (often user's native language)
| |
| | |
| Error Handling:
| |
| | |
| ]]
| |
| function p.langSwitch(frame) -- version to be used from wikitext
| |
| args = frame.args
| |
| -- if no expected args provided than check parent template/module args
| |
| if args.en==nil and args.default==nil and args.nocat==nil then
| |
| args = mw.getCurrentFrame():getParent().args
| |
| end
| |
| if args.lang and args.lang ~= '' then
| |
| lang = args.lang
| |
| else -- get user's chosen language
| |
| lang = frame:callParserFunction( "int", "lang" )
| |
| end
| |
| args.lang = nil
| |
| return p._langSwitch(args, lang)
| |
| end
| |
| | |
| --[[
| |
| autotranslate
| |
|
| |
| This function is the core part of the Autotranslate template.
| |
|
| |
| Usage from a template:
| |
| {{#invoke:fallback|autotranslate|base=|lang= }} | |
|
| |
| Parameters:
| |
| frame.args.base - base page name
| |
| frame.args.lang - desired language (often user's native language)
| |
| | |
| Error Handling:
| |
| | |
| ]]
| |
| function p.autotranslate(frame)
| |
| local args = frame.args
| |
| if not args.lang or args.lang == '' then
| |
| args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
| |
| end
| |
| local langList = mw.language.getFallbacksFor(args.lang)
| |
| table.insert(langList,1,args.lang)
| |
|
| |
| -- find base page
| |
| local base = args.base
| |
| if not base or base == '' then
| |
| return '<strong class="error">Base page not provided for autotranslate</strong>'
| |
| end
| |
| if not mw.ustring.find(base,':') then -- if base page does not indicate namespace
| |
| base = 'Template:' .. base -- than assume it is a template
| |
| end
| |
|
| |
| -- find base template language subpage
| |
| local page = nil
| |
| for _, language in ipairs(langList) do
| |
| if mw.title.new(base .. '/' .. language).exists then
| |
| page = base .. '/' .. language -- returns only the page
| |
| break
| |
| end
| |
| end
| |
| if not page then
| |
| return string.format('<strong class="error">no fallback page found for autotranslate (base=[[%s]], lang=%s)</strong>', args.base, args.lang)
| |
| end
| |
|
| |
| -- repack args in a standard table
| |
| local inArgs = {}
| |
| for field, value in pairs(args) do
| |
| inArgs[field] = value;
| |
| end
| |
|
| |
| -- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
| |
| inArgs.base = nil
| |
| return frame:expandTemplate{ title = page, args = inArgs }
| |
| end
| |
|
| |
|
| --[[ | | --[[ |
141行目: |
23行目: |
| Parameters: | | Parameters: |
| frame.args.1 - name of translation module | | frame.args.1 - name of translation module |
| frame.args.2 - field name of the structure in Module:[frame.args.1] to use
| | frame.args.2 - field name of the structure in Module:[frame.args.1] to use |
| frame.args.lang - desired language (often user's native language) | | frame.args.lang - desired language (often user's native language) |
| | | |
| Error Handling: | | Error Handling: |
| | | |
| ]] | | ]] |
| function p.translatelua(frame) | | function p.translatelua(frame) |
| local lang = frame.args.lang | | local lang = mw.text.trim(frame.args.lang) |
| local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations | | local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations |
| if not lang or mw.text.trim(lang) == '' then | | if not lang or lang == '' then |
| lang = frame:callParserFunction( "int", "lang" ) | | lang = frame:callParserFunction("int", "lang") |
| end | | end |
| if frame.args[2] then | | if frame.args[2] then |
| page = page[mw.text.trim(frame.args[2])] | | page = page[mw.text.trim(frame.args[2])] |
| end | | end |
| return p._langSwitch(page, lang) | | |
| | local langList = mw.language.getFallbacksFor(lang) |
| | table.insert(langList,1,lang) |
| | for _,language in ipairs(langList) do |
| | if page[language] then |
| | return page[language] |
| | end |
| | end |
| end | | end |
|
| |
|
166行目: |
55行目: |
| Parameters: | | Parameters: |
| lang - desired language (often user's native language) | | lang - desired language (often user's native language) |
| | | |
| Error Handling: | | Error Handling: |
| | | |
| ]] | | ]] |
| function p.fblist(lang) -- list the full fallback chain from a language to en | | function p.fblist(lang) -- list the full fallback chain from a language to en |
| local fbtable = p.fallbackloop{ lang:lower() } | | local fbtable = p.fallbackloop{lang:lower()} |
| table.insert(fbtable, 'default') | | table.insert(fbtable, 'default') |
| table.insert(fbtable, 'en') | | table.insert(fbtable, 'en') |
177行目: |
66行目: |
| end | | end |
|
| |
|
| function _inArray(x, t) | | local function _inArray(x, t) |
| for i, v in ipairs(t) do | | for i, v in ipairs(t) do |
| if v == x then return i end | | if v == x then return i end |
184行目: |
73行目: |
| end | | end |
|
| |
|
| function p.fallbackloop(fbtable) --list of fallback languages in string format (more convenient than tables) | | function p.fallbackloop(fbtable) --list of fallback languages in string format (more convenient than tables) |
| | local langlist = require('Module:Fallbacklist') |
| local changes = false | | local changes = false |
| for i, j in ipairs(fbtable) do | | for i, j in ipairs(fbtable) do |