简体   繁体   English

服务器端国际化与客户端?

[英]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.我非常清楚如何组织服务器端国际化——检测语言首选项并在处理请求页面的 HTML 中打印相应的翻译字符串。

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检测 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:我们使用Handlebars ,我们的页面结构如下:

<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?现在,对于 TEXT_1 和 TEXT_2,我必须创建一个单独的模板吗? 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.我无法将Some {{copy}} goes here替换为{{lang.copy_of_template1}}因为{{copy}}不会展开。

So is it word the effort to do client-side i18n at all?那么,做客户端 i18n 的努力到底有没有用呢?

Doing clientside i18n is growing - as implementing clientside applications in web gets more mature. 随着在Web上实现客户端应用程序的日趋成熟,客户端i18n的应用正在增长。

you might get inspired by http://i18next.com/docs/ 您可能会受到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).在我看来,您应该通过服务器端翻译它,因为它更容易处理搜索引擎优化 (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.如果您选择强制服务器端,那么您应该听取Accept-Language标头。 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.然后你的服务器必须在数据库中搜索定义的翻译(我个人确实为此使用了一个 json 文件)并基于它构建文本内容。


There will be times that you are forced to do it in client side such as a javascript generated popup with not specific text.有时您被迫在客户端执行此操作,例如 javascript 生成的没有特定文本的弹出窗口。 But instead of rewriting your translations you should just make an REST call to your server-side translator before showing it.但是,与其重写您的翻译,不如在显示之前对您的服务器端翻译器进行 REST 调用。

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.如果您使用服务器端框架(如 ASP.NET 或 PHP),则可以在服务器端进行本地化。 If you use a client side frameworks such as Angular or React then you do that on the client side.如果您使用 Angular 或 React 等客户端框架,那么您可以在客户端执行此操作。

If your app consumes data from a REST API you might need to do some localization on the server.如果您的应用程序使用来自 REST API 的数据,您可能需要在服务器上进行一些本地化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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