i18n动态添加文本的翻译 [英] i18n Translation of Dynamically Added Text

查看:461
本文介绍了i18n动态添加文本的翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i18n刚开始在Node中翻译我的网站.我对如何转换在DOM加载并渲染了jade文件之后生成的文本(如用户单击按钮之后)感到有些茫然.

Just got started with i18n for translating my website in Node. I'm at a bit of a loss of how to translate text that is generated after the DOM has loaded and jade file rendered (like after a user clicks a button).

我知道i18n是服务器端模块,我生成的新文本在客户端.

I understand that i18n is a server side module and my new text generated is on the client side.

如果有帮助,我也正在运行快递.

I'm also running express, if that helps.

我想做的是能够翻译客户端javascript生成的文本.考虑一个任意按钮:

What I want to do is to be able to translate text that has been generated by javascript on the client side. Consider an arbitrary button:

INDEX.JADE
    form.feedback-r
        a#submitclientiddd.button.button-primary #{i18n.__('Reply')}
    // Other Jade Stuff Here
    script(src='javascripts/clientside.js', type='text/javascript')

CLIENT SIDE JS
    $("#posts").on("submit", "form.feedback-r", function(e) {
        actbutton.html("Sending").addClass("feedback").removeClass('send_reply').prop("disabled", true);
    });
... AJAX FUNCTION REQUEST AND ON SUCCESS ...
        actbutton.html("Reply").addClass("success-text").prop("disabled", false);

SERVER SIDE JS
    res.render('index', { title: 'Page Title', i18n: res});

  1. 玉通过i18n通过以下方式呈现的第一个按钮文本 (i18n .__('Reply'))并正确翻译
  2. 用户单击按钮
  3. 在AJAX请求之后,通过jQuery将文本更改为正在发送" 返回回复"
  4. 按钮文本不再动态翻译,因为它是动态的 生成
  1. First button text rendered by jade through i18n with (i18n.__('Reply')) and translated properly
  2. User clicks on button
  3. Text is changed via jQuery to read "Sending", and after an AJAX request back to "Reply"
  4. The button text is no longer translated since it was dynamically generated

是否可以在客户端上使用i18n来解决此问题?在最理想的情况下,我只想在客户端上执行此操作,但它不起作用:

Is there a way to use i18n on the client side to solve this problem? In the most ideal case, I'd like to just do this on the client side, but it's not working:

actbutton.html(i18n.__('Reply'));

推荐答案

我设法通过安装浏览器-i18n 到我的公共javascript文件夹(不通过NPM),然后在客户端使用与i18n在Node上相同的功能.

I managed to solve this by installing browser-i18n to my public javascript folder (not via NPM), and then using the same functions client-side as with i18n on Node.

唯一的问题是browser-i18n看起来不支持范围值,例如:[0]回复| [1,]答复. i18n-for-browser 可能是一种解决方案.

The only problem is that browser-i18n looks like it doesn't support ranged values, like: [0] Reply | [1,] Replies. i18n-for-browser may be a possible solution.

这篇关于i18n动态添加文本的翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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