Module:ISO 639 name/ISO 639 name to code/make: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 5:
--[[--------------------------< A D D _ L A N G >--------------------------------------------------------------
 
temp table is a table of tables where the key is the language name and the value is a 3-element table listing the ISO 639
the ISO 639 codes associated with that language name.
 
This function adds language name (as index) and its code (as a table element) tousing thean appropriate placeindex in the temp tablenumber.
 
<lang> is the language name from the source data
<code> is the associated ISO 639 code from the source data
<part> is 1 for ISO 639-1 language names and codes, 2, ...2B, 3 ..., 5 ... Note<part> thatfor partthe 5override codesdata gois inprefixed indexwith [4]'O'
 
This function does not create alias entries in temp table for those language names that use characters with diacritics.
To do so risks conflict between names that do not use diacritics (Bari, code bfa) and names that do (Barí, code mot).
 
]]
 
local function lang_add (lang, code, part)
part = ({ -- convert string <part> to a numeric index
part = ({ ['1']=1, ['2']=2, ['2B']=3, ['3']=4, ['5']=5, -- })[part];for the base ISO 639 parts
['O1']=6, ['O2']=7, ['O2B']=8, ['O3']=9, ['O5']=10, -- for the override tables
})[part];
 
lang = mw.ustring.lower (lang); -- convert to lowercase for use as table index
 
if not temp[lang] then -- when no entry for this language
temp[lang] = {}; -- make a blank entry: 1, 2, 2B, 3, 5
end
 
Line 31:
'[', -- open the key
part, -- add the index
']=\"', -- close key, add assignment operator and open quote beausebecause these<code> areis stringsa string
code, -- add the <code>
'\"' -- close the string quote
}));
end
Line 60:
end
end
 
part_data = mw.loadData ('Module:Language/data/ISO 639-2'); -- ISO 639-2 language codes / names
for code, v in pairs (part_data) do -- now part 2
Line 67:
end
end
 
part_data = mw.loadData ('Module:Language/data/ISO 639-2B'); -- ISO 639-2B language codes / names
for code, v in pairs (part_data) do -- now part 2B
Line 74:
end
end
 
part_data = mw.loadData ('Module:Language/data/iana languages'); -- used only for ISO 639-1 language codes / names');
for code, v in pairs (part_data) do -- now part 1
Line 80:
for _, lang in ipairs (v) do
lang_add (lang, code, '1');
end
end
end
 
part_data = mw.loadData ('Module:Language/data/ISO 639 override'); -- has override data for all parts
for _, o_part in ipairs ({'1', '2', '2B', '3', '5'}) do -- for each of the override tables
local o_part_data = part_data['override_' .. o_part]; -- point to override data
o_part = 'O' .. o_part; -- prefix o_part
for code, v in pairs (o_part_data) do -- for each code in the data table
for _, lang in ipairs (v) do -- and for each language name associated with that code
lang_add (lang, code, o_part); -- add
end
end
Line 89 ⟶ 100:
end
 
table.sort (out); -- sort in language name order
 
local key_str = '--[[Key:<br />&#9;[1]=ISO 639-1<br />&#9;[2]=ISO 639-2<br />&#9;[3]=ISO 639-2B<br />&#9;[4]=ISO 639-3<br />&#9;[5]=ISO 639-5<br />]]<br /><br />'
local key_str = table.concat ({
return table.concat ({'<pre>', key_str, 'return {<br />&#9;', table.concat (out, ',<br />&#9;'), '<br />&#9;}<br /></pre>'}); -- render
'--[[--------------------------< I S O _ 6 3 9 _ N A M E _ T O _ C O D E >--------------------------------------<br /><br />',
'Key:<br />&#9;',
'[1]=ISO 639-1&#9;&#9;[6]=ISO 639-1 override<br />&#9;',
'[2]=ISO 639-2&#9;&#9;[7]=ISO 639-2 override<br />&#9;',
'[3]=ISO 639-2B&#9;&#9;[8]=ISO 639-2B override<br />&#9;',
'[4]=ISO 639-3&#9;&#9;[9]=ISO 639-3 override<br />&#9;',
'[5]=ISO 639-5&#9;&#9;[10]=ISO 639-5 override',
'<br />]]<br /><br />'
})
return table.concat ({'<pre>', key_str, 'return {<br />&#9;', table.concat (out, ',<br />&#9;'), '<br />&#9;}<br /></pre>'}); -- render
end
 
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
 
return {iso_639_name_to_code = iso_639_name_to_code}