forked from Liquipedia/Lua-Modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.lua
More file actions
217 lines (185 loc) · 6.23 KB
/
Copy pathflags.lua
File metadata and controls
217 lines (185 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
---
-- @Liquipedia
-- wiki=commons
-- page=Module:Flags
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--
local MasterData = mw.loadData('Module:Flags/MasterData')
local Template = require('Module:Template')
local Logic = require('Module:Logic')
local Table = require('Module:Table')
local String = require('Module:StringUtils')
local FnUtil = require('Module:FnUtil')
local Class = require('Module:Class')
local Flags = {}
-- Returns a flag
--[[
supported args are:
flag - country name, flag code, or alias of the Flag
shouldLink - boolean that decides if the flag should link or not
--> set this to true for the flag to link to the according category
--> else the flag will not link
]]--
function Flags.Icon(args, flagName)
local shouldLink
if type(args) == 'string' then
flagName = args
elseif type(args) == 'table' then
shouldLink = args.shouldLink
if String.isEmpty(flagName) then
flagName = args.flag
end
end
if String.isEmpty(flagName) then
return ''
end
shouldLink = Logic.readBool(shouldLink)
local flagKey = Flags._convertToKey(flagName)
if flagKey then
local flagData = MasterData.data[flagKey]
if flagData.flag ~= 'File:Space filler flag.png' then
local link = ''
if flagData.name and shouldLink then
link = 'Category:' .. flagData.name
end
return '<span class="flag">[[' .. flagData.flag ..
'|' .. flagData.name .. '|link=' .. link .. ']]</span>'
else
return '<span class="flag">[[' .. flagData.flag .. '|link=]]</span>'
end
elseif shouldLink then
mw.log('Unknown flag: ', flagName)
return Template.safeExpand(mw.getCurrentFrame(), 'Flag/' .. flagName:lower()) ..
'[[Category:Pages with unknown flags]]'
else
mw.log('Unknown flag: ', flagName)
return Template.safeExpand(mw.getCurrentFrame(), 'FlagNoLink/' .. flagName:lower()) ..
'[[Category:Pages with unknown flags]]'
end
end
-- Returns the localisation/country-adjective of a country or region
-- @country - country name, flag code, or alias of the Flag
-- examples:
-- Flags.getLocalisation('de') = German
-- Flags.getLocalisation('Germany') = German
-- Flags.getLocalisation('deu') = German
function Flags.getLocalisation(country)
if String.isEmpty(country) then
return
end
-- clean the entered value
local countryKey = Flags._convertToKey(country)
if countryKey then
local data = MasterData.data[countryKey]
if String.isNotEmpty(data.localised) then
return data.localised
end
end
-- Return message if none is found
mw.log('Unknown localisation entry: ', country)
return nil, 'Unknown localisation entry "[[lpcommons:Module:Flags/MasterData' ..
'|' .. country .. ']]"[[Category:Pages with unknown countries]]'
end
-- supported args are:
-- @country - country name, flag code, or alias of the Flag
-- @hideError - boolean that decides if there should be a displayed error
-- if no entry is found but a country input was made
-- @simpleError - boolean that decides if displayed error should be simple or detailed
function Flags.localisationTemplate(args)
args = args or {}
local display, error = Flags.getLocalisation(args.country)
if not error or Logic.readBool(args.hideError) then
return display
end
if Logic.readBool(args.simpleError) then
return 'error'
end
return error
end
function Flags.languageIcon(args, langName)
if type(args) == 'string' then
langName = args
args = {}
elseif String.isEmpty(langName) then
langName = args.language or args.flag
end
if String.isEmpty(langName) then
return ''
end
langName = Flags._convertToLangKey(langName)
return Flags.Icon(args, langName)
end
-- Converts a country name, flag code, or alias to a standardized country name
function Flags.CountryName(flagName)
if String.isEmpty(flagName) then
return ''
end
local flagKey = Flags._convertToKey(flagName)
if flagKey then
return MasterData.data[flagKey].name
else
mw.log('Unknown flag: ', flagName)
return mw.text.trim(mw.text.split(Template.safeExpand(mw.getCurrentFrame(), 'Flag/' .. flagName), '|', true)[2] or '')
end
end
-- Converts a country name, flag code, or alias to its iso code
--[[
supported formats are:
alpha2 - returns the lowercase ISO 3166-1 alpha-2 flag code
alpha3 - returns the lowercase ISO 3166-1 alpha-3 flag code
default is alpha2
]]--
function Flags.CountryCode(flagName, format)
if String.isEmpty(flagName) then
return ''
end
local flagKey = Flags._convertToKey(flagName)
if flagKey then
if format == 'alpha3' then
return Flags._getAlpha3CodesByKey()[flagKey] or Flags._getLanguage3LetterCodesByKey()[flagKey]
else
flagKey = MasterData.iso31662[flagKey] or flagKey
return Flags._getAlpha2CodesByKey()[flagKey] or Flags._getLanguageCodesByKey()[flagKey]
end
end
mw.log('Unknown flag: ', flagName)
return ''
end
Flags._getAlpha2CodesByKey = FnUtil.memoize(function()
return Table.map(MasterData.twoLetter, function(key, code) return code, key end)
end)
Flags._getAlpha3CodesByKey = FnUtil.memoize(function()
return Table.map(MasterData.threeLetter, function(key, code) return code, key end)
end)
Flags._getLanguageCodesByKey = FnUtil.memoize(function()
return Table.map(MasterData.languageTwoLetter, function(key, code) return code, key end)
end)
Flags._getLanguage3LetterCodesByKey = FnUtil.memoize(function()
return Table.map(MasterData.languageThreeLetter, function(key, code) return code, key end)
end)
--[[
Converts a country name, flag code, or alias to a canonical key. The key is a
lower case no-space representation of the country name, and is used for other
functions in this module. Returns nil if the input is unrecognized.
Examples:
Flags.readKey('tn') -- returns 'tunisia'
Flags.readKey('tun') -- returns 'tunisia'
Flags.readKey('tunisia') -- returns 'tunisia'
Flags.readKey('Antigua and Barbuda') -- returns 'antiguaandbarbuda'
Flags.readKey('Czech Republic') -- returns 'czechia'
Flags.readKey('Czechoslovakia') -- returns nil
]]
function Flags._convertToKey(flagName)
flagName = flagName:gsub(' ', ''):lower()
return MasterData.twoLetter[flagName]
or MasterData.threeLetter[flagName]
or MasterData.aliases[flagName]
or (MasterData.data[flagName] and flagName)
end
function Flags._convertToLangKey(langName)
return MasterData.languageTwoLetter[langName]
or MasterData.languageThreeLetter[langName]
or langName
end
return Class.export(Flags)