localization using Jquery

Posted on 2014-12-14
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
  • 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

943 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

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now