Hello. We can read data from local resource files in WCF service as follows:
The below code snippet is a function "translations" which returns a JavaScript object of all the text read from "default.resx" resource file
//Reading data from resource file
public string translations()
{
//1- Assign variables for key and values and xml document and node which will identify a text from resource node
string key =
"", value =
"";
XmlDocument loResource;
XmlNodeList elemList;
//Reading data from ResourceFiles
#region translations
loResource =
new XmlDocument();
loResource.Load(
HostingEnvironment.MapPath(
"~") +
"App_LocalResources\\default.resx");
elemList = loResource.GetElementsByTagName(
"data");
//2- Read Data from each data node
nameDictionary =
new Dictionary<
string,
string>();
for (
int i = 0; i < elemList.Count; i++)
{
key = elemList[i].Attributes[
"name"].InnerText.Trim();
value = elemList[i].InnerText.Trim();
nameDictionary.Add(key, value);
}
#endregion
//----End Translations
//3- Return serialized JSON object of key/value pair dictionary
return serializer.Serialize(nameDictionary);
}
Sample Output:
{"btnBegin":"Begin","btnFilter":"Filter","btnSave":"Save","btnStartIssue":"Report Issue","btnStartTask":"Start Task","ltrAddProject":"Add Project","ltrAddResource":"Add Resource","ltrCloseIssue":"Close Issue","ltrDetail":"Task Detail","ltrDuration":"Duration In","ltrEndTask":"End Task","ltrIssue":"Issue Title" ,"ltrLinkage":"Linkage", "ltrPassword":"Password","ltrPowererdBy":"Powered by Wavetec","ltrPRLinkage":"Project-Resource Linkage" ,"ltrProject":"Project","ltrProjectName":"ProjectName","ltrResource":"Resource","ltrResourceName":"ResourceName", "ltrResourceType":"ResourceType","ltrStartDate":"StartDate","ltrTask":"Task Title","ltrTaskMng":"Task Management","ltrUserName":"UserName"}