express3-handlebars和18next-node - 基于页面的国际化? [英] express3-handlebars and 18next-node - internationalisation based on page?

查看:124
本文介绍了express3-handlebars和18next-node - 基于页面的国际化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个问题 - 请放轻松。我正在使用express,express3-handlebars和i18next-node with node.js



这个计划是使用不同的翻译名称空间,具体取决于哪个视图(即哪个句柄文件)目前正在服务。因此,如果我们查看名为ie(.hbs)的页面,则i18next将在名为ie(.json)的相关语言中查找。这使得翻译的组织和协调变得更容易。

这就是我目前的做法:首先,我将当前页面发送到handlebars模板进行渲染(即使这似乎不必要的 - handlebars不会自动暴露它渲染的文件?):
$ b res.render(url_base_path,{layout:(sub) ,title:title,currentpage:url_base_path});

然后我访问变量greeting在命名空间中翻译当前页面如 {{tgreetingpage = currentpage}} - 令人讨厌的是,每个页面上有10个这样的变量。不要重复自己,任何人?
$ b 't'是在express3-handlebars create()函数中定义的,就像这样, helpers:{ t:t}



和translate函数看起来像这样

  var t = function(i18next_key,options){
var page,result;
page = options.hash.page;
result = i18next.t(page +:+ i18next_key);
返回新的hbs.handlebars.SafeString(result);
};

为了充分披露,这是我当前页面的命名空间文件像

  {
greeting:您好,它似乎是使用Internet Explorer,一个过时的Web浏览器。
}

这有效,但似乎应该有一个更简单的解决方案。 / p>

我真正想要的是能够在句柄模板中输入 {{tgreeting}} 达到相同的结果。这是可能的,而不会重写核心的句柄功能?



这里是i18next文档页面
http://i18next.com/pages/doc_features.html

解决方案

我已经回答了我自己的问题 - 它比我预期的更容易。



您可以在我的Handlebars translate helper(duh?)中访问handlebars实例,帮助者: renderer = this (为了清楚起见),那么您可以访问 renderer.currentpage 以获取所需的命名空间。请注意,您仍然必须在渲染函数中发送currentpage(在 res.render())上,但是我对它的那一面还可以。



我已经这样做了(目前它可能非常缓慢,但它的工作原理完全如我所愿):

  //设置翻译名称空间
options.ns = options.ns || bestNamespaceFor(i18n_key);

result = i18n.t(i18n_key,options);
返回新的hbs.handlebars.SafeString(result);

function if(i18n_key,{ns:renderer.currentpage}
if(i18n.exists(i18n_key,{ns:renderer.layout}))return renderer.layout;
if(i18n.exists(i18n_key,{ns:common}))returncommon;

{{tmy translation key}} code>并且无论我在哪里使用它都可以获得正确的命名空间。


my first question here - please go easy. I'm using express, express3-handlebars and i18next-node with node.js

The plan is to work with a different translation namespace depending on which view (i.e. which handlebars file) is currently being served. So if we're looking at the page called ie(.hbs), i18next will look in the namespace called ie(.json) for the relevant language. This makes organisation and coordination of translations easier.

This is how I'm currently doing it: first I send the current page into the handlebars template for rendering (even this seems unnecessary - handlebars doesn't automatically expose which file it's rendering?):

res.render( url_base_path, { layout: ("sub"), title: title, currentpage: url_base_path } );

and then I access the variable "greeting" to be translated in the namespace of the current page like so {{t "greeting" page=currentpage }} - the annoying thing is that there are 10's of these variables on each page. Don't Repeat Yourself, anybody?

't' is defined in the express3-handlebars create() function, like so, helpers: { t: t }

and the translate function looks like this

var t = function (i18next_key, options) {
  var page, result;
  page = options.hash.page;
  result = i18next.t(page + ":" + i18next_key);
  return new hbs.handlebars.SafeString(result);
};

for the sake of full disclosure, this is what my (english) namespace file for the current page looks like

{
    "greeting": "Hello, it appears you're using Internet Explorer, an outdated web browser."
}

this works, but it seems like there should be a much simpler solution.

what i really want is to be able to just type {{t "greeting"}} into the handlebars template to achieve the same result. is this possible without overriding core handlebars functionality?

here is the i18next docs page http://i18next.com/pages/doc_features.html

解决方案

I've answered my own question - it turned out easier than I expected.

You can access the handlebars instance in my Handlebars translate helper (duh?), so in the helper: renderer = this (for clarity), then you can just access renderer.currentpage to get the name of the required namespace. Note, you still have to send currentpage in the render function (on res.render()), but I'm ok with that side of it.

I've done it like this (it's probably pretty slow like this at the moment, but it works exactly how I want it to):

  // Set namespace for translation
  options.ns = options.ns || bestNamespaceFor( i18n_key );

  result = i18n.t(i18n_key, options);
  return new hbs.handlebars.SafeString(result);

  function bestNamespaceFor( i18n_key ){
    if ( i18n.exists(i18n_key, { ns: renderer.currentpage }) ) return renderer.currentpage;
    if ( i18n.exists(i18n_key, { ns: renderer.layout }) ) return renderer.layout;
    if ( i18n.exists(i18n_key, { ns: "common" }) ) return "common";

Works perfectly with {{t "my translation key" }} and gets the right namespace no matter where I use it.

这篇关于express3-handlebars和18next-node - 基于页面的国际化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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