Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions lua/wikis/starcraft2/Widget/TranslationList.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
-- @Liquipedia
-- page=Module:Widget/TranslationList
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Lua = require('Module:Lua')

local Array = Lua.import('Module:Array')
local Class = Lua.import('Module:Class')
local Flags = Lua.import('Module:Flags')
local Logic = Lua.import('Module:Logic')
local Operator = Lua.import('Module:Operator')

local Box = Lua.import('Module:Widget/Basic/Box')
local UnorderedList = Lua.import('Module:Widget/List/Unordered')
local Widget = Lua.import('Module:Widget')

local COLUMN_BREAK = 6

---@class TranslationList: Widget
---@operator call(table): TranslationList
local TranslationList = Class.new(Widget)

---@return Widget?
function TranslationList:render()
---@param item {flag: string, value: string, postFix: string?}
---@return string
local displayItem = function(item)
return Flags.Icon{flag = item.flag} .. (item.postFix or ' ') .. item.value
end

---@type Widget[]
local parts = Array.map(self:_parse(), function(group)
return UnorderedList{children = Array.map(group, displayItem)}
end)

return Box{children = parts}
end

---@private
---@return {flag: string, value: string, postFix: string?}[][]
function TranslationList:_parse()
local data = Array.map(Array.extractKeys(self.props), function(key)
if key == 'children' then
return
elseif key == 'simpliefiedChinese' or key == 'chineseSimplified' then
return {flag = 'cn', value = self.props[key], postFix = ' (simplified) '}
elseif key == 'traditionalChinese' or key == 'chineseTraditional' then
return {flag = 'cn', value = self.props[key], postFix = ' (traditional) '}
end
return {
flag = assert(Logic.nilIfEmpty(Flags.CountryCode{flag = key}), 'Unsupported parameter: ' .. key),
value = self.props[key],
}
end)

Array.sortInPlaceBy(data, Operator.property('flag'))

local groupedData = {}
Array.forEach(data, function(item, index)
if index % COLUMN_BREAK == 1 then
table.insert(groupedData, {})
end
table.insert(groupedData[#groupedData], item)
end)

return groupedData
end

return TranslationList
Loading