Code Samples - i18n

ATTENTION: To run JClientPages samples you need Java installed.

i18n Sample

This is a non-i18n-ized Hello World. Click the buttons below to translate this message




This sample uses simple properties files for i18n, whose names are bundle_[de|en|es|fr|it].properties and that are located in the same folder of this page.

WARINIG: this website bandwidth is a little bit narrow, so page loading and initialization can be a bit slow. Please be patient if your FIRST interaction with the demo takes a bit of time to complete.

Source Code

   <script type="text/java">

   private ResourceBundle bundle;

   public void loadBundle(String localeName){
      Locale locale = new Locale(localeName);
      bundle = ResourceBundle.getBundle("bundle", locale);
   }

   public void i18n(){
      if(bundle!=null){
         i18n(getElementById("samplebody"));
      }
   }

   private void i18n(ClientPageNode node){
      String id = null;
      try{
         id = node.getAttribute("id");
      }catch(Exception e){}

      boolean changed = false;

      if(id!=null && (!id.equals("null")) && !"undefined".equals(id)){
         String localized = null;
         try{
            localized = bundle.getString(id);
         }catch(Exception e){}

         if(localized!=null){
            node.setInnerHTML(localized);
            changed = true;
         }
      }

      if((!changed) && node.isTag()){
         for(int i=0; i<node.getChildren().length; i++){
            ClientPageNode child = node.getChildren()[i];             i18n(child);
         }
      }
   }

   public void changeLocale(String locale){
      loadBundle(locale);
      i18n();
   }
   </script>

   <span id="samplebody">
   <div id="hellomessage">
   This is a non-i18n-ized Hello World. Click the buttons below to translate this message
   </div>
   
   <br/><br/>
   <input type="button" value="de" onclick="changeLocale('de')"/>
   <input type="button" value="en" onclick="changeLocale('en')"/>
   <input type="button" value="es" onclick="changeLocale('es')"/>
   <input type="button" value="fr" onclick="changeLocale('fr')"/>
   <input type="button" value="it" onclick="changeLocale('it')"/>
   </span>
bundle_fr.properties
hellomessage=Bonjour Monde!