Difference between revisions of "Module:Language/data/ISO 639 name to code/make"
< Module:Language | data | ISO 639 name to code
Jump to navigation
Jump to search
blackwiki>Trappist the monk |
blackwiki>Trappist the monk |
||
| Line 12: | Line 12: | ||
lang is the language name from the source data | lang is the language name from the source data | ||
code is the associated ISO 639 code 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 ..., 3 ... | + | part is 1 for ISO 639-1 language names and codes, 2 ..., 3 ..., 5 ... Note that part 5 codes go in index [4] |
This function does not create alias entries in temp table for those language names that use characters with diacritics. | This function does not create alias entries in temp table for those language names that use characters with diacritics. | ||
| Line 21: | Line 21: | ||
local function lang_add (lang, code, part) | local function lang_add (lang, code, part) | ||
local count; | local count; | ||
| + | |||
| + | if 5 == part then | ||
| + | part = 4; -- there is no current 639-4 map part 5 codes into temp[lang][4] | ||
| + | end | ||
lang = mw.ustring.lower (lang); -- convert to lowercase for use as table index | lang = mw.ustring.lower (lang); -- convert to lowercase for use as table index | ||
if not temp[lang] then -- when no entry for this language | if not temp[lang] then -- when no entry for this language | ||
| − | temp[lang] = {'""', '""', '""'}; | + | temp[lang] = {'""', '""', '""', '""'}; -- make a blank entry |
end | end | ||
| Line 37: | Line 41: | ||
]] | ]] | ||
| + | |||
local function iso_639_name_to_code () | local function iso_639_name_to_code () | ||
local out = {}; | local out = {}; | ||
| Line 43: | Line 48: | ||
local part2_data = mw.loadData ('Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-2'); -- ISO 639-2 language codes / names; to be moved to Module:Language/data/ISO 639-2 | local part2_data = mw.loadData ('Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-2'); -- ISO 639-2 language codes / names; to be moved to Module:Language/data/ISO 639-2 | ||
local part3_data = mw.loadData ('Module:Language/data/ISO 639-3'); -- existing data module | local part3_data = mw.loadData ('Module:Language/data/ISO 639-3'); -- existing data module | ||
| + | local part5_data = mw.loadData ('Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-5'); -- ISO 639-5 language codes / names; to be moved to Module:Language/data/ISO 639-5 | ||
for code, v in pairs (part3_data) do -- start with part 3 because it has the most codes | for code, v in pairs (part3_data) do -- start with part 3 because it has the most codes | ||
| Line 50: | Line 56: | ||
end | end | ||
| + | for code, v in pairs (part5_data) do -- now part 5 | ||
| + | for _, lang in ipairs (v) do | ||
| + | lang_add (lang, code, 5); | ||
| + | end | ||
| + | end | ||
| + | |||
for code, v in pairs (part2_data) do -- now part 2 | for code, v in pairs (part2_data) do -- now part 2 | ||
for _, lang in ipairs (v) do | for _, lang in ipairs (v) do | ||
Revision as of 23:13, 27 September 2018
| 30px | This module depends on the following other modules: |
Usage
{{#invoke:Language/data/ISO 639 name to code/make|iso_639_name_to_code}}
Results are at Module talk:Language/data/ISO 639 name to code/make
require('Module:No globals');
local temp = {};
--[[--------------------------< 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 codes associated with that language name.
This function adds language name (as index) and its code (as a table element) to the appropriate place in the temp table.
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 ..., 3 ..., 5 ... Note that part 5 codes go in index [4]
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)
local count;
if 5 == part then
part = 4; -- there is no current 639-4 map part 5 codes into temp[lang][4]
end
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
end
temp[lang][part] = table.concat ({'"', code, '"'}); -- add the code
end
--[[--------------------------< I S O 6 3 9 _ N A M E _ T O _ C O D E >----------------------------------------
read code-to-name source tables and convert to a name-to-code table.
]]
local function iso_639_name_to_code ()
local out = {};
local part1_data = mw.loadData ('Module:Language/data/iana languages'); -- used only for ISO 639-1 language codes / names
local part2_data = mw.loadData ('Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-2'); -- ISO 639-2 language codes / names; to be moved to Module:Language/data/ISO 639-2
local part3_data = mw.loadData ('Module:Language/data/ISO 639-3'); -- existing data module
local part5_data = mw.loadData ('Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-5'); -- ISO 639-5 language codes / names; to be moved to Module:Language/data/ISO 639-5
for code, v in pairs (part3_data) do -- start with part 3 because it has the most codes
for _, lang in ipairs (v) do -- code can have multiple names so for each one
lang_add (lang, code, 3); -- create and / or add this name / code pair to the output
end
end
for code, v in pairs (part5_data) do -- now part 5
for _, lang in ipairs (v) do
lang_add (lang, code, 5);
end
end
for code, v in pairs (part2_data) do -- now part 2
for _, lang in ipairs (v) do
lang_add (lang, code, 2);
end
end
for code, v in pairs (part1_data) do -- now part 1
if 2 == #code then -- IANA source data includes a mix of 2- and 3-character codes; ISO 639-1 is the 2-character variety
for _, lang in ipairs (v) do
lang_add (lang, code, 1);
end
end
end
for lang, codes in pairs (temp) do
table.insert (out, table.concat ({'["', lang, '"] = {', table.concat (codes, ', '), '}'})); -- reformat
end
table.sort (out);
return table.concat ({"<pre>return {<br />	", table.concat (out, ',<br />	'), "<br />	}<br /></pre>"}); -- render
end
return {iso_639_name_to_code = iso_639_name_to_code}