-- Base object for the Locale
-- Represents a card's details in a particular language
--
-- Unless otherwise stated, string params are in the language in question
-- @field lang string Two-letter code for the language
-- @field name string The card's name
-- @field romanizedName string The card's name, romanized
-- @field translatedName string The card's name, translated into English
-- @field pendulumEffect string The card's Penudlum Effect
-- @field material string The material portion of the card's lore
-- @field lore string The card description/effect portion of the lore (Should not include italic markup)
-- @field archetypeCondition string The archetype condition portion of the lore (Should not include parentheses)
-- @field italicLore boolean If the card description uses italics
local Locale = {
lang = nil,
name = nil,
romanizedName = nil,
translatedName = nil,
pendulumEffect = nil,
material = nil,
lore = nil,
archetypeCondition = nil,
italicLore = false
}
-- Create a new instance of the Locale object
-- @param lang string The language code
-- @param params table
-- @param isNormalMonster boolean
-- @return Locale
function Locale:new(lang, params, isNormalMonster)
-- Create a new instance of the Locale class with all the default values
local l = {}
setmetatable(l, Locale)
Locale.__index = Locale
-- English parameters don't have a prefix e.g. `name`, `lore`
-- Other languages use their code as their prefix e.g. `ja_name`, `ja_lore`
local prefix = lang == 'en' and '' or lang .. '_'
-- Check if language is Chinese/Japanese/Korean
local isCjk = TableTools.inArray({'zh', 'tc', 'sc', 'ja', 'ko'}, lang)
l.lang = lang
l.name = params[prefix .. 'name'] or nil
l.romanizedName = params[prefix .. 'romanized_name'] or nil
l.translatedName = params[prefix .. 'translated_name'] or nil
l.pendulumEffect = params[prefix .. 'pendulum_effect'] or nil
l.material = params[prefix .. 'material'] or nil
l.lore = params[prefix .. 'lore'] or nil
l.archetypeCondition = params[prefix .. 'archetype_condition'] or nil
-- Normal Monsters in non-Asian scripts use italics in their lores
l.italicLore = isNormalMonster and not isCjk
return l
end
-- Create many instances of the Locale object
-- @param langs array List of language codes
-- @param params table
-- @param isNormalMonster boolean
-- @return table Mapping of language codes to Locale objects
function Locale:createMany(langs, params, isNormalMonster)
params = Locale.normalizeParams(params)
-- List of all Locale objects for the card
local locales = {}
for _, lang in pairs(langs) do
-- Only add to the list if there is data for this language
if (Locale.hasParams(lang, params)) then
locales[lang] = self:new(lang, params, isNormalMonster)
end
end
return locales
end
-- Normalize the input params
-- Relable params to use a consistent format across all languages.
-- e.g. "romaji_name", "ko_rr_name" → "ja_romanized_name", "ko_romanized_name"
-- @param params table
function Locale.normalizeParams(params)
-- table mapping labels to standardized ones
local mappings = {
romaji_name = 'ja_romanized_name',
trans_name = 'ja_translated_name',
ko_rr_name = 'ko_romanized_name'
}
-- Loop through the mappings
-- If the old label is used in params, also its value under the new label
for inputParam, normalizedParam in pairs(mappings) do
if params[inputParam] then
params[normalizedParam] = params[inputParam]
end
end
return params
end
-- Check if a supplied input has params in a specified language
-- @param lang string
-- @param params table - Input params (after normalization)
-- @return boolean
function Locale.hasParams(lang, params)
-- There should always be an English
if lang == 'en' then return true end
-- Check if at least one param is supplied for the language
return (
params[lang .. '_name'] or
params[lang .. '_lore'] or
params[lang .. '_pendulum_effect'] or
params[lang .. '_material'] or
params[lang .. '_archetype_condition'] or
params[lang .. '_romanized_name'] or
params[lang .. '_translated_name']
) and true or false
end
-- Put the material, lore and archetype condition together to form the full lore
-- @return string
function Locale:getFullLore()
local output = self.material or ''
-- Add the effect text/card description
if (self.lore) then
-- Add a line break if there is already text in the lore
if (output ~= '') then
output = output .. '<br />'
end
-- This part of the lore may or may not contain italics.
if (self.italicLore) then
output = output .. '<i>' .. self.lore .. '</i>'
else
output = output .. self.lore
end
end
if (self.archetypeCondition) then
-- If there is text before the archetype condition, add line breaks
if (output ~= '') then
output = output .. '<br /><br />'
end
output = output .. '(' .. self.archetypeCondition .. ')'
end
return output
end
return Locale