title: Using same resource bundle in Java as well as JavaScript using (jquery.i18n.properties plugin)
body: <p>I have a J2EE application where I need to implement internationalization ( I'm not using any frameworks, it's kind of a legacy application). </p>
  
  <p>My Java code currently is something like this:</p>
  
  <pre><code>public String getString(Locale locale, String key){
      ResourceBundle bundle
              = ResourceBundle.getBundle("com.mycode.labels", locale);
      return bundle.getString(key);
  }
  </code></pre>
  
  <p>As you can see the resource files are in the java class path, so that i can use the class path for the getBundle's argument.</p>
  
  <p>But now, I want to use the same  resource bundle from javascript. I figured I could use the JQuery.i18n.properties plugin like this:</p>
  
  <p>First we initialize it like : </p>
  
  <pre><code>var lang = 'en'; // which can be en_US or anything, should be dynamic
  jQuery.i18n.properties({
      name:'labels', 
      path:'Bundle/', 
      mode:'map',
      language: lang
  }); 
  </code></pre>
  
  <p>Then use a global function like ( for the example, otherwise i'd use it within my own libs, it shouldn't matter here.) :</p>
  
  <pre><code>function geti18nString(key){
      return jQuery.i18n.prop(key);
  }
  </code></pre>
  
  <p>This works fine if i keep a copy of the same property files i kept in my class path again in the folder where jsps are [ files may be like labels.properties, labels_en_US, and so on)</p>
  
  <p><strong>Is there a way we can use a single copy of the property files kept in just one place?</strong></p>
  
  <p>ie, if we kept it in the class path, it should be available over the url for the jquery plugin to use it ( ie, not just one file, it is picked up with different locales given to the 'language')</p>
  
  <p>if we kept it in the web folder ( where the jsps are ), ResourseBundle.getBundle() should be able to pick it up from there.</p>
  
  <p>I think we can use some code like :</p>
  
  <pre><code>String filePath  = getServletContext().getRealPath("Bundle/");
  File file = new File(filePath);
  URL[] urls = {file.toURI().toURL()};
  ClassLoader loader = new URLClassLoader(urls);
  ResourceBundle rb = ResourceBundle.getBundle("labels", Locale.getDefault(), loader);
  </code></pre>
  
  <p>... if the code is written in a servlet. But that's not always the case here.</p>
  
  <p>It would be great if you could guide me in the right path. Probably I'm doing something wrong.</p>