简体   繁体   中英

Server-side internationalization vs client-side?

I am pretty much aware how to organize server-side internationalization - detect the language preferences and print the corresponding translated strings in the HTML of the requested page while processing it.

But shouldn't it be more appropriate to do it on the client? How should it be organized? I am thinking of

  1. detecting the language preferences in JavaScript
  2. request the language file
  3. print the strings on the page

I am having problems figuring out the last part: printing the strings on the page. We use Handlebars and our page structure is something as:

<body>
<h1>TEXT_1</h1>
<script type="text/template">
Some {{copy}} goes here
</script>
<p>TEXT_2</p>
<script type="text/template">
Some other {{othercopy}} goes here
</script>
... and so on

Now, for TEXT_1 and TEXT_2 I would have to create a separate template? How would I translate the strings in the templates? I can't replace Some {{copy}} goes here with {{lang.copy_of_template1}} because {{copy}} wouldn't be expanded.

So is it word the effort to do client-side i18n at all?

Doing clientside i18n is growing - as implementing clientside applications in web gets more mature.

you might get inspired by http://i18next.com/docs/

In my opinion, you should translate it via server-side because it's easier to deal with Search Engine Optimization (SEO).

When you do it in client-side, a slow connection might have more dificulty to see it. I have seen some sites that translates everything after some secs, changing all the content, thats terrible for viewers!

If you chose for the server side of the force, then you should listen to the Accept-Language header. Then your server must search for defined translations on a database (personally i do use a json file for that) and build the text content based on that.


There will be times that you are forced to do it in client side such as a javascript generated popup with not specific text. But instead of rewriting your translations you should just make an REST call to your server-side translator before showing it.

This depends on the framework you use. If you use a server side framework such as ASP.NET or PHP then you do the localization on the server side. If you use a client side frameworks such as Angular or React then you do that on the client side.

If your app consumes data from a REST API you might need to do some localization on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM