4

I would like to do client side localization (i.e embedding the javascript and resource files). Everything was working fine until we had both the js file and the resource file in the same project("XXX.Web").

As a part of a requirement, I had to move out all the resource files to "XXX.LocalizedResources" project, while I still need to do client side localization for the js file.

I tried the following in the AssemblyInfo.vb of the Web project:

Before:

Assembly: System.Web.UI.WebResource("XXX.Web.GlobalStrings.js", "text/javascript")

Assembly: System.Web.UI.ScriptResource("XXX.Web.GlobalStrings.js", "XXX.Web.Resources", "Resources")

After:

Assembly: System.Web.UI.WebResource("XXX.Web.GlobalStrings.js", "text/javascript")

Assembly: System.Web.UI.ScriptResource("XXX.Web.GlobalStrings.js", "XXX.LocalizedResources.Resources", "Resources")

(Please ignore the syntactical errors in the above lines ) And the error I get now is the following:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "XXX.LocalizedResources.Resources" was correctly embedded or linked into assembly "XXX.Web" at compile time, or that all the satellite assemblies required are loadable and fully signed.

From little researching I came across that only web or web.extension projects could be used in "Assembly: System.Web.UI.ScriptResource (...)".

I tried adding "Assembly: InternalsVisibleTo("XXX.Web")" in AssemblyInfo of "XXX.LocalizedResources" but that doesn't help in making the resource file open to "XXX.Web". Also the resource files are already public and embed option is set too.

Any idea how I could embed an external resource file into a javascript for client side localization?

1 Answers1

0

Include this in your page:

<script>
//declare string variable defaults here
function UrlExists(url){
var http = new XMLHttpRequest();
try{http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}catch(e){return false;}
}

var lang=navigator.userLanguage || navigator.language.toLowerCase();
var langsrc   = document.createElement("script");
langsrc.type  = "text/javascript";

if(UrlExists(lang+".js")){
   langsrc.src=lang+".js";
}else{
   langsrc.src="en-us.js";
}
document.body.appendChild(langsrc);
//rest of code here:
</script>

then for each supported language, set up a file such as en-us.js

//en-us.js localization for....
//set language specific variables here
//define language specific functions    

This file goes in the same directory as your page and can be shared by all pages in that directory (with modifications it could do multiple directories or sites or only one file, but keeping it simple so you can season to taste)

I would recommend using a JSON object for your variables if possible though so that you can share a single localization file with your server side (php for instance) You may need to include a second file for any localized javascript functions if you need any.

Donnelle
  • 5,689
  • 3
  • 27
  • 31
technosaurus
  • 7,676
  • 1
  • 30
  • 52