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

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

问题描述

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



所以问题是 - 我如何设置语言?也许会议或后参数?但我不明白如何GWT加载页面选择需要的区域设置。一些在页面加载之前在gwt上设置locale的方法?



谢谢!

解决方案

使用内置的国际化和使用正如预期的那样,如果不重新加载页面,则无法执行此操作。内置i18n只依赖加载与选定语言环境相对应的编译置换。这意味着你不能只添加一个元标记,你不能只是翻转一个设置,你将无法从内置的LocalInfo类中读取。



<正如我所见,有两种选择。首先,重新加载更少的代价,这样你就可以使用GWT自带的i18n东西了。要做到这一点,通过创建新应用程序或使用分割点,从登录名中分出应用程序的其余部分,以便尽可能少地加载代码。编译文件应该是缓存,所以它不应该影响下载时间,只需要启动应用程序。



第二个选项,遵循不重载:不要使用内置的I18n,而是根据运行时数据找到另一种方法来引入字符串(与通常的编译时方法相反)。


  • GWT记录了一种使用Dictionary类执行此操作的方式,使您可以读取由JavaScript加载的字符串(请参阅 http://code.google.com/webtoolkit/doc/latest/DevGuideI18n.html#DevGuideDynamicString国际化)。这可以通过在登录后使用这些字符串加载JS文件来完成(请考虑ScriptInjector,以便知道它何时准备就绪)。这还有一个好处,就是你不需要重新编译来改变字符串,如果你认为这是一个优点,并且缺点是你需要JS中的字符串,而不是java和属性文件(尽管一些小心的服务器工作可能会允许你从java / properties生成那个js)。
  • 创建几个I18n接口,而不是一个接口。他们可能都应该实现一个通用接口,以便您可以将实例传递给需要它们的任何东西,窗口小部件等。此方法的问题是,当您使用内置I18n时,您正在编译所有字符串,不管它们是否被使用。为了改善这一点,请考虑每个所需区域设置的分割点,并确保只加载其中的一个。


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.

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?

Thanks!

解决方案

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.

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.

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 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天全站免登陆