1 Reply Latest reply on Jun 25, 2012 10:25 AM by Geoffrey Poremba

    Get localization values from JS

      To give multilanguage feature to our applicaction, we implemented a Localization module with one file .rb for each language. Each file contains all literals needed:

       

      module Localization

      Views = {

        :admin_boton_aceptar => "Aceptar",

        :admin_boton_salir => "Salir"

      }

      end

       

      From a view, we access the values by the call:     <%= Localization::Views[:admin_boton_aceptar] %>

       

      But we want to access to this literals also from a .js file.

      Is there a JS API function to get the values?

       

      Thanks,

      Oesia.

        • Re: Get localization values from JS

          If you want to access data from javascript, you can create a controller that instead of calling a view, it just returns raw data json encoded.

           

          So what you would do in your controller is fetch your data, then put it into json, then return it.  the code would probably look something like this:

           

          // assume your model is called 'my_model'

          data = my_model.find()

          // generate json

          // make sure to require 'json' in the controller and to include json in your extensions in your build.yml file

          json_data = ::JSON.generate(data)

          // return the string from the controller

          render :string => json_data.to_s

           

          If this controller is in the 'Localization' module, and the action is called 'localize', you would make an ajax call from javascript to here:

          /app/Localization/localize

          and the response should be json.  If you make a json request, the app should automatically know not to include the layout, but check the response to make sure.

           

          Hope this helps.