is there anyway i can access my resource files (.resx) in javascript?
if no then are there any workarounds to diplay messages in javascript in different languages?
Asked
Active
Viewed 2.3k times
7

scatman
- 14,109
- 22
- 70
- 93
4 Answers
13
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).
Or a javascript solution may be this nice jQuery plugin.

tanathos
- 5,566
- 4
- 34
- 46
4
You can just use double quotes in MVC4.
var e = document.getElementById("myelementID");
e.textContent = "@Resource.MyVariable";
I tested and it worked for me.

matonur
- 41
- 2
4
i did it by using Jquery ajax which calls a serverside GetGlobalResourceObject()

scatman
- 14,109
- 22
- 70
- 93
-
8I hope you're not using this to resolve each and every resource.. That's a nice to way to spam your server. – JefClaes Oct 23 '12 at 13:32
0
Is it a separate javascript file? You can always use an asp literal to insert text into your html page (for exemple: var myString = '<asp:literal id="myLiteral" />';
) and fill the literal serverside.

GôTô
- 7,974
- 3
- 32
- 43