How to use localized messages in javascript file in asp.net -
How to use localized message in javascript file in asp.net.
I have a javascript file (global-script.js) which includes all my scripts. I use another file (messages.js) which contains all the fixed error messages (Example: " Are you sure you want to delete? ") So that they can be localized.
Is it possible to localize this main script file without a second file (messages.js), so that I can remove a script reference?
The ASP.Net Script Manager controls have the ability to provide some scripts to your script.
If your script is embedded inside you assembly then to find the script manager that you have some local strings stored in that .resx file, Used, want to serve at any time. These strings are injected into the page as JSN objects and then in your main script, instead of the JSN object, instead of literal strings, the output references are called instead. For example, you will embed your script in such a way:
[assembly: system. Web. UI. Web Resource ("ProjectNameSpace.Mscript.JS", "Application / X-Javascript")] [Assembly: System. Web. UI. Script Resource ("Project NameSpace.moscript.js", "ProjectNamespace.MyScriptResources", "Messages")]
"ProjectNamespace.MyScript.js" is the complete path to Embedded Resources which is your script . In ScriptResourceAttribute, the second parameter is the full path to the embedded .resx file (zero .resx extension), which includes all localized messages. You treat it like any other .resx, so you have MyScriptResources.resx for the default culture, then MyScriptResources.es-MX.resx for Mexican Spanish overrides, etc. The last parameter in the ScriptResourceAttribute is the name of the JSON which will be the object that will be generated.
In your script, you refer to JSON objects:
function DoSomething () {warning (message. Error message); }
In the above snippet, "Error Message" is the name of one of the string resources in the .resx file.
If you embed the script, refer to it
Alternatively, you can put local copies of the script entirely , such as " MyScript.js, "" Script from MyScript using a tag. Es-MX.js, "" MyScript.en-UK.js, "etc. Where localized logic and message are correct in the script.
If you do this localization method, then refer to the script manager that specifies a path that uses.
Note that if you are using ASP.NET MVC, then ScriptManager control does not really work with it, in that case you or potentially different Would like to see the solution.
Comments
Post a Comment