Solved

localization using Jquery

Posted on 2014-12-14
3
82 Views
Last Modified: 2015-01-08
I need to make my web app multi lingual
Its currently done using asp.net 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
0
Comment
Question by:websss
  • 2
3 Comments
 
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 ASP.net 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("user.name") %>

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.
0
 

Author Comment

by:websss
ID: 40499946
Thanks
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?
0
 
LVL 30

Accepted Solution

by:
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now