在URL概念中播放Framework 2语言代码吗? [英] Play Framework 2 Language Code in URL Concept?

查看:87
本文介绍了在URL概念中播放Framework 2语言代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关i18n的手册很简短: https://www.playframework.com/documentation /2.4.x/ScalaI18N 还有什么可以解释处理用户语言选择的概念吗?

The manual about i18n is short: https://www.playframework.com/documentation/2.4.x/ScalaI18N Is there anything more that explains the concept for handling the user's language choice?

我想实现的是许多其他网站的功能:将语言代码放入URL

What I'd like to achieve is what so many other sites do: put the language code into the URL

  • http://namepedia.org/en/home/
  • http://namepedia.org/de/home/
  • ...

然后,当调用(Java)Lang.defaultLang().language()或从Scala模板@lang.language调用时,我想获取该值.当然,按照通常的解决方法,它必须在application.conf play.i18n.langs = [ "en","de" ]

And then, when calling (Java) Lang.defaultLang().language() or from a Scala template @lang.language I'd like to get that value. Of course with the usual resolving, it must be in the application.conf play.i18n.langs = [ "en","de" ]

我真的需要自己从URL中读取它吗?

Do I really need to read it from the URL myself?

此外,在路由文件中,是否还没有用于将其直接映射到语言解析的概念?

Also, in the routes file, isn't there a concept already for mapping it directly to the language resolving?

替代方法是:

  1. 在每种语言的路由文件中复制所有路由,或对语言代码使用正则表达式
  2. 在每个控制器中调用一种用于设置语言的方法

推荐答案

您可以实现自定义请求处理程序并在每个请求上解析语言.就像您在每个控制器中调用一种用于设置语言的方法"一样,但是您只需要在一个地方编写代码-旧版GlobalSettings.onRequest或新版HttpRequestHandler.createAction

You can implement custom request handler and resolve language on every request. This is the same idea like your "in each controller call a method for setting the language" but you need to write the code only in one place - legacy GlobalSettings.onRequest or new HttpRequestHandler.createAction

关于基于播放部分的实现i18n的描述非常好,唯一的一件事-用于2.0.4,所以我想您会使用HttpRequestHandler.createAction,而使用GlobalSettings.onRequest.

There is a very good description about realisation i18n in play based on the url part, the only one thing - it's for 2.0.4, so I suppose you would use HttpRequestHandler.createAction but GlobalSettings.onRequest.

指南: http://www.flowstopper.org/2013 /01/i18n-play-framework-java-app-website.html

迁移指南: https://www.playframework.com/documentation/2.4.x /GlobalSettings

自定义请求处理程序: https://www.playframework.com/documentation/2.4. x/JavaHttpRequestHandlers

Custom Request Handlers: https://www.playframework.com/documentation/2.4.x/JavaHttpRequestHandlers

项目中的实时示例(Play 2.4.3,Java)

Live examples from my project (Play 2.4.3, Java)

application.conf

play.i18n.langs = [ "en", "de", "fr", "ua" ]
play.http.requestHandler = "plugins.RequestHandler"

路线

# Home page
GET     /$lang<[a-z]{2}>/home       controllers.Application.home(lang:String)

plugins/RequestHandler.java

package plugins;

import play.http.DefaultHttpRequestHandler;
import play.libs.F;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.Result;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.lang.reflect.Method;

public class RequestHandler extends DefaultHttpRequestHandler {

    @Override
    public Action createAction(Http.Request request, Method actionMethod) {
        return new Action.Simple() {
            @Override
            public F.Promise<Result> call(Http.Context ctx) throws Throwable {
                Path path = Paths.get(ctx.request().path());
                String lang = path.getName(0).toString();
                // we detect language only by URL path, cookies does not used 
                ctx.setTransientLang(lang);
                return delegate.call(ctx);
            }
        };
    }
}

controllers/Application.java

package controllers;

import play.*;
import play.mvc.*;
import play.i18n.Lang;

import views.html.*;

public class Application extends Controller {

    public Result home(String lang){
       return ok(ctx().lang().code());
    }

}

此应用程序将给出结果

http://localhost:9000/de/home -> "de"

http://localhost:9000/de/home -> "de"

http://localhost:9000/en/home -> "en"

http://localhost:9000/en/home -> "en"

http://localhost:9000/dk/home -> 例外:此应用程序不支持的语言:Lang.availables()中不包含Lang(dk,)"

http://localhost:9000/dk/home -> "exception: Language not supported in this application: Lang(dk,) not in Lang.availables()"

引起注意: Lang.defaultLang().language()将不返回当前的请求语言.您需要调用ctx().lang()来返回当前的请求语言.

Take in to attention: Lang.defaultLang().language() will not return current request language. You need to call ctx().lang() for returning current request language.

这篇关于在URL概念中播放Framework 2语言代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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