Hi all.
I am trying to internationalize my web application wich I developed in MVC 3.0.
I can read my values in .resx files from cshtml, but how can I read these values(in .resx files) from my .js files.
Is there any way to serialize or convert .resx files in JSON objects(in controllers in server side) and return that object.After that in this object I can read all ky/value pairs wothout any problem.
Is this possible or do you know any better solution of this problem.
Generaly asking: How to access values in .resx files from clients site javascript files .js ?
Thank You for reading this question and for everyone who can help me.
Thank You for your reply.You helped me so much "when my javascript is in the page" and I tried it.It works great.
...but my problem still is about accessing resx files "when my javascript is not in the page but it is in a .js file".How can I read resx strings from .js files.
I read before this tutorial and all microsoft1s tutorials but when they want to use .resx files in .js files then they use JQuery.
I want to do the same thing, but without JQuery.
well I understand it partially. As I could understand I have to:
1.Make seperate js files for every language for example: en.js, en-us.js fr-FR.js, etc, with all key.value pairs for every language.
2. then I shopuld use these files in all other js files in my application.
but I didn`t understand that part "In the separate js file you could do a json (or ajax call) to get the translations for the culture."
Why should I do that JSON call ?
I am sorry but as you can see I don`t have it all clear.
in these js files shold I have JSON objects like this for example:
var Strings={
confirmMessage:{
'en/US':'Are you sure?',
'fr/FR':'Est-ce que vous est certain?',
...
},
...
}
Thank You indeed for reading and trying to help me.
aha now I understand how to create files and inside them how to create JSON objects.
Now to be able to read from this file I need only to reference them(like all javascript files) in my view right ?
If so...why do I need an ajax call, because we know that with ajax we make requests in server from client side.Now if I reference these javascript files in my view then I suppose I don`t need ajax requests, or do I ?
Thank You for your patient.I really appreciate your help.
Correct. it was just 2 different approaches. You don't need ajax call if you don't need them.
When HTML 5, Client-side Storage becomes available, you could do an 1 ajax call in document.ready and store the translations in client storage and read this afterwards in javascript.
Marked as answer by drilli on Apr 12, 2012 11:28 AM
drilli
Member
18 Points
24 Posts
Pass resx files in client side as JSON or Javascript objects
Apr 10, 2012 07:29 AM|LINK
Hi all.
I am trying to internationalize my web application wich I developed in MVC 3.0.
I can read my values in .resx files from cshtml, but how can I read these values(in .resx files) from my .js files.
Is there any way to serialize or convert .resx files in JSON objects(in controllers in server side) and return that object.After that in this object I can read all ky/value pairs wothout any problem.
Is this possible or do you know any better solution of this problem.
Generaly asking: How to access values in .resx files from clients site javascript files .js ?
Thank You for reading this question and for everyone who can help me.
Bieters
Member
229 Points
48 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 10, 2012 08:50 AM|LINK
You can make localized version of the js file
for example:
localization.[de|en|fr|...].js
drilli
Member
18 Points
24 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 10, 2012 11:24 AM|LINK
...Sorry but I don`t understand exactly what do you think.I need to use my strings from .resx (in server side) in javascript files.
Explain me in more dtails please, if you can :)
Young Yang -...
All-Star
21343 Points
1818 Posts
Microsoft
Re: Pass resx files in client side as JSON or Javascript objects
Apr 12, 2012 03:55 AM|LINK
Hi
If your javascript is in the page you can use:
var globalResource = '<%= Resources.YourClass.YourResourceKey %>';
to access the Global Resources (/App_GlobalResources). Or
var localResource = '<%= GetLocalResourceObject("LocalResourceKey").ToString() %>';
to access the Local resources (/App_LocalResources relative to the page position).
You could also look at this tutorials.
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
drilli
Member
18 Points
24 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 12, 2012 07:16 AM|LINK
Thank You for your reply.You helped me so much "when my javascript is in the page" and I tried it.It works great.
...but my problem still is about accessing resx files "when my javascript is not in the page but it is in a .js file".How can I read resx strings from .js files.
I read before this tutorial and all microsoft1s tutorials but when they want to use .resx files in .js files then they use JQuery.
I want to do the same thing, but without JQuery.
Is this possible?
Bieters
Member
229 Points
48 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 12, 2012 08:18 AM|LINK
I guess you could
- pput a hidden field on the view with the culture.
In the separate js file you could do a json (or ajax call) to get the translations for the culture.
- Have seperate js files for each language with hardcoded strings and couple the right one to the view when rendering the view
Maybe other solutions ...
drilli
Member
18 Points
24 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 12, 2012 09:11 AM|LINK
well I understand it partially. As I could understand I have to:
1.Make seperate js files for every language for example: en.js, en-us.js fr-FR.js, etc, with all key.value pairs for every language.
2. then I shopuld use these files in all other js files in my application.
but I didn`t understand that part "In the separate js file you could do a json (or ajax call) to get the translations for the culture."
Why should I do that JSON call ?
I am sorry but as you can see I don`t have it all clear.
in these js files shold I have JSON objects like this for example:
var Strings={
Thank You indeed for reading and trying to help me.
P.S. I am using asp.net MVC 3
Bieters
Member
229 Points
48 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 12, 2012 09:25 AM|LINK
You don't have to do both.
It's create separate js files for each language
or
One js file in which you get translations with json call.
drilli
Member
18 Points
24 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 12, 2012 10:49 AM|LINK
aha now I understand how to create files and inside them how to create JSON objects.
Now to be able to read from this file I need only to reference them(like all javascript files) in my view right ?
If so...why do I need an ajax call, because we know that with ajax we make requests in server from client side.Now if I reference these javascript files in my view then I suppose I don`t need ajax requests, or do I ?
Thank You for your patient.I really appreciate your help.
Bieters
Member
229 Points
48 Posts
Re: Pass resx files in client side as JSON or Javascript objects
Apr 12, 2012 11:22 AM|LINK
Correct. it was just 2 different approaches. You don't need ajax call if you don't need them.
When HTML 5, Client-side Storage becomes available, you could do an 1 ajax call in document.ready and store the translations in client storage and read this afterwards in javascript.