GWT 动态国际化 [英] GWT dynamic internationalization

查看:41
本文介绍了GWT 动态国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序(GWT(EXT_GWT) + Spring)中,我需要设置用户(登录后)他的母语,而不需要获取参数.例如:用户在登录表单中填写他的登录名和密码,然后在准备工作表单中重定向(此表单必须使用本地用户语言.我从 db 获得的区域设置).我的语言文件写在 .properties 文件中,并在 module.gwt.xml 配置中枚举.

In my application(GWT(EXT_GWT) + Spring) I need to set user(after login) his native language, without get params. For example: user filling his login&pass at login form and then redirecting at ready-to-work form (this form must be with native user language. Locale I get from db). My language files written at .properties files and enumerated in module.gwt.xml config.

所以问题是 - 我如何设置语言?也许使用会话或发布参数?但我不明白 GWT 在加载页面之前如何选择所需的语言环境.在页面加载之前在 gwt 设置语言环境的一些方法?

So question is - how I can set language? Maybe with session or post params? But I don't understand how GWT before loading page choose needed locale. Some methods to set locale at gwt before page loaded?

谢谢!

推荐答案

使用内置的国际化并按预期使用,如果不重新加载页面,您将无法做到这一点.内置的 i18n 仅依赖于加载与所选语言环境相对应的编译排列.这意味着您不能只添加元标记,不能只翻转设置,也不能从内置的 LocalInfo 类中读取.

Using the built-in internationalization and using it as intended, you can't do this without reloading the page. The built-in i18n relies on only loading the compiled permutation that corresponds to the selected locale. This means you can't just add a meta tag, you can't just flip a setting, and you won't be able to read from the built in LocalInfo class.

在我看来,有两种选择.首先,使重新加载成本更低,这样您就可以使用 GWT 附带的 i18n 东西.为此,请通过创建一个新应用程序或使用拆分点从登录中拆分应用程序的其余部分,以便加载尽可能少的代码.编译文件应该是缓存,所以它不应该影响下载时间,只是启动应用程序所需的时间.

Two options, as I see it. First, make the reload less expensive, so you can use the i18n stuff GWT comes with. To do this, break out the rest of the app from the login, either by making a new app, or by using split points, so as little code as possible is loaded. Compiles files should be caches, so it shouldn't affect the download time, just the time it takes to start the app.

第二个选项,遵循不重新加载的约束:不要使用内置的 I18n,而是找到另一种基于运行时数据提取字符串的方法(与通常的编译时方法相反).

Second option, following your constraint of not reloading: Don't use the built in I18n, but find another way to pull in strings based on runtime data (as opposed to the usual compile-time approach).

  • GWT 记录了一种使用 Dictionary 类执行此操作的方法,允许您从 javascript 加载的字符串中读取(参见 http://code.google.com/webtoolkit/doc/latest/DevGuideI18n.html#DevGuideDynamicStringInternationalization).这可以通过在登录后加载带有这些字符串的 JS 文件来完成(为此考虑 ScriptInjector,以便您知道它何时准备就绪).如果您认为这是一个优点,那么您不需要重新编译来更改字符串,这具有额外的优点,而缺点是您需要在 JS 中拥有字符串,而不是 java 和属性文件(尽管一些仔细的服务器工作可能允许您可以从 java/properties 生成该 js).
  • 制作多个 I18n 接口,而不仅仅是一个.它们可能都应该实现一个通用接口,这样您就可以将实例传递给任何需要它们的东西、小部件等.这种方法的问题在于,当您使用内置 I18n 时,您正在编译所有字符串,无论它们是否被使用.为了改善这种情况,请考虑为所需的每个区域设置一个拆分点,并确保只加载其中一个.
  • GWT documents one such way of doing this with the Dictionary class, allowing you to read from strings loaded by javascript (see http://code.google.com/webtoolkit/doc/latest/DevGuideI18n.html#DevGuideDynamicStringInternationalization). This could be done by loading a JS file with these strings after login (consider ScriptInjector for this so you know when it is ready). This has the additional advantage that you don't need to recompile to change strings, if you consider that an advantage, and the disadvantage that you need to have the strings in JS, not java and properties files (though some careful server work could allow you to generate that js from java/properties).
  • Make several I18n interfaces instead of just one. They should probably all implement a common interface so you can pass along the instances to anything that needs them, widgets, etc. The problem with this approach is that while you are using the built-in I18n, you are compiling in all of the strings, whether they are used or not. To ameliorate this, consider a split point for each locale required, and make sure you only load one of them.

这篇关于GWT 动态国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆