Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


localization using Jquery

Posted on 2014-12-14
Medium Priority
Last Modified: 2015-01-08
I need to make my web app multi lingual
Its currently done using 4.5 webforms and Jquery

I figured I could just name each DOM element with and ID and lookup the translation in an XML file containing the translations

Has anyone done this? and can you shed some light/ examples on the best way to achieve it
performance and being able to do it quickly is key
Ideally it would cache too
Question by:websss
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40499929
In my opinion, if it's a server-side application you should bring the page already translated.
The problem of translating in the client-side is that the user will see labels popping up here and there while the translations are being applied. To avoid this you need to hide the form until the translation process is finished.

Another problem is that you have to load the full translation resource to the client, even if he only uses a part of it. Of course you can implement a partition strategy splitting translations by page but there will always be shared translations across pages and is something you have to deal with.

When user changes language probably you'll need to refresh the whole page anyway depending on the kind of data you're showing so I think for apps, server-side translation approach is the best.
Save the user language in the session and apply it on every page rendering as easy as using the server-side tags in your html:
<%= i18n("") %>

Open in new window

For this to work, just create a static method called i18n or create a base form that all forms inherit from (I like this one better) and implement your logic there, receiving a translation key and returning the translated value.
Store your translations either in textfiles (plain, JSON or XML that you can easily deserialize), database or even resource files it really doesn't matter as long as you abstract the way you fetch this information in your own i18n method.
This way you can even change your translation strategy in the future without the need to mess up with what's already in place.

Just as a justification why to store the translations in the DB, the best argument is if you have reports, specially in SSRS.
You'll want to reuse the same translations in both application and reports.

Author Comment

ID: 40499946
So you say do it server side....but not use Resx files?
I thought this was the obvious option for server side?

I write HTML server side and its generated by Javascript, so it would have to exist on both server and client
I was thinking it would be easier to do in one place

...also about your comment with labels changing.... I can imagine this happening if you are using a default language, i.e. default to english then switch to other language if needed ....instead of having blank values and populating with a specific en/fr/es/de etc  JSON file

What are other down sides about using client side?
LVL 30

Accepted Solution

Alexandre Simões earned 2000 total points
ID: 40500253
I thought this was the obvious option for server side?
Depends on what you are doing. I explained above why it's a good idea to abstract your translation logic and why it might be a good idea to put your translations in the DB.

I write HTML server side and its generated by Javascript, so it would have to exist on both server and client
I was thinking it would be easier to do in one place
You can also request the translations to the backend and receive a JSON object with the key value pairs for the language.
This way, even if you have a mixed internationalization strategy, the translation files are only in one place.
Basically the most part will arrive with the static HTML and only the dynamic part will have to be translated with javascript.
Anyway, and if you're generating HTML in javascript, consider applying the translation directly to the templates when the page loads.

instead of having blank values and populating with a specific en/fr/es/de etc  JSON file
You'll still see the translated values popping up, specially on slower machines.
It's fast but looks clumsy. The best solution if to simply hide the whole thing until the translations are all applied.
At the end it will still look like the page is loading.
Another side effect is that different languages have different text lengths. While applying the translation in javascript, the user will see the page moving and adjusting while the text is being applied.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question