GWT 如何为每个浏览器提供正确的 Javascript 代码,例如进行i18n和浏览器的兼容?

How does GWT provide the correct Javascript code to every browser e.g. to carry out i18n and browser compatibility?

刚接触GWT,进入Vaadin世界后才开始看一看

从维基阅读:https://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_DeferredBindingDefinition

When the GWT Compiler compiles your Java application, it determines all the different "idiosyncrasies" that it must support, and generates a separate, tightly streamlined version of the application for that specific configuration. For example, it generates a different version of the application file for Firefox than it does for Opera.

...

Another classic example of Deferred Binding is internationalization: the GWT Compiler uses Deferred Binding to generate a completely separate version of the application for each language. Why should an English speaker have to download the French text of your application?

GWT 使用延迟绑定并将 Java 代码编译为针对目标浏览器优化的不同 Java 脚本,GWT 还可以为每种语言生成一个 JS 子集以允许 i18n .

现在,关于延迟绑定的解释,这一切都是在编译时完成的,但实际上是在运行时,当有对服务器的传入请求时,用 GWT 编写的应用程序如何知道浏览器是否 Chrome、Firefox 还是 Opera?它是否解析请求的User-Agent header? (我会怀疑它,因为它不太可靠)它是否使用某种 Javascript 'bootstrap' 客户端代码,在运行时快速确定用户的浏览器,然后发出异步请求到服务器以下载适当优化的、特定于语言的 Java脚本代码,用于 那个 具有那个语言环境的浏览器?

魔术是如何发生的?

感谢关注!

Does it uses a sort of a Javascript 'bootstrap' client side code which quickly determines at runtime the browser of the user and then makes an async request to the server to download the proper optimized, language specific Javascript code for that browser with that locale?

事情就是这样。您从 html 页面(使用正常的<script> 标签)。此脚本使用 navigator.userAgent 确定浏览器类型,并从那里动态加载 'browser dependent' 部分(应该被缓存,因为它可能很大)。

在编译时,GWT 确定需要多少排列(外部因素的组合,例如浏览器模型、资源语言等)。然后为每个组合生成一个结果 javascript 模块。
虽然您会部署所有这些文件,但每个客户在访问您的网站时只会下载与其相关的文件。