gpt4 book ai didi

java - 仅支持有限数量的语言

转载 作者:行者123 更新时间:2023-11-30 08:02:34 25 4
gpt4 key购买 nike

我的 API 使用 Jersey 2,现在我想支持国际化。我知道我的客户应该指定 Accept-Language 参数,但我想了解如何正确处理它。

假设我的 API 应该只处理 FRENCHENGLISH 语言。我知道我可以使用以下代码检索首选 语言环境:

@GET
@Path("a-path")
public Response doSomething(@Context HttpServletRequest request) {
Locale locale = request.getLocale();
// ...
}

问题是当我的 API 不支持首选 语言环境时。假设我的客户根据 w3c 向我发送 Accept-Language: da, en-gb;q=0.8, en;q=0.7 ,它的基本意思是:“我更喜欢丹麦语,但会接受英式英语和其他类型的英语。”。由于首选 区域设置只返回最期望的区域设置,有没有办法让我的 API 选择第一个支持的语言?我想在一个地方处理它(即在 Filters 中)而不是在每个资源中。

最佳答案

获取语言环境的一种方法是使用 HttpHeaders#getAcceptableLanguages() .

Get a list of languages that are acceptable for the response.

If no acceptable languages are specified, a read-only list containing a single wildcard Locale instance (with language field set to "*") is returned.

Returns:a read-only list of acceptable languages sorted according to their q-value, with highest preference first.

您几乎可以在任何地方注入(inject) HttpHeaders,使用 @Context

public Response doSomething(@Context HttpHeaders headers) {
List<Locale> langs = headers.getAcceptableLanguages();

如果你想在 filter 中获取列表,您还可以从 ContainerRequestContext 中获取语言环境列表

@Override
public void filter(ContainerRequestContext requestContext) throw .. {
List<Locales> langs = requestContext.getAcceptableLanguages();
}

如果你想在资源方法中使用Locale,但不想在方法中做所有的区域设置“解析”,你可以使用一些依赖注入(inject),并创建一个Factory,您可以在其中注入(inject) HttpHeaders 并在那里解析语言环境

另请参阅: Dependency injection with Jersey 2.0

下面是一个完整的测试用例示例,它结合了我提到的关于在 Factory 中使用过滤器和依赖项注入(inject)的最后两点,这样您就可以注入(inject)已解析的 Locale 进入资源方法。该示例使用仅允许英语的虚拟语言环境解析器。解析语言环境后,我们将其设置到请求上下文属性中,并从 Factory 内部检索,以便我们可以将其注入(inject)资源方法

@GET
public String get(@Context Locale locale) {
return locale.toString();
}

另请参阅: How to inject an object into jersey request context?

如果您还想让我解释一下这个例子,请告诉我

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Stack Overflow question https://stackoverflow.com/q/36871274/2587435
*
* Run this like any other JUnit test. Only one required test dependency:
*
* <dependency>
* <groupId>org.glassfish.jersey.test-framework.providers</groupId>
* <artifactId>jersey-test-framework-provider-inmemory</artifactId>
* <version>${jersey2.version}</version>
* </dependency>
*
* @author Paul Samsotha
*/
public class AcceptLanguageTest extends JerseyTest {

@Path("language")
public static class TestResource {

@GET
public String get(@Context Locale locale) {
return locale.toString();
}
}

public static interface LocaleResolver {
Locale resolveLocale(List<Locale> locales);
}

// Note: if you look in the javadoc for getAcceptableLanguages()
// you will notice that it says if there is not acceptable language
// specified, that there is a default single wildcard (*) locale.
// So this implementation sucks, as it doesn't check for that.
// You will want to make sure to do so!
public static class DefaultLocaleResolver implements LocaleResolver {

@Override
public Locale resolveLocale(List<Locale> locales) {
if (locales.contains(Locale.ENGLISH)) {
return Locale.ENGLISH;
}
return null;
}
}

@Provider
@PreMatching
public static class LocaleResolverFilter implements ContainerRequestFilter {

static final String LOCALE_PROPERTY = "LocaleResolverFilter.localProperty";

@Inject
private LocaleResolver localeResolver;

@Override
public void filter(ContainerRequestContext context) throws IOException {
List<Locale> locales = context.getAcceptableLanguages();
Locale locale = localeResolver.resolveLocale(locales);
if (locale == null) {
context.abortWith(Response.status(Response.Status.NOT_ACCEPTABLE).build());
return;
}
context.setProperty(LOCALE_PROPERTY, locale);
}
}

public static class LocaleFactory implements Factory<Locale> {

@Context
private ContainerRequestContext context;

@Override
public Locale provide() {
return (Locale) context.getProperty(LocaleResolverFilter.LOCALE_PROPERTY);
}

@Override
public void dispose(Locale l) {}
}

@Override
public ResourceConfig configure() {
return new ResourceConfig(TestResource.class)
.register(LocaleResolverFilter.class)
.register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(LocaleFactory.class)
.to(Locale.class).in(RequestScoped.class);
bind(DefaultLocaleResolver.class)
.to(LocaleResolver.class).in(Singleton.class);
}
})
.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}

@Test
public void shouldReturnEnglish() {
final String accept = "da, en-gb;q=0.8, en;q=0.7";
final Response response = target("language").request()
.acceptLanguage(accept)
.get();
assertThat(response.readEntity(String.class), is("en"));
}

@Test
public void shouldReturnNotAcceptable() {
final String accept = "da";
final Response response = target("language").request()
.acceptLanguage(accept)
.get();
assertThat(response.getStatus(), is(Response.Status.NOT_ACCEPTABLE.getStatusCode()));
}
}

关于java - 仅支持有限数量的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com