Module:Nihongo
The Nihongo module serves as an organized way to properly format proper nouns and titles that come from Japanese origin. This means it must be used for character names, locations, game titles, book titles, etc. etc..
Credit goes to Vish from the Jojo's Bizarre Encyclopedia wiki for the original code.
Usage
{{nihongo|English|Kanji|Romaji|extra|extra2|type1=|lineBreak=|noParenthesis=|hover=}}
Default:
English (Kanji, Romaji, extra) extra2
With type1=J:
Kanji (Romaji, extra) extra2
With type1=J and blank Romaji:
Kanji
With type1=R:
Romaji (Kanji, extra) extra2
With type1=R and blank Kanji:
Romaji
local p = {}
-- Main function that generates the Nihongo text
function p.nihongo(frame)
-- Retrieve the arguments passed to the template
local args = frame:getParent().args
-- Helper function to create a span element with the given content and style
local function getSpan(content, style)
if content == "" then return "" end
return string.format('<span %s>%s</span>', style, content)
end
local function escapeQuotes(str)
if not str then return str end
str = str:gsub('"', """)
return str
end
-- Helper function to determine the style based on the type argument
local function switchType(type, hover, hovertext)
if type == "J" then
if hover and hover ~= '' and hovertext ~= '' then
return string.format('span class="popup nihongo-japanese" data-content="%s"', escapeQuotes(hovertext))
else
return 'lang="ja" style="font-family:Meiryo; display:inline;" class="nihongo-japanese"'
end
elseif type == "R" then
return 'style="font-style:italic; font-weight:normal;" class="nihongo-romaji"'
else
return 'class="nihongo-english"'
end
end
-- Determine the styles for each type
local type1Style = switchType(args.type1 or 'E', args.hover, args[2] or '')
local type2Style = switchType(args.type2 or 'J', args.hover, args[3] or '')
local type3Style = switchType(args.type3 or 'R', args.hover, args[4] or '')
if args.type1 == 'J' then
if args.hover and args.hover ~= '' then
args[5] = args[4] or ''
args[4] = ''
args[2] = args[3] or ''
args[3] = ''
type2Style = type3Style
else
args[5] = args[4] or ''
args[4] = ''
type2Style = switchType(args.type2 or 'R', args.hover, args[3] or '')
end
elseif args.type1 == 'R' then
args[5] = args[4] or ''
args[4] = ''
end
-- Initialize the result string
local result = '<span class="nihongo popup-wrapper">'
if args[1] and args[1] ~= '' then
-- Add the first argument
result = result .. getSpan(args[1], type1Style)
end
if args[2] and args[2] ~= '' then
-- Add line break if requested
if args.lineBreak and args.lineBreak ~= '' then
result = result .. '<br />'
end
result = result .. '<span class="noexcerpt">'
-- Add parenthesis if not excluded
if not args.noParenthesis or args.noParenthesis == '' then
if args[1] and args[1] ~= '' then
result = result .. '<span class="nihongo-bracket"> (</span>'
else
result = result .. '<span class="nihongo-bracket">(</span>'
end
else
result = result .. ' '
end
-- Add the second argument
result = result .. getSpan(args[2], type2Style)
-- Add the third argument if hover is excluded
if args[3] and args[3] ~= '' and not args.hover or args.hover == '' then
result = result .. ', '
result = result .. ' '
result = result .. getSpan(args[3], type3Style)
end
-- Add the fourth (first extra) argument
if args[4] and args[4] ~= '' then
result = result .. ', '
result = result .. args[4]
end
-- Close parenthesis if applicable
if not args.noParenthesis or args.noParenthesis == '' then
result = result .. '<span class="nihongo-bracket">)</span>'
end
-- Add the fifth (second extra) argument
if args[5] and args[5] ~= '' then
result = result .. ' '
result = result .. args[5]
end
-- Close span tags
result = result .. '</span>'
end
result = result .. '</span>'
return result
end
return p