এই মডিউলের জন্য মডিউল:NationAndOccupation/নথি-এ নথিপত্র তৈরি করা হয়ে থাকতে পারে

--[[  
  __  __           _       _        _   _       _   _                _              _  ___                             _   _             
 |  \/  | ___   __| |_   _| | ___ _| \ | | __ _| |_(_) ___  _ __    / \   _ __   __| |/ _ \  ___ ___ _   _ _ __   __ _| |_(_) ___  _ __  
 | |\/| |/ _ \ / _` | | | | |/ _ (_)  \| |/ _` | __| |/ _ \| '_ \  / _ \ | '_ \ / _` | | | |/ __/ __| | | | '_ \ / _` | __| |/ _ \| '_ \ 
 | |  | | (_) | (_| | |_| | |  __/_| |\  | (_| | |_| | (_) | | | |/ ___ \| | | | (_| | |_| | (_| (__| |_| | |_) | (_| | |_| | (_) | | | |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)_| \_|\__,_|\__|_|\___/|_| |_/_/   \_\_| |_|\__,_|\___/ \___\___|\__,_| .__/ \__,_|\__|_|\___/|_| |_|

 
This module translates a person’s nationality and profession into user’s preferred language. 
The template takes care for the right word order: {{NationAndOccupation|m|FR|painter|poet}} 
gives “French painter and poet”, if the user’s preferred language is set to English, but 
“pintor y poeta francés”, if the language is set to Spanish. This is especially useful with 
the “Description” field of {{Creator}} templates.

]]

local function linguistic_conj(args, lang, conjtype)
	local comma    = mw.message.new( "comma-separator"):inLanguage(lang):plain()
	local wordsep  = mw.message.new( "Word-separator" ):inLanguage(lang):plain()
	local andtable = { ar=' و', he=' ו', ja='および', pl=' i '} -- languages with a problem with the MediaWiki:And
	local sep1, sep2 = comma, nil
	if conjtype == 'comma' then
		sep2 = comma
	elseif conjtype == 'or' or conjtype == 'explicit or' then 
		local wordor = 'অথবা'
		sep2 = wordsep .. wordor  .. wordsep -- pad it with spaces (if needed)
		sep1 = (conjtype == 'explicit or' and sep2) or comma -- adds "or" betwen all words when the context can be confusing
	elseif conjtype and conjtype ~= 'and' and conjtype ~= '' then
		sep1, sep2 = conjtype, conjtype
	elseif andtable[lang] then 
		sep2 = andtable[lang]
	else
		sep2 = mw.message.new( "and" ):inLanguage(lang):plain() .. wordsep
	end
	return mw.text.listToText(args, sep1, sep2)

end

-- =======================================
-- === Dependencies ======================
-- =======================================
local core  = require("Module:core")
local q2iso = {
	Q228      = 'AD',	  --Andorra
	Q878      = 'AE',	  --United Arab Emirates
	Q889      = 'AF',	  --Afghanistan
	Q781      = 'AG',	  --Antigua and Barbuda
	Q25228    = 'AI',	  --Anguilla
	Q222      = 'AL',	  --Albania
	Q179248   = 'AL',	  --Albanians
	Q1242155  = 'AL',	  --Albanians in Kosovo
	Q399      = 'AM',	  --Armenia
	Q79797    = 'AM',	  --Armenians
	Q2590074  = 'AM',	  --Armenians in Turkey
	Q2325516  = 'AM/US',	  --Armenian American
	Q25227    = 'AN',	  --Netherlands Antilles
	Q916      = 'AO',	  --Angola
	Q21590062 = 'AQ',	  --Antarctic Treaty area
	Q414      = 'AR',	  --Argentina
	Q35323    = 'arab',	  --Arab
	Q485732   = 'arab',	  --Quraysh
	Q16641    = 'AS',	  --American Samoa
	Q699964   = 'AT',	  --Archduchy of Austria
	Q40       = 'AT',	  --Austria
	Q131964   = 'AT',	  --Austrian Empire
	Q237534   = 'AT',	  --Austrians
	Q518101   = 'AT',	  --First Austria
	Q324216   = 'AT/US',	  --Austrian American
	Q408      = 'AU',	  --Australia
	Q1318423  = 'AU',	  --Australians
	Q170355   = 'AU',	  --indigenous Australians
	Q28513    = 'austro–hungarian',	  --Austria-Hungary
	Q21203    = 'AW',	  --Aruba
	Q5689     = 'AX',	  --Ã…land Islands
	Q227      = 'AZ',	  --Azerbaijan
	Q482942   = 'AZ',	  --Azerbaijani people
	Q225      = 'BA',	  --Bosnia and Herzegovina
	Q940348   = 'BA',	  --Bosniaks
	Q165235   = 'BA',	  --Bosnians
	Q126756   = 'basque',	  --Basque people
	Q2886395  = 'basque/US',	  --Basque American
	Q244      = 'BB',	  --Barbados
	Q902      = 'BD',	  --Bangladesh
	Q31       = 'BE',	  --Belgium
	Q692015   = 'BE',	  --United States of Belgium
	Q2894388  = 'BE/US',	  --Belgian American
	Q4887679  = 'bengali',	  --Bengali Hindus
	Q402913   = 'bengali',	  --Bengali people
	Q965      = 'BF',	  --Burkina Faso
	Q219      = 'BG',	  --Bulgaria
	Q133255   = 'BG',	  --Bulgarians
	Q398      = 'BH',	  --Bahrain
	Q2879337  = 'BH',	  --Bahrani people
	Q967      = 'BI',	  --Burundi
	Q962      = 'BJ',	  --Benin
	Q25362    = 'BL',	  --Saint-Barthélemy
	Q23635    = 'BM',	  --Bermuda
	Q921      = 'BN',	  --Brunei
	Q750      = 'BO',	  --Bolivia
	Q39193    = 'bohemian',	  --Bohemia
	Q165783   = 'BQ',	  --British Antarctic Territory
	Q27561    = 'BQ',	  --Caribbean Netherlands
	Q155      = 'BR',	  --Brazil
	Q873625   = 'BR',	  --Brazilians
	Q2088324  = 'BR',	  --Colonial Brazil
	Q217230   = 'BR',	  --Empire of Brazil
	Q2587830  = 'BR',	  --Pardo Brazilians
	Q2995285  = 'BR',	  --White Brazilian
	Q778      = 'BS',	  --The Bahamas
	Q917      = 'BT',	  --Bhutan
	Q23408    = 'BV',	  --Bouvet Island
	Q963      = 'BW',	  --Botswana
	Q184      = 'BY',	  --Belarus
	Q483569   = 'BY',	  --Belarusians
	Q2895     = 'BY',	  --Byelorussian Soviet Socialist Republic
	Q12544    = 'byzantine',	  --Byzantine Empire
	Q242      = 'BZ',	  --Belize
	Q16       = 'CA',	  --Canada
	Q1196645  = 'CA',	  --Canadians
	Q5029681  = 'CA/US',	  --Canadian American
	Q244504   = 'catalan',	  --Catalan people
	Q36004    = 'CC',	  --Cocos (Keeling) Islands
	Q974      = 'CD',	  --Democratic the Congo
	Q929      = 'CF',	  --Central African Republic
	Q971      = 'CG',	  --the Congo
	Q124216   = 'CH',	  --Swiss people
	Q39       = 'CH',	  --Switzerland
	Q637979   = 'CH/US',	  --Swiss American
	Q1008     = 'CI',	  --Ivory Coast
	Q26988    = 'CK',	  --Cook Islands
	Q298      = 'CL',	  --Chile
	Q1009     = 'CM',	  --Cameroon
	Q630276   = 'CN',	  --Ancient China
	Q275453   = 'CN',	  --Bai people
	Q29520    = 'CN',	  --China
	Q13426199 = 'CN',	  --China (1912–49)
	Q1173004  = 'CN',	  --Chinese people
	Q42740    = 'CN',	  --Han Chinese
	Q145665   = 'CN',	  --Manchu
	Q9903     = 'CN',	  --Ming dynasty
	Q148      = 'CN',	  --People's China
	Q8733     = 'CN',	  --Qing dynasty
	Q7462     = 'CN',	  --Song dynasty
	Q588574   = 'CN',	  --Tujia people
	Q1207351  = 'CN/CA',	  --Chinese Canadian
	Q1146100  = 'CN/US',	  --Chinese American
	Q739      = 'CO',	  --Colombia
	Q2997267  = 'CO',	  --Colombians
	Q146744   = 'coptic',	  --Copts
	Q17409176 = 'coptic',	  --Copts (ethnic group)
	Q800      = 'CR',	  --Costa Rica
	Q37024    = 'CS',	  --Serbia and Montenegro
	Q241      = 'CU',	  --Cuba
	Q1195125  = 'CU',	  --Cubans
	Q1790886  = 'CU/US',	  --Cuban American
	Q1011     = 'CV',	  --Cape Verde
	Q25279    = 'CW',	  --Curaçao
	Q31063    = 'CX',	  --Christmas Island
	Q229      = 'CY',	  --Cyprus
	Q213      = 'CZ',	  --Czech Republic
	Q170217   = 'CZ',	  --Czechs
	Q518188   = 'CZ/US',	  --Czech American
	Q521972   = 'czechoslovak',	  --Czechoslovak Republic
	Q33946    = 'czechoslovak',	  --Czechoslovakia
	Q140359   = 'czechoslovak',	  --First Czechoslovak Republic
	Q157139   = 'DE',	  --Baltic Germans
	Q47261    = 'DE',	  --Duchy of Bavaria
	Q154195   = 'DE',	  --Kingdom of Bavaria
	Q326029   = 'DE',	  --Duchy of Brunswick
	Q1794     = 'DE',	  --Frankfurt
	Q117020   = 'DE',	  --Free Imperial City of Nuremberg
	Q161036   = 'DE',	  --Free State of Prussia
	Q188      = 'DE',	  --German
	Q151624   = 'DE',	  --German Confederation
	Q16957    = 'DE',	  --German Democratic Republic
	Q561041   = 'DE',	  --German diaspora
	Q43287    = 'DE',	  --German Empire
	Q1206012  = 'DE',	  --German Empire
	Q22633    = 'DE',	  --Germanic peoples
	Q42884    = 'DE',	  --Germans
	Q183      = 'DE',	  --Germany
	Q7318     = 'DE',	  --Nazi Germany
	Q27306    = 'DE',	  --Kingdom of Prussia
	Q38872    = 'DE',	  --Prussia
	Q41304    = 'DE',	  --Weimar Republic
	Q713750   = 'DE',	  --West Germany
	Q5030929  = 'DE/CA',	  --Canadians of German ethnicity
	Q702193   = 'DE/RU',	  --German-Russians
	Q141817   = 'DE/US',	  --German American
	Q977      = 'DJ',	  --Djibouti
	Q164714   = 'DK',	  --Danes
	Q35       = 'DK',	  --Denmark
	Q3015339  = 'DK/US',	  --Danish American
	Q784      = 'DM',	  --Dominica
	Q3247924  = 'DM',	  --Dominican people
	Q5290725  = 'DM/US',	  --Dominican American
	Q786      = 'DO',	  --Dominican Republic
	Q262      = 'DZ',	  --Algeria
	Q736      = 'EC',	  --Ecuador
	Q191      = 'EE',	  --Estonia
	Q2174038  = 'EE',	  --Estonia (1918–1940)
	Q173302   = 'EE',	  --Estonians
	Q79       = 'EG',	  --Egypt
	Q1061510  = 'EG',	  --Egyptians
	Q6250     = 'EH',	  --Western Sahara
	Q21       = 'english',	  --England
	Q42406    = 'english',	  --English people
	Q2935829  = 'english/CA',	  --English Canadian
	Q1344183  = 'english/US',	  --English American
	Q986      = 'ER',	  --Eritrea
	Q1211934  = 'ES',	  --Hispanic
	Q29       = 'ES',	  --Spain
	Q160894   = 'ES',	  --Spaniards
	Q142417   = 'ES',	  --Kingdom of Valencia
	Q7910685  = 'ES',	  --Valenciana
	Q7573109  = 'ES/FR',	  --Spaniards in France
	Q622783   = 'ES/NL',	  --Spanish Netherlands
	Q1041917  = 'ES/US',	  --Spanish American
	Q20715202 = 'ES/US',	  --Spanish Americans
	Q115      = 'ET',	  --Ethiopia
	Q33       = 'FI',	  --Finland
	Q62633    = 'FI',	  --Grand Duchy of Finland
	Q170284   = 'FI',	  --Finns
	Q712      = 'FJ',	  --Fiji
	Q1249802  = 'FK',	  --Falkland Islands
	Q9648     = 'FK',	  --Falkland Islands
	Q157070   = 'flemish',	  --County of Flanders
	Q234      = 'flemish',	  --Flanders
	Q9337     = 'flemish',	  --Flemish Region
	Q702      = 'FM',	  --Federated States of Micronesia
	Q4628     = 'FO',	  --Faroe Islands
	Q4712     = 'FR',	  --Duchy of Burgundy
	Q530670   = 'FR',	  --Kingdom of Burgundy
	Q142      = 'FR',	  --France
	Q70972    = 'FR',	  --Kingdom of France
	Q69323    = 'FR',	  --Kingdom of France (1791-1792)
	Q121842   = 'FR',	  --French people
	Q58296    = 'FR',	  --French Republic
	Q58326    = 'FR',	  --French Second Republic
	Q70802    = 'FR',	  --French Third Republic
	Q71092    = 'FR',	  --Second French Empire
	Q857880   = 'FR/CA',	  --French Canadian
	Q5501705  = 'FR/CA',	  --French Canadian American
	Q5501667  = 'FR/CA',	  --French-speaking Quebecer
	Q1369944  = 'FR/US',	  --French American
	Q106416   = 'frisian',	  --Frisians
	Q1000     = 'GA',	  --Gabon
	Q179876   = 'GB',	  --Kingdom of England
	Q8680     = 'GB',	  --British Empire
	Q842438   = 'GB',	  --British people
	Q23666    = 'GB',	  --Great Britain
	Q161885   = 'GB',	  --Kingdom of Great Britain
	Q174193   = 'GB',	  --United Kingdom of Great Britain and Ireland
	Q145      = 'GB',	  --United Kingdom
	Q7994501  = 'GB',	  --White British
	Q4969441  = 'GB/US',	  --British American
	Q769      = 'GD',	  --Grenada
	Q230      = 'GE',	  --Georgia
	Q130229   = 'GE',	  --Georgian Soviet Socialist Republic
	Q3769     = 'GF',	  --French Guiana
	Q25230    = 'GG',	  --Guernsey
	Q117      = 'GH',	  --Ghana
	Q1410     = 'GI',	  --Gibraltar
	Q285828   = 'GI',	  --Gibraltarian people
	Q223      = 'GL',	  --Greenland
	Q1005     = 'GM',	  --Gambia
	Q1006     = 'GN',	  --Guinea
	Q17012    = 'GP',	  --Guadeloupe
	Q983      = 'GQ',	  --Equatorial Guinea
	Q844930   = 'GR',	  --Classical Athens
	Q41       = 'GR',	  --Greece
	Q539051   = 'GR',	  --Greeks
	Q5690     = 'GR',	  --Sparta
	Q5601752  = 'GR/CA',	  --Greek Canadians
	Q1150312  = 'GR/US',	  --Greek American
	Q35086    = 'GS',	  --South Georgia and the South Sandwich Islands
	Q774      = 'GT',	  --Guatemala
	Q16635    = 'GU',	  --Guam
	Q1007     = 'GW',	  --Guinea-Bissau
	Q734      = 'GY',	  --Guyana
	Q8646     = 'HK',	  --Hong Kong
	Q131198   = 'HM',	  --Heard Island and McDonald Islands
	Q783      = 'HN',	  --Honduras
	Q224      = 'HR',	  --Croatia
	Q167420   = 'HR',	  --Croats
	Q3003133  = 'HR/US',	  --Croatian American
	Q790      = 'HT',	  --Haiti
	Q5639720  = 'HT/US',	  --Haitian American
	Q9067     = 'HU',	  --Hungarian
	Q133032   = 'HU',	  --Hungarian people
	Q28       = 'HU',	  --Hungary
	Q171150   = 'HU',	  --Kingdom of Hungary
	Q948126   = 'HU/US',	  --Hungarian American
	Q252      = 'ID',	  --Indonesia
	Q4256383  = 'ID/US',	  --Indonesian American
	Q27       = 'IE',	  --Ireland
	Q170826   = 'IE',	  --Irish people
	Q3154490  = 'IE/CA',	  --Irish Canadian
	Q6071674  = 'IE/GB',	  --Irish migration to Great Britain
	Q1075293  = 'IE/US',	  --Irish American
	Q801      = 'IL',	  --Israel
	Q1752440  = 'IL',	  --Israeli Jews
	Q875556   = 'IL',	  --Israelis
	Q699923   = 'illyrian',	  --Illyrian Provinces
	Q9676     = 'IM',	  --Isle of Man
	Q668      = 'IN',	  --India
	Q226324   = 'IN',	  --Indian
	Q862086   = 'IN',	  --Indian people
	Q1983634  = 'IN',	  --Odia
	Q2643509  = 'IN/US',	  --Indian American
	Q189975   = 'inuit',	  --Inuit
	Q43448    = 'IO',	  --British Indian Ocean Territory
	Q796      = 'IQ',	  --Iraq
	Q794      = 'IR',	  --Iran
	Q2859361  = 'IR',	  --Iranian Arabs
	Q1672477  = 'IR',	  --Iranian peoples
	Q215530   = 'IE',	  --Kingdom of Ireland
	Q189      = 'IS',	  --Iceland
	Q191356   = 'IS',	  --Icelanders
	Q174306   = 'IT',     --Republic of Genova  
	Q2252973  = 'IT',	  --Duchy of Florence
	Q148540   = 'IT',	  --Florence
	Q153660   = 'IT',	  --Italian Social Republic
	Q50001    = 'IT',	  --Italians
	Q38       = 'IT',	  --Italy
	Q172579   = 'IT',	  --Kingdom of Italy
	Q223936   = 'IT',	  --Kingdom of Italy
	Q153529   = 'IT',	  --Duchy of Milan
	Q173065   = 'IT',	  --Kingdom of Naples
	Q170174   = 'IT',	  --Papal States
	Q154849   = 'IT',	  --Grand Duchy of Tuscany
	Q4948     = 'IT',	  --Venice
	Q1065371  = 'IT/BR',	  --Italian Brazilians
	Q974693   = 'IT/US',	  --Italian American
	Q49209    = 'javanese',	  --Javanese people
	Q785      = 'JE',	  --Jersey
	--Q34069    = 'jewish',	  --Ashkenazi Jews
	--Q7325     = 'jewish',	  --Jewish people
	Q678551   = 'US',	  --American Jews
	Q766      = 'JM',	  --Jamaica
	Q6127476  = 'JM',	  --Jamaicans of African ancestry
	Q810      = 'JO',	  --Jordan
	Q101828   = 'JP',	  --Ainu people
	Q188712   = 'JP',	  --Empire of Japan
	Q17       = 'JP',	  --Japan
	Q161652   = 'JP',	  --Japanese people
	Q1156766  = 'JP/US',	  --Japanese American
	Q114      = 'KE',	  --Kenya
	Q813      = 'KG',	  --Kyrgyzstan
	Q424      = 'KH',	  --Cambodia
	Q710      = 'KI',	  --Kiribati
	Q970      = 'KM',	  --Comoros
	Q763      = 'KN',	  --Saint Kitts and Nevis
	Q533312   = 'KO/JP',	  --Koreans in Japan
	Q18097    = 'korean',	  --Korea
	Q484464   = 'korean',	  --Koreans
	Q276879   = 'korean/US',	  --Korean American
	Q423      = 'KP',	  --North Korea
	Q884      = 'KR',	  --South Korea
	Q817      = 'KW',	  --Kuwait
	Q5785     = 'KY',	  --Cayman Islands
	Q80040    = 'KZ',	  --Kazakhs
	Q232      = 'KZ',	  --Kazakhstan
	Q819      = 'LA',	  --Laos
	Q822      = 'LB',	  --Lebanon
	Q760      = 'LC',	  --Saint Lucia
	Q347      = 'LI',	  --Liechtenstein
	Q932244   = 'LK',	  --Sinhalese people
	Q854      = 'LK',	  --Sri Lanka
	Q1815623  = 'LK',	  --Sri Lankan Tamil people
	Q155019   = 'lorrain',	  --Duchy of Lorraine
	Q1014     = 'LR',	  --Liberia
	Q1013     = 'LS',	  --Lesotho
	Q49683    = 'LT',	  --Grand Duchy of Lithuania
	Q37       = 'LT',	  --Lithuania
	Q186192   = 'LT',	  --Lithuanian people
	Q2997706  = 'LT',	  --Litvin
	Q32       = 'LU',	  --Luxembourg
	Q211      = 'LV',	  --Latvia
	Q49540    = 'LV',	  --Latvian people
	Q1016     = 'LY',	  --Libya
	Q1028     = 'MA',	  --Morocco
	Q2436423  = 'macedonian',	  --Macedonians
	Q1401272  = 'macedonian',	  --Macedonians
	Q235      = 'MC',	  --Monaco
	Q10957559 = 'MD',	  --Moldavia
	Q217      = 'MD',	  --Moldova
	Q236      = 'ME',	  --Montenegro
	Q126125   = 'MF',	  --Saint Martin
	Q1019     = 'MG',	  --Madagascar
	Q709      = 'MH',	  --Marshall Islands
	Q221      = 'MK',	  --Macedonia
	Q912      = 'ML',	  --Mali
	Q836      = 'MM',	  --Myanmar
	Q711      = 'MN',	  --Mongolia
	Q41975    = 'MN',	  --mongols
	Q14773    = 'MO',	  --Macau
	Q16644    = 'MP',	  --Northern Mariana Islands
	Q17054    = 'MQ',	  --Martinique
	Q1025     = 'MR',	  --Mauritania
	Q13353    = 'MS',	  --Montserrat
	Q233      = 'MT',	  --Malta
	Q1027     = 'MU',	  --Mauritius
	Q826      = 'MV',	  --Maldives
	Q1020     = 'MW',	  --Malawi
	Q1209676  = 'MX',	  --Mexicans
	Q96       = 'MX',	  --Mexico
	Q581921   = 'MX/US',	  --Mexican American
	Q833      = 'MY',	  --Malaysia
	Q1029     = 'MZ',	  --Mozambique
	Q1030     = 'NA',	  --Namibia
	Q135863   = 'native american',	  --Algonquian people
	Q102090   = 'native american',	  --Apache
	Q105405   = 'native american',	  --Cherokee
	Q322951   = 'native american',	  --Cheyenne people
	Q324516   = 'native american',	  --Choctaw
	Q117133   = 'native american',	  --Comanche
	Q117191   = 'native american',	  --Cree
	Q68518    = 'native american',	  --Iroquois
	Q116971   = 'native americans',	  --Mohawk people
	Q36747    = 'native americans',	  --Native Americans
	Q49297    = 'native americans',	  --Native Americans in the United States
	Q108266   = 'native americans',	  --Navajo people
	Q26774    = 'native americans',	  --Shoshone people
	Q107434   = 'native americans',	  --Sioux
	Q33788    = 'NC',	  --New Caledonia
	Q1032     = 'NE',	  --Niger
	Q31057    = 'NF',	  --Norfolk Island
	Q1033     = 'NG',	  --Nigeria
	Q7032863  = 'NG',	  --Nigerian American
	Q811      = 'NI',	  --Nicaragua
	Q200569   = 'NL',	  --Dutch people
	Q170072   = 'NL',	  --Dutch Republic
	Q55       = 'NL',	  --Netherlands
	Q774783   = 'NL',	  --Sovereign Principality of the United Netherlands
	Q29999    = 'NL',	  --Kingdom of the Netherlands
	Q15864    = 'NL',	  --United Kingdom of the Netherlands
	Q1058496  = 'NL/US',	--Dutch American
	Q20       = 'NO',	  --Norway
	Q188779   = 'NO',	  --Norwegians
	Q1193808  = 'NO/US',	  --Norwegian American
	Q27996474 = 'northern netherlandish',	  --Northern Netherlands
	Q837      = 'NP',	  --Nepal
	Q697      = 'NR',	  --Nauru
	Q34020    = 'NU',	  --Niue
	Q664      = 'NZ',	  --New Zealand
	Q7015349  = 'NZ',	  --New Zealand European
	Q842      = 'OM',	  --Oman
	Q12560    = 'ottoman',	  --Ottoman Empire
	Q804      = 'PA',	  --Panama
	Q419      = 'PE',	  --Peru
	Q83311    = 'persian',	  --Persian Empire
	Q484206   = 'persian',	  --Persian people
	Q30971    = 'PF',	  --French Polynesia
	Q691      = 'PG',	  --Papua New Guinea
	Q928      = 'PH',	  --Philippines
	Q843      = 'PK',	  --Pakistan
	Q3310017  = 'PK',	  --Pakistani
	Q7125803  = 'PK/US',	  --Pakistani American
	Q221457   = 'PL',	  --Congress Poland
	Q171348   = 'PL',	  --Crown of the Kingdom of Poland
	Q501303   = 'PL',	  --Free City of Krakow
	Q147239   = 'PL',	  --Kashubians
	Q211274   = 'PL',	  --People's Poland
	Q1649871  = 'PL',	  --Kingdom of Poland
	Q36       = 'PL',	  --Poland
	Q1026     = 'PL',	  --Poles
	Q172107   = 'PL',	  --Polish–Lithuanian Commonwealth
	Q207272   = 'PL',	  --Second Polish Republic
	Q2025025  = 'PL/UK',	  --Poles in Ukraine
	Q1189552  = 'PL/US',	  --Polish American
	Q34617    = 'PM',	  --Saint Pierre and Miquelon
	Q35672    = 'PN',	  --Pitcairn Islands
	Q679524   = 'pontic',	  --Pontic Greeks
	Q893594   = 'PR',	  --Puerto Rican people
	Q1183     = 'PR',	  --Puerto Rico
	Q7258666  = 'PR/US',	  --Puerto Ricans in the United States
	Q23792    = 'PS',	  --Palestine
	Q201190   = 'PS',	  --Palestinian people
	Q407199   = 'PS',	  --Palestinian territories
	Q45670    = 'PT',	  --Kingdom of Portugal
	Q45       = 'PT',	  --Portugal
	Q178630   = 'PT',	  --Portuguese people
	Q2615416  = 'PT/BR',	  --Portuguese Brazilians
	Q3267812  = 'PT/US',	  --Portuguese American
	Q695      = 'PW',	  --Palau
	Q733      = 'PY',	  --Paraguay
	Q846      = 'QA',	  --Qatar
	Q17070    = 'RE',	  --Réunion
	Q203493   = 'RO',	  --Kingdom of Romania
	Q218      = 'RO',	  --Romania
	Q7913     = 'RO',	  --Romanian
	Q2685298  = 'RO',	  --Romanian People's Republic
	Q485150   = 'RO',	  --Romanians
	Q389004   = 'RO',     --Wallachia
	Q171393   = 'RO',     --Wallachia
	Q1747689  = 'roman',	  --Ancient Rome
	Q2277     = 'roman',	  --Roman Empire
	Q684030   = 'RS',	  --Principality of Serbia
	Q241748   = 'RS',	  --Kingdom of Serbia
	Q403      = 'RS',	  --Serbia
	Q1259835  = 'RS',	  --Serbians
	Q127885   = 'RS',	  --Serbs
	Q1064662  = 'RS',	  --Serbs of Bosnia and Herzegovina
	Q1280677  = 'RS',	  --Serbs of Croatia
	Q1261777  = 'RS/US',	  --Serbian American
	Q170770   = 'RU',	  --Grand Duchy of Moscow
	Q159      = 'RU',	  --Russia
	Q34266    = 'RU',	  --Russian Empire
	Q139319   = 'RU',	  --Russian Republic
	Q2184     = 'RU',	  --Russian Soviet Federative Socialist Republic
	Q49542    = 'RU',	  --Russians
	Q1140588  = 'RU/US',	  --Russian Americans
	Q1037     = 'RW',	  --Rwanda
	Q851      = 'SA',	  --Saudi Arabia
	Q153015   = 'saxon',	  --Kingdom of Saxony
	Q685      = 'SB',	  --Solomon Islands
	Q1042     = 'SC',	  --Seychelles
	Q230791   = 'scottish',	  --Kingdom of Scotland
	Q22       = 'scottish',	  --Scotland
	Q181634   = 'scottish',	  --Scottish people
	Q7437641  = 'scottish/CA',	  --Scottish Canadian
	Q3476361  = 'scottish/US',	  --Scottish American
	Q1049     = 'SD',	  --Sudan
	Q34       = 'SE',	  --Sweden
	Q165192   = 'SE',	  --Swedes
	Q115026   = 'SE/US',	  --Swedish American
	Q334      = 'SG',	  --Singapore
	Q192184   = 'SH',	  --Saint Helena, Ascension and Tristan da Cunha
	Q15863    = 'SI',	  --Slovenes
	Q215      = 'SI',	  --Slovenia
	Q7541693  = 'SI/US',	  --Slovene American
	Q81720    = 'silesian',	  --Silesia
	Q140472   = 'silesian',	  --Silesians
	Q842829   = 'SJ',	  --Svalbard and Jan Mayen
	Q214      = 'SK',	  --Slovakia
	Q171336   = 'SK',	  --Slovaks
	Q1044     = 'SL',	  --Sierra Leone
	Q238      = 'SM',	  --San Marino
	Q1041     = 'SN',	  --Senegal
	Q241696   = 'SO',	  --Somali people
	Q1045     = 'SO',	  --Somalia
	Q5408188  = 'SO/US',	  --Somali American
	Q146521   = 'sorbian',	  --Sorbs
	Q6581823  = 'southern netherlandish',	  --Southern Netherlands
	Q2305208  = 'RU',	  --Soviet Russia
	Q15180    = 'soviet', --Soviet Union
	Q730      = 'SR',	  --Suriname
	Q958      = 'SS',	  --South Sudan
	Q1039     = 'ST',	  --Sao Tome and Principe
	Q792      = 'SV',	  --El Salvador
	Q26273    = 'SX',	  --Sint Maarten
	Q858      = 'SY',	  --Syria
	Q878607   = 'SY',	  --Syrian people
	Q1050     = 'SZ',	  --Swaziland
	Q35565    = 'tatar',	  --Tatars
	Q18221    = 'TC',	  --Turks and Caicos Islands
	Q657      = 'TD',	  --Chad
	Q129003   = 'TF',	  --French Southern and Antarctic Lands
	Q945      = 'TG',	  --Togo
	Q2408653  = 'TH',	  --Thai Chinese
	Q472834   = 'TH',	  --Thai people
	Q869      = 'TH',	  --Thailand
	Q4450068  = 'TH/US',	  --Thai American
	Q187985   = 'tibetan',	  --Tibetan people
	Q483505   = 'TJ',	  --Tajik people
	Q863      = 'TJ',	  --Tajikistan
	Q36823    = 'TK',	  --Tokelau
	Q574      = 'TL',	  --East Timor
	Q484472   = 'TM',	  --Turkmen people
	Q874      = 'TM',	  --Turkmenistan
	Q948      = 'TN',	  --Tunisia
	Q678      = 'TO',	  --Tonga
	Q43       = 'TR',	  --Turkey
	Q245807   = 'TR',	  --Turkish Cypriots
	Q84072    = 'TR',	  --Turkish people
	Q2593852  = 'TR',	  --Turks in the Macedonia
	Q170352   = 'transylvanian saxon',	  --Transylvanian Saxons
	Q754      = 'TT',	  --Trinidad and Tobago
	Q672      = 'TV',	  --Tuvalu
	Q193067   = 'TV',	  --Tuvans
	Q865      = 'TW',	  --Taiwan
	Q44614    = 'TW/US',	  --Taiwanese American
	Q924      = 'TZ',	  --Tanzania
	Q212      = 'UA',	  --Ukraine
	Q243610   = 'UA',	  --Ukrainian People's Republic
	Q133356   = 'UA',	  --Ukrainian Soviet Socialist Republic
	Q1508143  = 'UA',	  --Ukrainian State
	Q44806    = 'UA',	  --Ukrainians
	Q457167   = 'UA',	  --West Ukrainian People's Republic
	Q1036     = 'UG',	  --Uganda
	Q16645    = 'UM',	  --United States Minor Outlying Islands
	Q49085    = 'US',	  --African Americans
	Q846570   = 'US',	  --Americans
	Q686949   = 'US',	  --Cajun
	Q43103    = 'US',	  --European American
	Q30       = 'US',	  --United States of America
	Q49078    = 'US',	  --White American
	Q77       = 'UY',	  --Uruguay
	Q265      = 'UZ',	  --Uzbekistan
	Q483047   = 'UZ',	  --Uzbeks
	Q237      = 'VA',	  --Vatican City
	Q757      = 'VC',	  --Saint Vincent and the Grenadines
	Q717      = 'VE',	  --Venezuela
	Q25305    = 'VG',	  --British Virgin Islands
	Q11703    = 'VI',	  --United States Virgin Islands
	Q881      = 'VN',	  --Vietnam
	Q216151   = 'VN',	  --Vietnamese people
	Q686      = 'VU',	  --Vanuatu
	Q381151   = 'walloon',	  --Walloons
	Q188353   = 'welsh',	  --Welsh people
	Q3094841  = 'welsh/US',	  --Welsh American
	Q35555    = 'WF',	  --Wallis and Futuna
	Q683      = 'WS',	  --Samoa
	Q7409937  = 'WS/US',	  --Samoan American
	Q1246     = 'XK',	  --Kosovo
	Q805      = 'YE',	  --Yemen
	Q17063    = 'YT',	  --Mayotte
	Q838261   = 'YU',	  --Federal Yugoslavia
	Q83286    = 'YU',	  --Socialist Federal Yugoslavia
	Q191077   = 'YU',	  --Kingdom of Yugoslavia
	Q36704    = 'YU',	  --Yugoslavia
	Q236807   = 'YU',	  --Yugoslavs
	Q258      = 'ZA',	  --South Africa
	Q953      = 'ZM',	  --Zambia
	Q954      = 'ZW'	  --Zimbabwe
}
local n2iso = {
	['afghan' ]            = 'AF',
	['albanian' ]          = 'AL',
	['algerian' ]          = 'DZ',
	['american samoan' ]   = 'AS',
	['andorran' ]          = 'AD',
	['angolan' ]           = 'AO',
	['anguillan' ]         = 'AI',
	['antarctic' ]         = 'AQ',
	['argentine' ]         = 'AR',
	['armenian' ]          = 'AM',
	['aruban' ]            = 'AW',
	['australian' ]        = 'AU',
	['austrian' ]          = 'AT',
	['azerbaijani' ]       = 'AZ',
	['bahamian' ]          = 'BS',
	['bahraini' ]          = 'BH',
	['bangladeshi' ]       = 'BD',
	['barbadian' ]         = 'BB',
	['belarusian' ]        = 'BY',
	['belgian' ]           = 'BE',
	['belizean' ]          = 'BZ',
	['beninese' ]          = 'BJ',
	['bermudan' ]          = 'BM',
	['bhutanese' ]         = 'BT',
	['bolivian' ]          = 'BO',
	['bosnian' ]           = 'BA',
	['herzegovinian' ]     = 'BA',
	['botswanan' ]         = 'BW',
	['brazilian' ]         = 'BR',
	['bruneian' ]          = 'BN',
	['bulgarian' ]         = 'BG',
	['burkinabe' ]         = 'BF',
	['burundian' ]         = 'BI',
	['cambodian' ]         = 'KH',
	['cameroonian' ]       = 'CM',
	['canadian' ]          = 'CA',
	['cape verdean' ]      = 'CV',
	['caymanian' ]         = 'KY',
	['central african' ]   = 'CF',
	['chadian' ]           = 'TD',
	['chilean' ]           = 'CL',
	['chinese' ]           = 'CN',
	['colombian' ]         = 'CO',
	['comorian' ]          = 'KM',
	['congolese (r)' ]     = 'CG',
	['congolese (dr)' ]    = 'CD',
	['costa rican' ]       = 'CR',
	['ivorian' ]           = 'CI',
	['croatian' ]          = 'HR',
	['cuban' ]             = 'CU',
	['cypriot' ]           = 'CY',
	['czech' ]             = 'CZ',
	['danish' ]            = 'DK',
	['djiboutian' ]        = 'DJ',
	['dominican' ]         = 'DM',
	['dominican (r)' ]     = 'DO',
	['ecuadorian' ]        = 'EC',
	['egyptian' ]          = 'EG',
	['salvadoran' ]        = 'SV',
	['equatorial guinean' ]= 'GQ',
	['eritrean' ]          = 'ER',
	['estonian' ]          = 'EE',
	['ethiopian' ]         = 'ET',
	['falkland island' ]   = 'FK',
	['faroese' ]           = 'FO',
	['fijian' ]            = 'FJ',
	['finnish' ]           = 'FI',
	['french' ]            = 'FR',
	['french guianese' ]   = 'GF',
	['french polynesian' ] = 'PF',
	['gabonese' ]          = 'GA',
	['gambian' ]           = 'GM',
	['georgian' ]          = 'GE',
	['german' ]            = 'DE',
	['ghanaian' ]          = 'GH',
	['gibraltar' ]         = 'GI',
	['greek' ]             = 'GR',
	['greenlandic' ]       = 'GL',
	['grenadian' ]         = 'GD',
	['guadeloupian' ]      = 'GP',
	['guamanian' ]         = 'GU',
	['guatemalan' ]        = 'GT',
	['guinean' ]           = 'GN',
	['guinea-bissauan' ]   = 'GW',
	['guyanese' ]          = 'GY',
	['haitian' ]           = 'HT',
	['vaticanese' ]        = 'VA',
	['honduran' ]          = 'HN',
	['hungarian' ]         = 'HU',
	['icelandic' ]         = 'IS',
	['indian' ]            = 'IN',
	['indonesian' ]        = 'ID',
	['iranian' ]           = 'IR',
	['iraqi' ]             = 'IQ',
	['irish' ]             = 'IE',
	['manx' ]              = 'IM',
	['israeli' ]           = 'IL',
	['italian' ]           = 'IT',
	['jamaican' ]          = 'JM',
	['japanese' ]          = 'JP',
	['jordanian' ]         = 'JO',
	['kazakhstani' ]       = 'KZ',
	['kenyan' ]            = 'KE',
	['kiribati' ]          = 'KI',
	['north korean' ]      = 'KP',
	['south korean' ]      = 'KR',
	['kuwaiti' ]           = 'KW',
	['kyrgyzstani' ]       = 'KG',
	['laotian' ]           = 'LA',
	['latvian' ]           = 'LV',
	['lebanese' ]          = 'LB',
	['lesothan' ]          = 'LS',
	['liberian' ]          = 'LR',
	['libyan' ]            = 'LY',
	['liechtensteinian' ]  = 'LI',
	['lithuanian' ]        = 'LT',
	['luxembourgish' ]     = 'LU',
	['macedonian' ]        = 'MK',
	['malagasy' ]          = 'MG',
	['malawian' ]          = 'MW',
	['malaysian' ]         = 'MY',
	['maldivian' ]         = 'MV',
	['malian' ]            = 'ML',
	['maltese' ]           = 'MT',
	['marshallese' ]       = 'MH',
	['martinican' ]        = 'MQ',
	['mauritanian' ]       = 'MR',
	['mauritian' ]         = 'MU',
	['mahoran' ]           = 'YT',
	['mexican' ]           = 'MX',
	['micronesian' ]       = 'FM',
	['moldovan' ]          = 'MD',
	['monégasque' ]        = 'MC',
	['mongolian' ]         = 'MN',
	['montenegrin' ]       = 'ME',
	['montserratian' ]     = 'MS',
	['moroccan' ]          = 'MA',
	['mozambican' ]        = 'MZ',
	['burmese' ]           = 'MM',
	['namibian' ]          = 'NA',
	['nauruan' ]           = 'NR',
	['nepali' ]            = 'NP',
	['dutch' ]             = 'NL',
	['dutch antillean' ]   = 'AN',
	['new caledonian' ]    = 'NC',
	['new zealand' ]       = 'NZ',
	['nicaraguan' ]        = 'NI',
	['nigerien' ]          = 'NE',
	['nigerian' ]          = 'NG',
	['niuean' ]            = 'NU',
	['norfolkish' ]        = 'NF',
	['northern marianan' ] = 'MP',
	['norwegian' ]         = 'NO',
	['omani' ]             = 'OM',
	['pakistani' ]         = 'PK',
	['palauan' ]           = 'PW',
	['palestinian' ]       = 'PS',
	['panamanian' ]        = 'PA',
	['papua new guinean' ] = 'PG',
	['paraguayan' ]        = 'PY',
	['peruvian' ]          = 'PE',
	['philippine' ]        = 'PH',
	['pitcairnian' ]       = 'PN',
	['polish' ]            = 'PL',
	['portuguese' ]        = 'PT',
	['puerto rican' ]      = 'PR',
	['qatari' ]            = 'QA',
	['réunionese' ]        = 'RE',
	['romanian' ]          = 'RO',
	['russian' ]           = 'RU',
	['rwandan' ]           = 'RW',
	['saint helenian' ]    = 'SH',
	['st. lucian' ]        = 'LC',
	['vincentian' ]        = 'VC',
	['samoan' ]            = 'WS',
	['sammarinese' ]       = 'SM',
	['são toméan' ]        = 'ST',
	['saudi arabian' ]     = 'SA',
	['senegalese' ]        = 'SN',
	['serbian' ]           = 'RS',
	['seychellois' ]       = 'SC',
	['sierra leonean' ]    = 'SL',
	['singaporean' ]       = 'SG',
	['slovak' ]            = 'SK',
	['slovene' ]           = 'SI',
	['solomonian' ]        = 'SB',
	['somali' ]            = 'SO',
	['south african' ]     = 'ZA',
	['spanish' ]           = 'ES',
	['sri lankan' ]        = 'LK',
	['sudanese' ]          = 'SD',
	['surinamese' ]        = 'SR',
	['swazi' ]             = 'SZ',
	['swedish' ]           = 'SE',
	['swiss' ]             = 'CH',
	['syrian' ]            = 'SY',
	['taiwanese' ]         = 'TW',
	['tajikistani' ]       = 'TJ',
	['tanzanian' ]         = 'TZ',
	['thailandish' ]       = 'TH',
	['timorese' ]          = 'TL',
	['togolese' ]          = 'TG',
	['tokelauan' ]         = 'TK',
	['tongan' ]            = 'TO',
	['tunisian' ]          = 'TN',
	['turkish' ]           = 'TR',
	['turkmen' ]           = 'TM',
	['tuvaluan' ]          = 'TV',
	['ugandan' ]           = 'UG',
	['ukrainian' ]         = 'UA',
	['emirati' ]           = 'AE',
	['british' ]           = 'GB',
	['american' ]          = 'US',
	['uruguayan' ]         = 'UY',
	['uzbekistani' ]       = 'UZ',
	['vanuatuan' ]         = 'VU',
	['venezuelan' ]        = 'VE',
	['vietnamese' ]        = 'VN',
	['sahrawian' ]         = 'EH',
	['yemeni' ]            = 'YE',
	['zambian' ]           = 'ZM',
	['zimbabwean' ]        = 'ZW'
}


-- ==================================================
-- === Internal functions ===========================
-- ==================================================

-------------------------------------------------------------------------------
local function getBareLabel(id, userLang) 
-- code equivalent to require("Module:Wikidata label")._getLabel with Wikidata=- option
	local label, link
	-- build language fallback list
	local langList = mw.language.getFallbacksFor(userLang)
	table.insert(langList, 1, userLang)
	for _, lang in ipairs(langList) do  -- loop over language fallback list looking for label in the specific language
		label = mw.wikibase.getLabelByLang(id, lang)
		if label then break end                    -- label found and we are done
	end	
	return label or id
end

------------------------------------------------------------------------------
-- straight union of two arrays (tables)
local function union ( a, b )
    local result = {}
    for _,v in pairs ( a or {} ) do
        table.insert( result, v )
    end
    for _,v in pairs ( b or {} ) do
         table.insert( result, v )
    end
    return result
end

------------------------------------------------------------------------------
-- get female forms of occupation using " female form of label (P2521) " property
local function getFemaleLabel(item, lang)
	local label = {}
	for _, statement in pairs( mw.wikibase.getBestStatements( item, 'P2521' )) do
		local v = statement.mainsnak.datavalue.value
		if v then 
			label[v.language] = v.text
		end
	end
	if label then
		label = core.langSwitch(label,lang)
	end
	if not label then
		label = getBareLabel(item, lang)
	end
	return label
end

--[[
Implementation of Template:NationAndOccupation/default
INPUTS:
* nationality - array of string in the form compatible with Template:Nationality
* occupation  - array of already translated strings
* gender      - single gender string "male" or "female"
* lang        - users language
]]
local function assembleNaO(nationality, occupation, gender, lang)

	local styleLUT = { -- language dependent order
		-- Occupation then nationality order
		ca=10 , es=10, eu=10, fa=10, he=10, it=10, pt=10, ro=10, vi=10,
		-- Occupation then nationality order with first nationality in a special form
		fr=11, 
		-- Nationality then Occupation order
		cs=20 , da=20, el=20, en=20, eo=20, et=20, hu=20, mk=20, ml=20, nl=20, 
		-- Nationality then Occupation order, no space
		zh=21,
		-- Nationality then Occupation order with 1st nationality in a special form and 2nd nationality upper case
		nds=22, de=22 , 
		-- Nationality then Occupation order with 1st nationality in a special form and 2nd nationality lower case
		pl=23, ru=23, sl=23, bg=23}
    -- Use LangSwitch to choose the style based on the language. That way language fallback chain is used
	local style = core.langSwitch(styleLUT, lang) 
	 
	-- create nationality string
	gender = gender or 'male'
	local frame = mw.getCurrentFrame()
	local nStr=''
	if nationality and #nationality==1 then --Single nationality case
		nStr = frame:expandTemplate{ title='জাতীয়তা', args={nationality[1], gender, lang=lang} }
	elseif nationality and #nationality>1 then                 --Double nationality case
		local N2 = frame:expandTemplate{ title='জাতীয়তা', args={nationality[2], gender, lang=lang} }
		if style==11 or style==22 or style==23 then -- nationality in a special form
			gender = 's'
		end
		local N1 = frame:expandTemplate{ title='জাতীয়তা', args={nationality[1], gender, lang=lang} }
		if style==23 then
			N2 = mw.ustring.lower(N2)
		end
		nStr = N1 .. '-' .. N2
	end
	
	-- Create final string
	if occupation then
		local oStr = linguistic_conj(occupation, lang, 'and')
		if style<20 then -- Type 1: Occupation then nationality order
			return oStr .. ' ' .. nStr
		elseif style==21 then -- Type 1: Nationality then Occupation order, no space
			return nStr .. oStr
		else             -- Type 2: Nationality then Occupation order
			return nStr .. ' ' .. oStr
		end
	else
		return nStr
	end
end

--[[
Implementation of Template:NationAndOccupation
INPUTS:
* entity - wikidata entity 
* lang   - users language
OUTPUTS:
* data   - data structure with data extracted from Wikidata, including fields:
	* nationality   - array of string in the form compatible with Template:Nationality
	* occupation    - array of already translated occupation strings
	* occupationEN  - array of occupation strings in english
	* gender        - single gender string "male" or "female"
]]
local function harvest_wikidata(entity, lang)
	local occupation, occupationEN, nationality, gender, data = {}, {}, {}, {}, {}
	
	-- if wikidata q-code is provided than look up few properties
	if entity then
		-- harvest  properties from wikidata
		local property = {P21='gender', P27='country', P106='occupation', P172='ethnicity'}
		for prop, field in pairs( property ) do
			if entity.claims and entity.claims[prop] then -- if we have wikidata item and item has the property
				-- capture multiple "best" Wikidata value
				data[field] = core.parseStatements(entity:getBestStatements( prop ), nil)
			end
		end
	end
	
	-- Look up gender
	if data.gender then	
		local LUT = { Q6581097='male', Q2449503='male', Q6581072='female', Q1052281='female' }
		gender = LUT[data.gender[1]]
	end
	if gender~='male' and gender~='female' then
		gender = 'male'
	end
	
	-- Look up occupation
	local occ
	for i, oItem in ipairs(data.occupation or {}) do
		if i>6 then
			break -- only 6 occupations are allowed
		end
		local occEN = mw.wikibase.getLabelByLang(oItem, 'en')
		if gender == 'female' then -- get localized (translated) occupation labels in female form
			occ = getFemaleLabel(oItem, lang) 
		elseif lang=='en' then     -- get English occupation labels in male form
			occ = occEN
		else                       -- get localized (translated) occupation labels in male form
			occ = getBareLabel(oItem, lang)
		end
		table.insert(occupation  , occ) 
		table.insert(occupationEN, occEN)
	end
	
	-- Look up nationality
	if data.country or data.ethnicity then -- from wikidata
		-- process P27/country and P172/ethnicity
		local nTable = {} -- table of unique nationality iso codes stored as keys
		for _, v in ipairs( union(data.country, data.ethnicity) ) do
			for iso in mw.text.gsplit( q2iso[v] or '', '/', true ) do
				nTable[ iso ] = 1
			end
		end
		for nat, _ in pairs(nTable) do
			table.insert(nationality, nat)
		end
	end
	data = {nationality=nationality, occupation=occupation, gender=gender, occupationEN=occupationEN}
	return data
end

-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}

-- ===========================================================================
-- === Version of the function to be called from other LUA codes
-- ===========================================================================

--[[
Implementation of Template:NationAndOccupation
INPUTS:
* args.nationality - '/' separated string with substrings in the form compatible 
                     with Template:Nationality
* args.occupation  - '/' separated string with substrings with english names of 
                     occupations compatible with Template:Occupations
* args.gender      - single gender string "male" or "female"
* args.wikidata    - wikidata q-code
* args.lang        - users language
OUTPUTS:
* OutStr - string with transpaced phrase like "english writer"
* args   - data structure with processed inputs
* data   - data structure with data extracted from Wikidata
]]
function p._NationAndOccupation(args0)
	local occupation, nationality, entity, occupationEN
	
	-- if wikidata q-code is provided than look up few properties
	local q = args0.wikidata
	if q and type(q)=='string' and string.sub(q,1,1)=="Q"  then --  
		entity = mw.wikibase.getEntity(q)
	elseif q then
		entity = q
	end
	local data   = harvest_wikidata(entity, args0.lang)
	local gender = args0.gender or data.gender
	
	-- Look up occupation
	if args0.occupation then -- from input arguments
		local frame = mw.getCurrentFrame()
		local occArray = mw.text.split(args0.occupation, '/')
		occupation = {}
		for i = 1,6 do 
			if occArray[i] and occArray[i]~='' then 
				local args={occArray[i], gender, lang=args0.lang}
				table.insert(occupation, frame:expandTemplate{ title='Occupation', args=args })
			end
		end
		if #occupation==0 then
			occupation = nil
		end
	end
	
	-- Look up nationality
	if args0.nationality then -- from input arguments
		nationality = mw.text.split(args0.nationality, '/')
		for i = 1,2 do -- if nationality is a word than see if we can find iso code
			local N = string.lower(nationality[i] or '')
			if #N>2 and n2iso[N] then 
				nationality[i] = n2iso[N]
			end
		end
		if #nationality==0 then
			nationality = nil
		end
	end
	local outStr = assembleNaO(nationality or data.nationality, occupation or data.occupation, gender, args0.lang)
	local args = {nationality=nationality, occupation=occupation, gender=args0.gender, occupationEN=occupationEN}
    --outStr = outStr .. '\n' .. mw.text.jsonEncode(data) .. '\n' .. mw.text.jsonEncode(args)
	return outStr, args, data
end

-- ===========================================================================
-- === Version of the functions to be called from template namespace
-- ===========================================================================

--[[
NationAndOccupation
 
This function is the core part of the NationAndOccupation template. 
 
Usage:
{{#invoke:}}
 
 Parameters:
  *nationality - '/' separated string with substrings in the form compatible 
                     with Template:Nationality
  * occupation  - '/' separated string with substrings with english names of 
                     occupations compatible with Template:Occupations
  * gender      - single gender string "male" or "female"
  * wikidata    - wikidata q-code
  * lang        - users language
 Error Handling:

]]
function p.NationAndOccupation(frame)
	local args0 = core.getArgs(frame)
	local outStr, args, data = p._NationAndOccupation(args0)
	return outStr
end

return p