前言

Dropwizard官方文档并没有提供国际化的模块,所以只能自己加。Spring的MessageResource用的很顺手,所以copy过来。

Easy i18n

在整合Dropwizard的时候,多语言貌似只能通过jdk自带的ResourceBundle拿数据。其实也就够了,但在开发过程中发现需要缓存,需要解析占位符等。代码越写越多,显然不是仅仅一个调用就完事的。写的差不多的时候突然觉得和spring context里的message source结构类似。于是,放弃维护已经开始变的复杂的逻辑,直接使用spring。

但选取dropwizard的时候就是摒弃了spring,再拿过来也不好玩了。干脆,抽取Spring context项目的MessageResource相关代码,重写封装了一个library: https://github.com/Ryan-Miao/easy-i18n, 欢迎star。

easy-i18n还是和在Spring项目中相同。

首先,引入依赖,由于github项目的library已经有仓库去维护了,就没费心思放到maven和jcenter了,直接从github上拉取。类库地址为:

  1. <repositories>
  2. <repository>
  3. <id>jitpack.io</id>
  4. <url>https://jitpack.io</url>
  5. </repository>
  6. </repositories>

引入

  1. <dependency>
  2. <groupId>com.github.Ryan-Miao</groupId>
  3. <artifactId>easy-i18n</artifactId>
  4. <version>1.0</version>
  5. </dependency>

简单使用

#情形一 只有一个Resource Bundle

在resources下新建i18n/messages.properties以及i18n/messages_zh_CN.properties. demo位置:l4dropwizard

然后,调用方法如下:

  1. @Test
  2. public void testI18n(){
  3. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  4. messageSource.addBasenames("i18n/messages");
  5. messageSource.setDefaultEncoding("UTF-8");
  6. String index = messageSource.getMessage("index", null, Locale.US);
  7. System.out.println(index);
  8. }

#情形二 我有多个Resource Bundle

实际项目中,由于产品分类,有时候需要创建多个Resource Bundle,这时候也简单,只要创建多个ResourceBundleMessageSource来读取翻译即可。


  1. public void testI18n2(){
  2. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  3. messageSource.addBasenames("i18n/messages");
  4. messageSource.setDefaultEncoding("UTF-8");
  5. String index = messageSource.getMessage("index", null, Locale.US);
  6. System.out.println(index);
  7. ResourceBundleMessageSource messageSource2 = new ResourceBundleMessageSource();
  8. messageSource.addBasenames("i18n/messages2");
  9. messageSource.setDefaultEncoding("UTF-8");
  10. String second = messageSource.getMessage("second", null, Locale.US);
  11. System.out.println(second);
  12. }

#情形三 我有多个Resource Bundle但读取翻译的时候我想一起

有时候,想要读取翻译,可能翻译文件在不同的Resource Bundle,但我指向用一个接口去调用。这时候,做法时候在这几个Resource Bundle的里面添加命名空间,即key要在这几个Resource Bundle里唯一,而不仅仅是本文件唯一。

然后,


  1. public void testI18n2(){
  2. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  3. messageSource.addBasenames("i18n/messages", "i18n/messages2");
  4. messageSource.setDefaultEncoding("UTF-8");
  5. String index = messageSource.getMessage("index", null, Locale.US);
  6. System.out.println(index);
  7. }

这种做法,会一次从两个Resource Bundle里寻找翻译,找到即返回。因此,如果有相同的key,将导致只有第一个生效。

#情形4

没有了,你翻译要那么复杂吗。

更多用法,参考测试类:ResourceBundleMessageSourceTest

Demo source

https://github.com/Ryan-Miao/l4dropwizard

本文是基于dropwizard入门之上的演进。

确保依赖都是最新的,或者自行解决版本冲突,比如jackson不同版本之间的类有所不同。

引入easy-i18n

repository url

  1. <repositories>
  2. <repository>
  3. <id>jitpack.io</id>
  4. <url>https://jitpack.io</url>
  5. </repository>
  6. </repositories>

引入

  1. <dependency>
  2. <groupId>com.github.Ryan-Miao</groupId>
  3. <artifactId>easy-i18n</artifactId>
  4. <version>1.0</version>
  5. </dependency>

添加Resource Bundle

resources下新增文件夹i18n, 依次添加几个Resource Bundle。具体做法是,在文件夹i18n右键 -> new -> Resource Bundle, 然后选择想要支持的语言。比如美国en_US,简体中文zh_CN

新建MessageService

创建一个Util来处理翻译功能。

com.test.domain.service.IMessageService

  1. package com.test.domain.service;
  2. import java.text.MessageFormat;
  3. import java.util.List;
  4. import java.util.Locale;
  5. /**
  6. * The Message translation service
  7. * Created by Ryan Miao on 11/23/17.
  8. */
  9. public interface IMessageService {
  10. /**
  11. * Get translation by message key.
  12. *
  13. * @param key The message key in the properties
  14. * @return the translated message
  15. */
  16. String getMessage(String key, Locale locale);
  17. /**
  18. * Get translation by message key and compose it with variables.
  19. * Note that the variable would be injected by {@link MessageFormat}
  20. *
  21. * @param key The message key in the properties
  22. * @param args The variables to inject into the message.
  23. * @return the translated message.
  24. */
  25. String getMessage(String key, List<String> args, Locale locale);
  26. }

实现类com.test.domain.service.impl.MessageService

  1. package com.test.domain.service.impl;
  2. import com.miao.easyi18n.support.ResourceBundleMessageSource;
  3. import com.test.domain.service.IMessageService;
  4. import javax.inject.Inject;
  5. import javax.inject.Singleton;
  6. import java.util.List;
  7. import java.util.Locale;
  8. /**
  9. * Created by Ryan Miao on 11/23/17.
  10. */
  11. @Singleton
  12. public class MessageService implements IMessageService{
  13. private final ResourceBundleMessageSource messageSource;
  14. @Inject
  15. public MessageService(ResourceBundleMessageSource messageSource) {
  16. this.messageSource = messageSource;
  17. }
  18. @Override
  19. public String getMessage(String key, Locale locale) {
  20. return messageSource.getMessage(key, null, locale);
  21. }
  22. @Override
  23. public String getMessage(String key, List<String> args, Locale locale) {
  24. return messageSource.getMessage(key, args.toArray(), locale);
  25. }
  26. }

在IoC中提供ResourceBundleMessageSource

由于ResourceBundleMessageSource是公共组件,需要单独提取出来,并使用单例模式创建。关于IoC的配置,参阅dropwizard中添加DI

ConfigurationModule中:

  1. package com.test.domain.ioc.module;
  2. import com.miao.easyi18n.support.ResourceBundleMessageSource;
  3. import com.test.configuration.HelloWorldConfiguration;
  4. import dagger.Module;
  5. import dagger.Provides;
  6. import javax.inject.Singleton;
  7. /**
  8. * Created by Ryan Miao on 11/20/17.
  9. */
  10. @Module
  11. public class ConfigurationModule {
  12. private final HelloWorldConfiguration configuration;
  13. public ConfigurationModule(HelloWorldConfiguration configuration) {
  14. this.configuration = configuration;
  15. }
  16. @Provides
  17. @Singleton
  18. HelloWorldConfiguration helloWorldConfiguration(){
  19. return configuration;
  20. }
  21. @Singleton
  22. @Provides
  23. ResourceBundleMessageSource resourceBundleMessageSource(){
  24. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  25. messageSource.addBasenames("i18n/messages", "i18n/messages2", "i18n/otherGroup");
  26. messageSource.setDefaultEncoding("UTF-8");
  27. return messageSource;
  28. }
  29. }

这里,关于Resource Bundle的位置没有单独提出来,后面可以放到HelloWorldConfiguration,提到配置文件中。

测试

在dagger中,接口和实现类的绑定只能通过手动声明。因此,绑定IMessageService

  1. @Singleton
  2. @Provides
  3. IMessageService messageService(MessageService messageService){
  4. return messageService;
  5. }

创建测试Resource, com.test.domain.resource.LocalResource

  1. package com.test.domain.resource;
  2. import com.codahale.metrics.annotation.Timed;
  3. import com.google.common.collect.ImmutableMap;
  4. import com.test.domain.entiry.GithubUser;
  5. import com.test.domain.service.IMessageService;
  6. import io.swagger.annotations.Api;
  7. import io.swagger.annotations.ApiOperation;
  8. import io.swagger.annotations.ApiResponse;
  9. import io.swagger.annotations.ApiResponses;
  10. import javax.inject.Inject;
  11. import javax.validation.Valid;
  12. import javax.validation.constraints.NotNull;
  13. import javax.validation.constraints.Pattern;
  14. import javax.ws.rs.GET;
  15. import javax.ws.rs.HeaderParam;
  16. import javax.ws.rs.Path;
  17. import javax.ws.rs.PathParam;
  18. import javax.ws.rs.Produces;
  19. import javax.ws.rs.core.MediaType;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. /**
  23. * Test localization
  24. * Created by Ryan Miao on 11/23/17.
  25. */
  26. @Api("/local")
  27. @Path("/local")
  28. @Produces(MediaType.APPLICATION_JSON)
  29. public class LocalResource {
  30. private final IMessageService messageService;
  31. @Inject
  32. public LocalResource(IMessageService messageService) {
  33. this.messageService = messageService;
  34. }
  35. @GET
  36. @Timed
  37. @Path("/{key}")
  38. @ApiOperation(value = "Get github user profile.", notes = "There should be the note.")
  39. @ApiResponses({
  40. @ApiResponse(code = 401, message = "Valid credentials are required to access this resource."),
  41. @ApiResponse(code = 400, message = "Params not valid."),
  42. @ApiResponse(code = 500, message = "Something wrong from the server."),
  43. @ApiResponse(code = 200, message = "Success.", response = GithubUser.class)
  44. })
  45. public Map<String, String> getIndex(
  46. @PathParam("key") final String index,
  47. @HeaderParam("Accept-Language") @Valid
  48. @NotNull(message = "cannot be null.")
  49. @Pattern(regexp = "([a-z]{2}-[A-Z]{2})", message = "pattern should like zh-CN, en-US.")
  50. final String language
  51. ) {
  52. final Locale locale = Locale.forLanguageTag(language);
  53. final String message = messageService.getMessage(index, locale);
  54. return ImmutableMap.of(index, message);
  55. }
  56. }

结果



使用dropwizard(6)-国际化-easy-i18n的更多相关文章

  1. Java国际化(i18n)

    Java国际化(i18n) 最近在做一个网站国际化的功能.用Java做开发,使用spring+velocity. Java提供了对i18n的支持,spring对其做了集成,可以很方便的配置.主要思想就 ...

  2. 国际化支持(I18N)

    本章译者:@nixil 使用国际化支持(I18N)能够使你的应用根据用户所在地区的不同选择不同的语言.下面介绍如何在引用中使用国际化. 只允许使用UTF-8 Play只支持UTF-8一种字符编码.这是 ...

  3. java框架篇---Struts2 本地化/国际化(i18n)

    国际化(i18n)是规划和实施的产品和服务,使他们能很容易地适应特定的本地语言和文化的过程中,这个过程被称为本地化.国际化的过程有时也被称为翻译或本地化启用.国际化是缩写i18n,因为我和两端用n字打 ...

  4. 【转】jQuery之前端国际化jQuery.i18n.properties

    jQuery之前端国际化jQuery.i18n.properties 基于jQuery.i18n.properties 实现前端页面的资源国际化 jquery-i18n-properties

  5. java框架篇---Struts2 本地化/国际化(i18n)(转)

    源地址:https://www.cnblogs.com/oumyye/p/4368453.html 国际化(i18n)是规划和实施的产品和服务,使他们能很容易地适应特定的本地语言和文化的过程中,这个过 ...

  6. [Ruby on Rails系列]4、专题:Rails应用的国际化[i18n]

    1. 什么是internationalization(i18n)? 国际化,英文简称i18n,按照维基百科的定义:国际化是指在设计软件,将软件与特定语言及地区脱钩的过程.当软件被移植到不同的语言及地区 ...

  7. jQuery之前端国际化jQuery.i18n.properties

    jQuery.i18n.properties是一款轻量级的jQuery国际化插件,能实现Web前端的国际化. 国际化英文单词为:Internationalization,又称i18n,"i& ...

  8. Chrome浏览器扩展开发系列之十八:扩展的软件国际化chrome.i18n API

    i18n是internationalization 的简写,这里将讨论软件国际化的问题.熟悉软件国际化的朋友应该知道,软件国际化要求,页面中所有用户可见的字符串都必须置于资源属性文件中.资源属性文件中 ...

  9. jQuery之前端国际化jQuery.i18n.properties[转]

    http://www.ibm.com/developerworks/cn/web/1305_hezj_jqueryi18n/ jQuery.i18n.properties是一款轻量级的jQuery国际 ...

随机推荐

  1. Echarts数据可视化series-pie饼图,开发全解+完美注释

    全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...

  2. Appium python自动化测试系列之Android知识讲解(三)

    ​3.1 ADB工具讲解 3.1.1 什么是ADB呢? 我们不去解释官方语言的翻译,给大家说一个通熟易懂的说法,ADB我理解为他就是电脑和手机连接的桥梁.此连接不是充电的连接,大家不要混淆,说他是一个 ...

  3. struts2使用模型传值

    用户bean package userBeans; public class User { private String username; public String getUsername() { ...

  4. 脱壳第一讲,手工脱壳ASPack2.12的壳.ESP定律

    脱壳第一讲,手工脱壳ASPack2.12的壳.ESP定律 一丶什么是ESP定律 首先我们要明白什么是壳.壳的作用就是加密PE的. 而ESP定律就是壳在加密之前,肯定会保存所有寄存器环境,而出来的时候, ...

  5. 如何设置App的启动图

    如何设置App的启动图,也就是Launch Image? Step1 1.点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image" 2.如 ...

  6. svn的简介

    Svn(Subversion)是近年来崛起的版本管理工具,在当前的开源项目里(J2EE),几乎95%以上的项目都用到了SVN.Subversion项目的初衷是为了替换当年开源社区最为流行的版本控制软件 ...

  7. LeetCode 229. Majority Element II (众数之二)

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. Ubuntu远程登陆、SSH图形界面、WOL远程唤醒

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 实现目标:通过路由器配置路由路径,将拨号获取的公网IP地址指向局域网Ubuntu服务器.家里有 ...

  9. Supervised Learning and Unsupervised Learning

    Supervised Learning In supervised learning, we are given a data set and already know what our correc ...

  10. 探究Angular依赖注入对象$injector

    $injector其实是一个IOC容器,包含了很多我们通过.module()和$provide创建的模块和服务.$injector服务提供了对依赖注入器对象的访问,当然我们也可以调用angular.i ...