请问这样的Nodejs代码如何进行国际化?

请问这样的Nodejs代码如何进行国际化?

https://github.com/cnodejs/nodeclub/blob/master/views/index.html

    当前话题
    <% if (locals.current_user) { %>
      <a href='/topic/create' id='create_topic_btn'>
        <button class='btn btn-success'>发布话题</button>
      </a>
    <% } %>

2 回复

要实现 Node.js 应用的国际化(i18n),可以使用 i18next 这样的库。以下是如何将给定的代码片段进行国际化的步骤:

步骤 1: 安装依赖

首先需要安装 i18nextexpress-i18next 两个包:

npm install i18next i18next-http-middleware --save

步骤 2: 初始化 i18next

创建一个配置文件来初始化 i18next。例如,创建一个名为 i18n.js 的文件:

const i18next = require('i18next');
const HttpMiddleware = require('i18next-http-middleware');

// 设置语言资源
i18next
  .use(HttpMiddleware.LanguageDetector)
  .init({
    fallbackLng: 'en', // 默认语言
    debug: true,
    resources: {
      en: {
        translation: {
          "current_topic": "Current Topic",
          "post_topic": "Post Topic"
        }
      },
      zh: {
        translation: {
          "current_topic": "当前话题",
          "post_topic": "发布话题"
        }
      }
    }
  });

module.exports = i18next;

步骤 3: 集成到 Express 应用中

接下来,将 i18next 中间件集成到你的 Express 应用中:

const express = require('express');
const i18next = require('./i18n');

const app = express();

app.use(i18next.middleware.handle(i18next));

app.get('/', (req, res) => {
  const locals = {
    current_user: true
  };

  res.render('index', locals);
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

步骤 4: 修改模板

最后,修改模板文件以使用翻译键而不是硬编码的文本:

<!-- views/index.html -->
当前话题
<% if (locals.current_user) { %>
  <a href='/topic/create' id='create_topic_btn'>
    <button class='btn btn-success'><%= t('post_topic') %></button>
  </a>
<% } %>

在这个例子中,我们使用了 <%= t('post_topic') %> 来获取翻译后的文本。

总结

通过上述步骤,我们可以将硬编码的文本替换为动态的翻译键,并通过 i18next 动态加载相应的翻译资源。这样,应用就可以根据用户的语言偏好显示不同的文本。


要将如上的Node.js代码进行国际化,你可以使用一些现成的库来帮助实现这一目标。常见的选择包括 i18nextmoment.js(用于日期格式化)等。这里我将展示一个简单的示例,使用 i18next 来实现文本内容的多语言支持。

首先,安装 i18next 和相关中间件:

npm install i18next i18next-http-middleware --save

然后,你可以创建一个简单的配置文件,比如 i18n.js,用于设置不同语言的翻译资源:

const i18next = require('i18next');
const Backend = require('i18next-http-backend');
const Middleware = require('i18next-http-middleware');

i18next
  .use(Backend)
  .use(Middleware.LanguageDetector)
  .init({
    fallbackLng: 'en',
    backend: {
      loadPath: './locales/{{lng}}/translation.json'
    }
  });

module.exports = i18next;

接着,在项目中添加翻译文件,例如在 locales/en/translation.jsonlocales/zh/translation.json 中:

en/translation.json

{
  "release_topic": "Release Topic"
}

zh/translation.json

{
  "release_topic": "发布话题"
}

最后,你需要更新你的视图模板以使用这些翻译资源。假设你正在使用的模板引擎是 EJS:

<% const t = i18next.t.bind(i18next); %>
当前话题
<% if (locals.current_user) { %>
  <a href='/topic/create' id='create_topic_btn'>
    <button class='btn btn-success'>
      <%= t('release_topic') %>
    </button>
  </a>
<% } %>

通过这种方式,你可以在不改变现有逻辑结构的前提下,轻松地为应用添加国际化支持。此外,为了更好地处理语言检测,你可能需要在HTTP请求中间件中设置适当的策略,例如根据用户的浏览器设置自动选择语言。

这样,当用户切换语言时,只需更改他们浏览器的语言设置或在应用程序中手动设置语言,即可看到相应的本地化文本。

回到顶部