Localization in angular4 by fetching data from server using VS2017
1,This is my webapi controller from where the data for localization is fetched
//This is our webApi COntroller
// GET: api/local/5
[HttpGet, Route("api/local/{name}")]public IDictionary
{
Dictionary
dict.Add("user01", "Nombre");//Name
dict.Add("user02", "Nombre del Padre");//Father Name
dict.Add("user03", "Direccion postal");//Postal Address
return dict.ToDictionary(p => p.Key, p => p.Value);
}
2,In our template url file, we will be giving the key of the dictionary object and covering it with a function which will fetch the value from the key from the component
{{name("user01")}}
3,In our component we have to write a get method to fetch the localization values from webapi controller
My Component.ts
//localisation-Json result from the service is passed to a html localstorage where our value is stored
and is used by our html page
this.dataService.getlocal()
.subscribe((item: any[]) => {
if (item != null || item != undefined) {
if (localStorage.getItem("localizationAray") != null) {
localStorage.removeItem("localizationAray");
}
localStorage.setItem("localizationAray", JSON.stringify(item));
}
});
}
//localization -This method is call from template url to get value fro key . We will fetch value from the local storage in html5
name(value: string) {
var retVal = localStorage.getItem("localizationAray");
var OrgVal = JSON.parse(retVal);
if (OrgVal != null || OrgVal != undefined) {
retVal = OrgVal[value];
}
return retVal;
}
My Service.ts
//local- This service fetch dictionary value from webApi controller
getlocal() {
var retVal = localStorage.getItem("userlang").toString();
// return this.http.get('api/local/?name=${retVal}')
//'"' + retVal + '"'
return this.http.get('api/local/' + retVal )
.map((res: Response) => res.json());
}
Please free to comment under for any further clarification.
Comments
Post a Comment