Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Play 2.0中的本地化URL?_Url_Seo_Playframework 2.0 - Fatal编程技术网

Play 2.0中的本地化URL?

Play 2.0中的本地化URL?,url,seo,playframework-2.0,Url,Seo,Playframework 2.0,昨天,我们在本地进行了Play 2.0演示,但我们无法确定是否可以使用本地化URL(用于SEO目的) 例如/help、/hilfe等应指向同一控制器,但模板应使用不同的语言内容呈现 在Play 2.0中有什么方法可以做到这一点吗?我喜欢你的问题,因为它至少对我来说是有创意的:)检查这种方法它对我有效: conf/routes: GET /help controllers.Application.helpIndex(lang = "en") GET /hilfe co

昨天,我们在本地进行了Play 2.0演示,但我们无法确定是否可以使用本地化URL(用于SEO目的)

例如/help、/hilfe等应指向同一控制器,但模板应使用不同的语言内容呈现


在Play 2.0中有什么方法可以做到这一点吗?

我喜欢你的问题,因为它至少对我来说是有创意的:)检查这种方法它对我有效:

conf/routes

GET     /help     controllers.Application.helpIndex(lang = "en")
GET     /hilfe    controllers.Application.helpIndex(lang = "de")

GET     /help/:id     controllers.Application.helpTopic(lang = "en", id: Long)
GET     /hilfe/:id    controllers.Application.helpTopic(lang = "de", id: Long)
public static Result helpIndex(String lang) {
    return ok("Display help's index in " + lang.toUpperCase());
}

public static Result helpTopic(String lang, Long id) {
    return ok("Display details of help topic no " + id + " in " + lang.toUpperCase());
}
<a href="@routes.Application.helpIndex("en")">Help index</a><br/>
<a href="@routes.Application.helpIndex("de")">Hilfe index</a><br/>

<a href="@routes.Application.helpTopic("en", 12)">Help topic no 12</a><br/>
<a href="@routes.Application.helpTopic("de", 12)">Hilfe topic no 12</a>
控制器/应用程序.java

GET     /help     controllers.Application.helpIndex(lang = "en")
GET     /hilfe    controllers.Application.helpIndex(lang = "de")

GET     /help/:id     controllers.Application.helpTopic(lang = "en", id: Long)
GET     /hilfe/:id    controllers.Application.helpTopic(lang = "de", id: Long)
public static Result helpIndex(String lang) {
    return ok("Display help's index in " + lang.toUpperCase());
}

public static Result helpTopic(String lang, Long id) {
    return ok("Display details of help topic no " + id + " in " + lang.toUpperCase());
}
<a href="@routes.Application.helpIndex("en")">Help index</a><br/>
<a href="@routes.Application.helpIndex("de")">Hilfe index</a><br/>

<a href="@routes.Application.helpTopic("en", 12)">Help topic no 12</a><br/>
<a href="@routes.Application.helpTopic("de", 12)">Hilfe topic no 12</a>
视图/someView.scala.html

GET     /help     controllers.Application.helpIndex(lang = "en")
GET     /hilfe    controllers.Application.helpIndex(lang = "de")

GET     /help/:id     controllers.Application.helpTopic(lang = "en", id: Long)
GET     /hilfe/:id    controllers.Application.helpTopic(lang = "de", id: Long)
public static Result helpIndex(String lang) {
    return ok("Display help's index in " + lang.toUpperCase());
}

public static Result helpTopic(String lang, Long id) {
    return ok("Display details of help topic no " + id + " in " + lang.toUpperCase());
}
<a href="@routes.Application.helpIndex("en")">Help index</a><br/>
<a href="@routes.Application.helpIndex("de")">Hilfe index</a><br/>

<a href="@routes.Application.helpTopic("en", 12)">Help topic no 12</a><br/>
<a href="@routes.Application.helpTopic("de", 12)">Hilfe topic no 12</a>




这在游戏1.2.x中是可能的,而据我所知,不是在游戏2.x中。我的意思是,如果不复制文件中的映射,为EN添加一个映射,为DE添加一个映射,等等,这是不可能的

SEO的一个更简单的选择可能是“伪造”站点地图文件中的URL

所以你的路由文件

GET  /action/:param/:seo-string   Controller.methodAction(param)
因此,
seo字符串
将在处理过程中被忽略,您将在站点地图文件上生成多个链接:

/action/1/english-text
/action/1/german-text
这将设置搜索引擎。对于用户,为了让他们看到正确语言的url,您可以使用HTML5历史记录更改url

这是额外的工作,但如果您真的想要它…

(这与中的方法不同,因此作为单独的方法添加)

您还可以在DB中创建某种类型的
映射表
,在这里您可以存储到具有不同参数的记录的完整路径:

urlpath              record_id    lang
/help/some-topic     12           en
/hilfe/ein-topic     12           de
conf/routes
文件中,您需要使用允许您使用跨越多个/的
动态部分的规则(请参阅),即:

您还可以将标准路由机制和自定义路由机制混合使用,方法是将上述规则放在
conf/routes
文件的末尾。如果没有可用的“静态”规则,则它将尝试在映射表中查找该规则,或返回
notFound()
结果。

您使用from并检查该规则是否为url的翻译版本。然后你可以重定向。但是,这以默认语言的URL结束

更干净的方法是使用,您可以在其中实现自己的逻辑来获取处理程序

此外,您还可以创建自己的路由器。在一次会议上就此事进行了讨论