I have a bare bones class:
internal class CLMExplorerTranslations
{
// LBL_BROTHER
public string Brother { get; set; }
// LBL_SISTER
public string Sister { get; set; }
// LBL_ABBREV_CHAIR
public string Chairman { get; set; }
// LBL_ABBREV_TREAS_TALK
public string TreasuresTalk { get; set; }
// LBL_ABBREV_TREAS_DIG
public string SpiritualGems { get; set; }
// LBL_ABBREV_TREAS_READ
public string BibleReading { get; set; }
// LBL_TALK
public string Talk { get; set; }
// LBL_ABBREV_DEMO
public string Demonstration { get; set; }
// LBL_ABBREV_ASST
public string Assistant { get; set; }
// LBL_ABBREV_LIVING
public string Living { get; set; }
// LBL_ABBREV_CBS
public string ConductorCBS { get; set; }
// LBL_ASSIGNMENT_CBS_READ
public string ReaderCBS { get; set; }
// LBL_ABBREV_PRAY
public string Prayer { get; set; }
public CLMExplorerTranslations()
{
Brother = "Brother";
Sister = "Sister";
Chairman = "Chair";
TreasuresTalk = "Treas. Talk";
SpiritualGems = "Spiritual Gems";
BibleReading = "Bible Read";
Talk = "Talk";
Demonstration = "Demos";
Assistant = "Asst";
Living = "Living";
ConductorCBS = "CBS";
ReaderCBS = "CBS Reader";
Prayer = "Pray";
}
}
As you can see, it is very simple. The constructor initializes the properties with English values. I then have a public method which is passed a language code as a string. This in turn updates the properties. For example:
public void InitTranslations(string langCode)
{
if (langCode == "AFK")
{
Brother = "Broer";
Sister = "Suster";
Chairman = "Voors.";
TreasuresTalk = "Skatte toespr.";
SpiritualGems = "Skatte soek";
BibleReading = "Skatte leesged.";
Talk = "Toespraak";
Demonstration = "Demon.";
Assistant = "Asst";
Living = "Lewe";
ConductorCBS = "GBS";
ReaderCBS = "GBS leser";
Prayer = "Geb.";
return;
}
if (langCode == "CHS")
{
Brother = "弟兄";
Sister = "姊妹";
Chairman = "主席";
TreasuresTalk = "宝藏";
SpiritualGems = "挖掘";
BibleReading = "朗读";
Talk = "演讲";
Demonstration = "示范";
Assistant = "助手";
Living = "生活";
ConductorCBS = "研经班";
ReaderCBS = "课文朗读者";
Prayer = "祷告";
return;
}
// More
}
There are a total of 26 if
clauses for 26 different languages. The translations get set once and then don't need changing again.
It functions fine but is there a simpler way to manage this that does not end up with a function being some 500 lines long?
This class is part of a DLL library/
Context
It isn't for a GUI. A part of my DLL is using CvsHelper
to read a CSV document. One of the fields in the CSV has several values with its own delimiter. I split this single field into a list of values and then need to parse the values to identify what each are. The user states what the language of the CSV file will be, so that I know what values to test for. Then when I find the matches I can convert into my own enumerated values for my own classes to use.
In the comments it has been suggested that I use embedded resources. But it is not clear how to do this given the above context. Eg. if I pass CHS
to the function then it would need to retreive the CHS
embedded resource values.
I see there are more comments just added for me to review.
Update
As per one of the answers I am trying to add single JSON files. Thought I would start with English. I tried adding a function to my DLL to obtain the translations:
private CLMExplorerTranslations GetTranslations(string languageCode)
{
using var stream = typeof(MSAToolsLibraryClass).Assembly.GetManifestResourceStream(
$"MSAToolsLibrary.Resources.language-{languageCode}.json"); using var reader = new StreamReader(stream, Encoding.UTF8); return JsonSerializer.Deserialize(reader.ReadToEnd()); }
But it gives me two problems:
- error CS8370: Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0 or greater.
- error CS1503: Argument 1: cannot convert from string to Newtonsoft.Json.JsonReader.
Update 2
Sorted the first error by tweaking the code:
private CLMExplorerTranslations GetTranslations(string languageCode)
{
using (var stream = typeof(MSAToolsLibraryClass).Assembly.GetManifestResourceStream($"MSAToolsLibrary.Resources.language-{languageCode}.json"))
using (var reader = new StreamReader(stream, Encoding.UTF8))
return JsonSerializer.Deserialize<CLMExplorerTranslations>(reader.ReadToEnd());
}
But still have the second error with this line:
JsonSerializer.Deserialize<CLMExplorerTranslations>(reader.ReadToEnd());
Update 3
I got it to work with the individual JSOn files by using:
private CLMExplorerTranslations GetTranslations(string languageCode)
{
using (var stream = typeof(MSAToolsLibraryClass).Assembly.GetManifestResourceStream(
$"MSAToolsLibrary.Resources.language-{languageCode}.json"))
using (var reader = new StreamReader(stream, Encoding.UTF8))
return JsonConvert.DeserializeObject<CLMExplorerTranslations>(reader.ReadToEnd());
//return JsonSerializer.Deserialize<CLMExplorerTranslations>(reader.ReadToEnd());
}