Language Pack Tester.fh_lua

--[[
@Title: Language Pack Tester
@Type: Standard
@Author: Calico Pie
@Version: 1.1
@Keywords: language, language pack
@LastUpdated: 3 Apr 2023
@Licence: This plugin is copyright (c) Calico Pie and contributors, and is licensed under the MIT License
which is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)

@Description: 
This plugin is for the use of language pack authors only, for testing language plugins.  Instructions for 
using it are stored in the plugin itself, just below the header.  Currently it only tests the FH_GetDateText entry point.
However in the future it could be extended to test any entry point.
@Changes:
v1.1 Corrected minor error in GetNarrDisplayText
v1.0 Initial version
]]


--[[
INSTRUCTIONS FOR USING THE LANGUAGE PACK TESTER
================================================
The Language Pack Tester is not designed to be run from the Plugin Window, although it is installed in the normal way and, when
installed, will be listed in the Plugin Window like any other plugin.  To use it, you need to add 
some lines to the language pack plugin you want to test, and then run the language pack plugin in the debugger.  To open a language
pack in a debugger, click on "Language Packs..." on the Tools menu, select the required language and click the 'Plugin' button.
The lines you add will only have an effect when the plugin is executed in the debugger, and only if the Language Pack
Tester has been installed in the Plugin Window.  

Note: at present the language pack tester is only used for testing FH_GetDateText.  But more tests may be added in the future.

Add the following lines near the top of your language pack plugin:

	g_bLangPackTesterLoaded = false;
	if DEBUG_MODE then
		local sPath = fhGetContextInfo("CI_APP_DATA_FOLDER") .. "\\plugins\\Language Pack Tester.fh_lua";
		g_bLangPackTesterLoaded = pcall(function() dofile(sPath) end);
	end

Then at the bottom of the language pack plugin, or in its debugging function if it has one, add something like the following:

	if g_bLangPackTesterLoaded then
		local sRes = fhMessageBox("Test FH_GetDateText?", "MB_YESNO");
		if sRes == "Yes" then
	   	Test_FH_GetDateText();
		end
	end

Finally run the language pack plugin within the debugger and click 'Yes' when asked if you want to test FH_GetDateText.
================================================
]]

fhInitialise(7);  -- implies users will need version 7 as a minimum

-- The table below stores the sample dates.  The format is type, subtype, Year, Month, Day, Year, Month, Day, text-for-phrase, BC1, BC2
-- Make sure that the 'type' and 'subtype' values match the allowed parameters for date objects (see Family Historian API)
-- You can omit unneeded trailing values for a date (e.g. unwanted second set of Year, Month, Day, etc) but if you need
-- to omit a value which is not a trailing value (e.g. unwanted day or month in a date range) set it to 0 or "" as appropriate.
-- Add more dates for further testing if required
g_dates = { 
	{ "Simple", "", 					45, 0, 0, 0, 0, 0, "", true },
	{ "Simple", "", 					1911 },
	{ "Simple", "", 					1912, 1 },
	{ "Simple", "", 					1913, 2,	1 },
	{ "Simple", "Approximate", 	1914 },
	{ "Simple", "Approximate", 	1915, 3 },
	{ "Simple", "Approximate", 	1916,	4, 2 },
	{ "Simple", "Estimated", 		1917 },
	{ "Simple", "Estimated", 		1918, 5 },
	{ "Simple", "Estimated", 		1919, 6, 3 },
	{ "Simple", "Calculated", 		1920 },
	{ "Simple", "Calculated", 		1921, 7 },
	{ "Simple", "Calculated", 		1922, 8, 4 },
	{ "Period", "From-To", 			1923, 0, 0, 1927 },
	{ "Period", "From-To", 			1924, 9, 0, 1927, 10 },
	{ "Period", "From-To", 			1925, 11, 5, 1927, 12, 6 },
	{ "Period", "From", 				1926 },
	{ "Period", "From", 				1927, 1 },
	{ "Period", "From", 				1928, 2, 7 },
	{ "Period", "To", 				1929 },
	{ "Period", "To", 				1930, 3 },
	{ "Period", "To", 				1931, 4, 8 },
	{ "Range",  "Between", 			1932, 0, 0, 1957 },
	{ "Range",  "Between", 			1933, 5, 0, 1957, 6 },
	{ "Range", 	"Between", 			1934, 7, 9, 1957, 8, 10 },
	{ "Range", 	"After", 			1935 },
	{ "Range", 	"After", 			1936, 9 },
	{ "Range", 	"After", 			1937, 10, 11 },
	{ "Range", 	"Before", 			1938 },
	{ "Range", 	"Before", 			1939, 11 },
	{ "Range", 	"Before", 			1940, 12, 12 },
	{ "Phrase", "", 					0, 0, 0, 0, 0, 0, "xxxxxxxxxxxx" },
	{ "Phrase", "", 					1941, 1, 1, 0, 0, 0, "xxxxxxxxxxxx" },
};

-- constants for param indexing (more self-descriptive this way)
g_iDI_Type 		= 1;
g_iDI_Subtype 	= 2;
g_iDI_YY1 		= 3;
g_iDI_MM1 		= 4;
g_iDI_DD1 		= 5;
g_iDI_YY2 		= 6;
g_iDI_MM2 		= 7;
g_iDI_DD2 		= 8;
g_iDI_Phrase 	= 9;
g_iDI_BC1	 	= 10;
g_iDI_BC2	 	= 11;

g_qualifiers = { "LONG", "COMPACT","ABBREV", "ABBREV2", "ABBREV3", "ABBREV4" };

-------------------------------------------------------
-- Entry-point function, called by language pack plugin
-------------------------------------------------------
function Test_FH_GetDateText()

	local sText = "";
	local dtDate = fhNewDate();
	local sQualifier = "LONG";
	local iDateTableLen = #g_dates;
	local iQualTableLen = #g_qualifiers;
	local iCount = 0;

	for iQualIndex = 1, iQualTableLen do
		sQualifier = g_qualifiers[iQualIndex];
		iCount = iCount + 1;

		sText = sText .. "====================================\n";
		sText = sText .. "QUALIFIER = " .. sQualifier .. "\n";
		sText = sText .. "====================================\n";

		sText = sText .. "\n";
		sText = sText .. "    SENTENCE (narrative reports)\n";
		sText = sText .. "    ---------------------------------------\n";
		for iDateIndex = 1, iDateTableLen do
			if InitDate(dtDate, iDateIndex) then
				sText = sText .. "    [" .. dtDate:GetDisplayText("COMPACT") .. "]:    	";
				sText = sText .. GetNarrDisplayText(dtDate, sQualifier) .. "    ==>     	";
				sText = sText .. FH_GetDateText("SENTENCE", dtDate, sQualifier) .. "\n";
			end			
		end

		sText = sText .. "\n";
		sText = sText .. "    PREDICATE (non-narrative reports)\n";
		sText = sText .. "    --------------------------------------------\n";
		for iDateIndex = 1, iDateTableLen do
			if InitDate(dtDate, iDateIndex) then
				sText = sText .. "    [" .. dtDate:GetDisplayText("COMPACT") .. "]:    	";
				sText = sText .. dtDate:GetDisplayText(sQualifier) .. "    ==>     	";
				sText = sText .. FH_GetDateText("PREDICATE", dtDate, sQualifier) .. "\n";
			end
		end

		sText = sText .. "\n";
		sText = sText .. "\n";

	end

	local rt = fhNewRichText(sText, false);
	fhDisplayRichTextBox(rt, "Sample Date Translations");

end


-------------------------------------------------------
-- 
-------------------------------------------------------
function GetDatePt(iYY, iMM, iDD, bBC)
	if iYY == nil then
		iYY = 0;
	end
	if iMM == nil then
		iMM = 0;
	end
	if iDD == nil then
		iDD = 0;
	end

	return fhNewDatePt(iYY, iMM, iDD, false, bBC);
end

-------------------------------------------------------
-- 
-------------------------------------------------------
function InitDate(dtDate, iIndex)

	local rec = g_dates[iIndex];
	local sType = rec[g_iDI_Type];
	local sSubtype = rec[g_iDI_Subtype];
	if sSubtype == nil then
		sSubtype = "";
	end

	if sType == "Period" then
		local dt1 = GetDatePt(rec[g_iDI_YY1], rec[g_iDI_MM1], rec[g_iDI_DD1], rec[g_iDI_BC1]);
		local dt2;
		if sSubtype == "From-To" then
			dt2 = GetDatePt(rec[g_iDI_YY2], rec[g_iDI_MM2], rec[g_iDI_DD2], rec[g_iDI_BC2]);
		end
		if dt2 == nil then
			dtDate:SetPeriod(sSubtype, dt1);
		else
			dtDate:SetPeriod(sSubtype, dt1, dt2);
		end
	elseif sType == "Range" then
		local dt1 = GetDatePt(rec[g_iDI_YY1], rec[g_iDI_MM1], rec[g_iDI_DD1], rec[g_iDI_BC1]);
		local dt2;
		if sSubtype == "Between" then
			dt2 = GetDatePt(rec[g_iDI_YY2], rec[g_iDI_MM2], rec[g_iDI_DD2], rec[g_iDI_BC2]);
		end
		if dt2 == nil then
			dtDate:SetRange(sSubtype, dt1);
		else
			dtDate:SetRange(sSubtype, dt1, dt2);
		end
	elseif sType == "Phrase" then
		local dt1;
		if rec[g_iDI_YY1] ~= 0 then
			dt1 = GetDatePt(rec[g_iDI_YY1], rec[g_iDI_MM1], rec[g_iDI_DD1], rec[g_iDI_BC1]);
			dtDate:SetDatePhrase(rec[g_iDI_Phrase], dt1);
		else
			dtDate:SetDatePhrase(rec[g_iDI_Phrase]);
		end
	elseif sType == "Simple" then
		local dt1 = GetDatePt(rec[g_iDI_YY1], rec[g_iDI_MM1], rec[g_iDI_DD1], rec[g_iDI_BC1]);
		dtDate:SetSimpleDate(dt1, sSubtype);
	else
		fhMessageBox("Unrecognised date type");
		return false;
	end

	return true;
end

-------------------------------------------------------
-- 
-------------------------------------------------------
function GetNarrDisplayText(dtDate, sQualifier)

	local sDate = dtDate:GetDisplayText(sQualifier);
	local sType = dtDate:GetType();
	local sSubtype = dtDate:GetSubtype();

	if sSubtype == nil then
		sSubtype = "";
	end

	local dtDatePt1 = dtDate:GetDatePt1();

	if ((sType == "Simple" and sSubtype ~= "Approximate") or (sType == "Phrase")) then
		if dtDatePt1 ~= nil and not dtDatePt1:IsNull() then
			if dtDatePt1:GetDay() > 0 and sQualifier ~= "ABBREV4" then
				sDate = "on " .. sDate;
			else
				sDate = "in " .. sDate;
			end
		end
	end
	
	return sDate;
end

-------------------------------------------------------
-- Is executed when this plugin is loaded by a language pack plug (at which point g_bLangPackTesterLoaded will be false but not nil)
-- and when this plugin is run directly in the Plugin Window (g_bLangPackTesterLoaded will be nil in that case).
-- In the latter case we just want to warn the user that running this plugin in the Plugin Window is not how it is intended to be used.
-------------------------------------------------------
function main()
	if g_bLangPackTesterLoaded == nil then
		fhMessageBox("This plugin is for the use of language pack authors only, for testing language pack plugins.  " .. 
						"Instructions for using it are stored in the plugin itself, just below the header.");
	end
end


-------------------------------------------------------

main();


Source:Language-Pack-Tester-2.fh_lua