Finology 大数据金融

通过大数据以量化金融

只要客户里面有老外,那都会涉及到多语言国际化的问题。我们看看在 Java Spring Boot 项目中如何设置多语言。

yaml配置

通过查看 org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,我们可以知道需要配置如下属性:spring.messages.basename

1
2
3
spring:
messages:
basename: i18n/message

需要在src\main\resources\i18n目录下添加如下文件:

默认配置文件:message.properties
英文:message_en_US.properties
中文:message_zh_CN.properties

内容都是 key=value 的格式,一行一条。

语言处理类

通过此设置,当请求头里面的Accept-Lanuage没有值时,以中文显示,否则按设置的值来处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cn.com.wind.Wind.RTC.WebService.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;


@Configuration
public class CustomLocaleResolver extends AcceptHeaderLocaleResolver implements WebMvcConfigurer {

@Override
public Locale resolveLocale(HttpServletRequest request) {

if (!StringUtils.hasText(request.getHeader("Accept-Language"))) {
return Locale.SIMPLIFIED_CHINESE;
}

return super.resolveLocale(request);

}

@Bean
public LocaleResolver localeResolver() {
return this;
}
}

语言工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.com.wind.convertserver.utils;

import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;


@Component
public class I18nMessageUtils {

private static MessageSource messageSource;

public MessageUtils(MessageSource messageSource) {
MessageUtils.messageSource = messageSource;
}

public static String getMessage(String messageKey, Locale locale) throws NoSuchMessageException {
if (locale == null) {
locale = LocaleContextHolder.getLocale();
}
return messageSource.getMessage(messageKey, null, locale);
}
}

这样就可以通过这个工具类,做国际化的语言转换了。

我们还发现一个问题,通过tomcat容器去运行的应用程序,在request请求里面未能获取到Locale的信息,但把tomcat改为jetty后,是可以成功获取到的。

pom.xml设置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

最近在 Linux OpenSUSE 上运行 pdfbox 工具,将 Pdf 转成图片,会遇到找不到字体的警告提示。

1
[15:02:34.732] WARN  org.apache.pdfbox.pdmodel.font.PDTrueTypeFont 220 <init> - Using fallback font 'LiberationSans' for 'Arial,Bold'

所以这篇文章讲述一下如何在 OpenSUSE 系统上安装字体字库。

查看 OpenSUSE 版本号

1
2
3
4
5
6
7
8
9
10
11
# cat /etc/os-release 
NAME="openSUSE Leap"
VERSION="42.3"
ID=opensuse
ID_LIKE="suse"
VERSION_ID="42.3"
PRETTY_NAME="openSUSE Leap 42.3"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:42.3"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"

查看当前系统已经安装的字体

1
2
3
4
5
# fc-list | less

/usr/share/fonts/truetype/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/truetype/OpenSans-CondLightItalic.ttf: Open Sans,Open Sans Condensed Light:style=Condensed Light Italic,Italic
...

获取字体

大多数的字体,在 Windows 系统上都可以找到。

字体目录所在位置:C:\Windows\Fonts

安装字体

OpenSUSE 的字库位置在 /usr/share/fonts/truetype,通过刚才命令也可以看出来。

然后再把 Windows 下面的字体拷贝到上述目录中。

重新加载新添加的字体:

1
fc-cache -fv

最后,要让java应用程序生效,还得重新启动一个应用。

[ 值 for 元素 in 可迭代对象 if 条件 ] # 值可以是函数表达式

[ 值1 if 条件1 else 值2 for 元素 in 可迭代对象] # 将 if-else 语句写入列表解析式

1
2
3
4
5
6
7
8
9
10
11
ret = [0.37, -1.9, 0.6, 2.54, 0.51, 1.09, 1.79, -1.2, -0.14, -0.5, -2.37, 0.0, -0.22, -0.15, 4.28, -0.85, -1.79, -0.44, -0.15, 2.34]

# 例:从收益率序列中提取出负的收益率
[i for i in ret if i < 0]

[-1.9, -1.2, -0.14, -0.5, -2.37, -0.22, -0.15, -0.85, -1.79, -0.44, -0.15]

# 正收益率赋值为1,负收益率赋值为0
[1 if i > 0 else 0 for i in ret]

[1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
1
2
3
4
ret_set = {(0.37, -1.9, 0.6, 2.54, 0.51), (1.09, 1.79, -1.2, -0.14, -0.5), (-2.37, 0.0, -0.22, -0.15, 4.28), (-0.85, -1.79, -0.44, -0.15, 2.34)}

# 如何将元素打平
[i for t in ret_set for i in t]

内循环在外循环后面

0%