Home > CMS Tutorial > WordPress > body text

Localize stimulus applications using I18Next

WBOY
Release: 2023-08-28 23:53:07
Original
1446 people have browsed it

使用 I18Next 本地化刺激应用程序

In my previous article, I introduced Stimulus - a simple JavaScript framework created by Basecamp. Today I will discuss internationalization of Stimulus applications, since the framework does not provide any internationalization tools out of the box. Internationalization is an important step, especially when your application is used by people around the world, so a basic understanding of how to do internationalization can really come in handy.

Of course, it's up to you to decide which internationalization solution to implement, whether it's jQuery.I18n, Polyglot, or something else. In this tutorial, I want to show you a popular I18n framework called I18next, which has many cool features and provides many additional third-party plugins to further simplify the development process. Even with all these features, I18next is not a complex tool and you don’t need to study a ton of documentation to get started.

In this article, you will learn how to enable I18n support in your Stimulus application with the help of the I18next library. Specifically, we will discuss:

  • I18Next configuration
  • Translate files and load asynchronously
  • Perform translation and translate the entire page at once
  • Handling plural and gender information
  • Switch between locales and keep the selected locale in the GET parameters
  • Set locale based on user preferences

Source code can be found in the tutorial GitHub repository.

GUIDE STIMULUS APP

First, let’s clone the Stimulus Starter project and install all dependencies using the Yarn package manager:

git clone https://github.com/stimulusjs/stimulus-starter.git
cd stimulus-starter
yarn install
Copy after login

We will build a simple web application to load information about registered users. For each user we will display his/her login name and the number of photos he/she has uploaded so far (it doesn't matter what those photos are).

Additionally, we will provide a language switcher at the top of the page. Once you select a language, the interface should be translated immediately without the need to reload the page. Additionally, the URL should be appended with the ?locale GET parameter specifying the current locale in use. Of course, if this parameter is provided when the page loads, the correct language should be set automatically.

Okay, let's go ahead and render our user. Add the following lines of code to the public/index.html file:

<div data-controller="users" data-users-url="/api/users/index.json"></div>
Copy after login

Here we use the users controller and provide a URL for loading users. In a real-world application, we might have a server-side script that fetches the user from the database and responds with JSON. However, for this tutorial we will simply put all the necessary data into the public/api/users/index.json file:

[
  {
    "login": "johndoe",
    "photos_count": "15",
    "gender": "male"
  },
  {
    "login": "annsmith",
    "photos_count": "20",
    "gender": "female"
  }
]
Copy after login

Now create a new src/controllers/users_controller.js file:

import { Controller } from "stimulus"

export default class extends Controller {
  connect() {
    this.loadUsers()
  }
}
Copy after login

Once the controller is connected to the DOM, we load users asynchronously with the help of the loadUsers() method:

  loadUsers() {
    fetch(this.data.get("url"))
    .then(response => response.text())
    .then(json => {
      this.renderUsers(json)
    })
  }
Copy after login

This method sends a get request to the given URL, gets the response, and finally presents the user:

  renderUsers(users) {
    let content = ''
    JSON.parse(users).forEach((user) => {
      content += `<div>Login: ${user.login}<br>Has uploaded ${user.photos_count} photo(s)</div><hr>`
    })
    this.element.innerHTML = content
  }
Copy after login

renderUsers() Parse the JSON in sequence, construct a new string containing all the content, and finally display this content on the page (this.element is the controller connection that will be returned The actual DOM node reached, in our case div).

I18Next

Now we will continue integrating I18next into our application. Add two libraries to our project: I18next itself and a plugin to enable asynchronous loading of translation files from the backend:

yarn add i18next i18next-xhr-backend
Copy after login

We will store all I18next related stuff in a separate src/i18n/config.js file, so create it now:

import i18next from 'i18next'
import I18nXHR from 'i18next-xhr-backend'

const i18n = i18next.use(I18nXHR).init({
  fallbackLng: 'en',
  whitelist: ['en', 'ru'],
  preload: ['en', 'ru'],
  ns: 'users',
  defaultNS: 'users',
  fallbackNS: false,
  debug: true,
  backend: {
    loadPath: '/i18n/{{lng}}/{{ns}}.json',
  }
}, function(err, t) {
  if (err) return console.error(err)
});
 
export { i18n as i18n }
Copy after login

Let’s understand what’s going on here from top to bottom:

  • use(I18nXHR) Enable i18next-xhr-backend plugin.

  • fallbackLng Tell it to use English as the fallback language.

  • whitelist Only English and Russian are allowed to be set. Of course, you can choose any other language.

  • preload Indicates to preload translation files from the server instead of loading them when the corresponding language is selected.

  • ns Represents "namespace" and accepts a string or array. In this example we have only one namespace, but for larger applications you can introduce other namespaces such as admin, cart, profile wait. A separate translation file should be created for each namespace.

  • defaultNS Sets users as the default namespace.

  • fallbackNS Disable namespace fallback.

  • debug 允许在浏览器的控制台中显示调试信息。具体来说,它会说明加载哪些翻译文件、选择哪种语言等。您可能希望在将应用程序部署到生产环境之前禁用此设置。

  • backend 为 I18nXHR 插件提供配置并指定从何处加载翻译。请注意,路径应包含区域设置的标题,而文件应以命名空间命名并具有 .json 扩展名

  • function(err, t) 是当 I18next 准备好时(或当出现错误时)运行的回调。

接下来,让我们制作翻译文件。俄语翻译应放入 public/i18n/ru/users.json 文件中:

{
  "login": "Логин"
}
Copy after login

login 这里是翻译键,而 Логин 是要显示的值。

英文翻译应该转到 public/i18n/en/users.json 文件:

{
  "login": "Login"
}
Copy after login

为了确保 I18next 正常工作,您可以将以下代码行添加到 i18n/config.js 文件内的回调中:

// config goes here...
function(err, t) {
  if (err) return console.error(err)
  console.log(i18n.t('login'))
}
Copy after login

在这里,我们使用一个名为 t 的方法,意思是“翻译”。该方法接受翻译键并返回相应的值。

但是,我们可能有很多 UI 部分需要翻译,而使用 t 方法来翻译会非常乏味。相反,我建议您使用另一个名为 loc-i18next 的插件,它允许您一次翻译多个元素。

一次性翻译

安装loc-i18next插件:

yarn add loc-i18next
Copy after login

将其导入src/i18n/config.js文件的顶部:

import locI18next from 'loc-i18next'
Copy after login

现在提供插件本身的配置:

// other config

const loci18n = locI18next.init(i18n, {
  selectorAttr: 'data-i18n',
  optionsAttr: 'data-i18n-options',
  useOptionsAttr: true
});

export { loci18n as loci18n, i18n as i18n }
Copy after login

这里有几点需要注意:

  • locI18next.init(i18n) 基于之前定义的 I18next 实例创建一个新的插件实例。
  • selectorAttr 指定使用哪个属性来检测需要本地化的元素。基本上,loc-i18next 将搜索此类元素并使用 data-i18n 属性的值作为翻译键。

  • optionsAttr 指定哪个属性包含附加翻译选项。

  • useOptionsAttr 指示插件使用其他选项。

我们的用户正在异步加载,因此我们必须等到此操作完成,然后才执行本地化。现在,我们简单地设置一个计时器,在调用 localize() 方法之前等待两秒——当然,这是一个临时的 hack。

  import { loci18n } from '../i18n/config'
  
  // other code...
  
  loadUsers() {
    fetch(this.data.get("url"))
    .then(response => response.text())
    .then(json => {
      this.renderUsers(json)
      setTimeout(() => { // <---
        this.localize()
      }, '2000')
    })
  }
Copy after login

编写 localize() 方法本身的代码:

  localize() {
    loci18n('.users')
  }
Copy after login

如您所见,我们只需要将选择器传递给 loc-i18next 插件即可。内部的所有元素(设置了 data-i18n 属性)都将自动本地化。

现在调整 renderUsers 方法。现在,我们只翻译“Login”一词:

  renderUsers(users) {
    let content = ''
    JSON.parse(users).forEach((user) => {
      content += `<div class="users">ID: ${user.id}<br><span data-i18n="login"></span>: ${user.login}<br>Has uploaded ${user.photos_count} photo(s)</div><hr>`
    })
    this.element.innerHTML = content
  }
Copy after login

不错!重新加载页面,等待两秒钟,并确保每个用户都显示“登录”字样。

复数和性别

我们对部分界面进行了本地化,这真的很酷。尽管如此,每个用户还有两个字段:上传的照片数量和性别。由于我们无法预测每个用户将拥有多少张照片,因此应根据给定的数量将“照片”一词正确地复数化。为此,我们需要之前配置的 data-i18n-options 属性。要提供计数,应为 data-i18n-options 分配以下对象:{ "count": YOUR_COUNT }

性别信息也应考虑在内。英语中的“uploaded”一词可以适用于男性和女性,但在俄语中它要么变成“загрузил”或“загрузила”,所以我们再次需要 data-i18n-options,其中有 { "context": "GENDER" } 作为值。顺便请注意,您可以利用此上下文来完成其他任务,而不仅仅是提供性别信息。

  renderUsers(users) {
    let content = ''
    JSON.parse(users).forEach((user) => {
      content += `<div class="users"><span data-i18n="login"></span>: ${user.login}<br><span data-i18n="uploaded" data-i18n-options="{ 'context': '${user.gender}' }"></span> <span data-i18n="photos" data-i18n-options="{ 'count': ${user.photos_count} }"></span></div><hr>`
    })
    this.element.innerHTML = content
  }
Copy after login

现在更新英文翻译:

{
  "login": "Login",
  "uploaded": "Has uploaded",
  "photos": "one photo",
  "photos_plural": "{{count}} photos"
}
Copy after login

这里没什么复杂的。由于对于英语,我们不关心性别信息(即上下文),因此翻译键应该只是 uploaded。为了提供正确的复数翻译,我们使用 photosphotos_plural 键。 {{count}} 部分为插值,将替换为实际数字。

至于俄语,事情就更复杂了:

{
  "login": "Логин",
  "uploaded_male": "Загрузил уже",
  "uploaded_female": "Загрузила уже",
  "photos_0": "одну фотографию",
  "photos_1": "{{count}} фотографии",
  "photos_2": "{{count}} фотографий"
}
Copy after login

首先,请注意,我们有两个可能的上下文的 uploaded_maleuploaded_female 键。接下来,俄语中的复数规则也比英语中更复杂,因此我们必须提供不是两个而是三个可能的短语。 I18next 支持多种开箱即用的语言,这个小工具可以帮助您了解应该为给定语言指定哪些复数键。

切换区域设置

我们已经完成了应用程序的翻译,但用户应该能够在区域设置之间切换。因此,向 public/index.html 文件添加一个新的“语言切换器”组件:

<ul data-controller="languages" class="language-switcher"></ul>
Copy after login

src/controllers/languages_controller.js 文件中制作相应的控制器:

import { Controller } from "stimulus"
import { i18n, loci18n } from '../i18n/config'

export default class extends Controller {
    initialize() {
      let languages = [
        {title: 'English', code: 'en'},
        {title: 'Русский', code: 'ru'}
      ]

      this.element.innerHTML = languages.map((lang) => {
        return `<li data-action="click->languages#switchLanguage"
        data-lang="${lang.code}">${lang.title}</li>`
      }).join('')
    }
}
Copy after login

这里我们使用 initialize() 回调来显示支持的语言列表。每个 li 都有一个 data-action 属性,该属性指定单击元素时应触发的方法(在本例中为 switchLanguage)。

现在添加 switchLanguage() 方法:

  switchLanguage(e) {
    this.currentLang = e.target.getAttribute("data-lang")
  }
Copy after login

它只是获取事件的目标并获取 data-lang 属性的值。

我还想为 currentLang 属性添加 getter 和 setter:

  get currentLang() {
    return this.data.get("currentLang")
  }

  set currentLang(lang) {
    if(i18n.language !== lang) {
      i18n.changeLanguage(lang)
    }

    if(this.currentLang !== lang) {
      this.data.set("currentLang", lang)
      loci18n('body')
      this.highlightCurrentLang()
    }
  }
Copy after login

getter 非常简单——我们获取当前使用的语言的值并返回它。

setter 更复杂。首先,如果当前设置的语言与所选语言不相等,我们使用 changeLanguage 方法。此外,我们将新选择的语言环境存储在 data-current-lang 属性(在 getter 中访问)下,使用 loc-i18next 插件本地化 HTML 页面的主体,最后突出显示当前使用的区域设置。

让我们编写 highlightCurrentLang() 的代码:

  highlightCurrentLang() {
    this.switcherTargets.forEach((el, i) => {
      el.classList.toggle("current", this.currentLang === el.getAttribute("data-lang"))
    })
  }
Copy after login

这里我们迭代区域设置切换器的数组,并将它们的 data-lang 属性的值与当前使用的区域设置的值进行比较。如果值匹配,则为切换器分配 current CSS 类,否则删除该类。

为了使 this.switcherTargets 构建工作,我们需要按以下方式定义刺激目标:

static targets = [ "switcher" ]
Copy after login

此外,为 lis 添加值为 switcherdata-target 属性:

  initialize() {
      // ...
      this.element.innerHTML = languages.map((lang) => {
        return `<li data-action="click->languages#switchLanguage"
        data-target="languages.switcher" data-lang="${lang.code}">${lang.title}</li>`
      }).join('')
      // ...
  }
Copy after login

另一个需要考虑的重要事项是翻译文件可能需要一些时间来加载,我们必须等待此操作完成才能允许切换区域设置。因此,让我们利用 loaded 回调:

  initialize() {
    i18n.on('loaded', (loaded) => { // <---
      let languages = [
        {title: 'English', code: 'en'},
        {title: 'Русский', code: 'ru'}
      ]

      this.element.innerHTML = languages.map((lang) => {
        return `<li data-action="click->languages#switchLanguage"
        data-target="languages.switcher" data-lang="${lang.code}">${lang.title}</li>`
      }).join('')

      this.currentLang = i18n.language
    })
  }
Copy after login

最后,不要忘记从 loadUsers() 方法中删除 setTimeout

  loadUsers() {
    fetch(this.data.get("url"))
    .then(response => response.text())
    .then(json => {
      this.renderUsers(json)
      this.localize()
    })
  }
Copy after login

在 URL 中保留区域设置

切换语言环境后,我想在包含所选语言代码的 URL 中添加 ?lang GET 参数。在 History API 的帮助下,可以轻松地添加 GET 参数而不重新加载页面:

  set currentLang(lang) {
    if(i18n.language !== lang) {
      i18n.changeLanguage(lang)
      window.history.pushState(null, null, `?lang=${lang}`) // <---
    }

    if(this.currentLang !== lang) {
      this.data.set("currentLang", lang)
      loci18n('body')
      this.highlightCurrentLang()
    }
  }
Copy after login

检测区域设置

我们今天要实现的最后一件事是能够根据用户的偏好设置区域设置。一个名为 LanguageDetector 的插件可以帮助我们解决这个任务。添加新的 Yarn 包:

yarn add i18next-browser-languagedetector
Copy after login

i18n/config.js 文件中导入 LanguageDetector

import LngDetector from 'i18next-browser-languagedetector'
Copy after login

现在调整配置:

const i18n = i18next.use(I18nXHR).use(LngDetector).init({ // <---
  // other options go here...
  detection: {
    order: ['querystring', 'navigator', 'htmlTag'],
    lookupQuerystring: 'lang',
  }
}, function(err, t) {
  if (err) return console.error(err)
});
Copy after login

order 选项列出了插件应尝试的所有技术(按重要性排序),以便“猜测”首选区域设置:

  • querystring 表示检查包含区域设置代码的 GET 参数。
  • lookupQuerystring 设置要使用的 GET 参数的名称,在我们的例子中是 lang
  • navigator 表示从用户的请求中获取语言环境数据。
  • htmlTag 涉及从 html 标记的 lang 属性获取首选区域设置。

结论

在本文中,我们介绍了 I18next——一种轻松翻译 JavaScript 应用程序的流行解决方案。您已经学习了如何将 I18next 与 Stimulus 框架集成、配置它以及以异步方式加载翻译文件。此外,您还了解了如何在区域设置之间切换以及如何根据用户的偏好设置默认语言。

I18next 有一些额外的配置选项和许多插件,因此请务必浏览其官方文档以了解更多信息。另请注意,Stimulus 不会强制您使用特定的本地化解决方案,因此您也可以尝试使用 jQuery.I18n 或 Polyglot 等解决方案。

That’s all for today! Thanks for reading, and until next time.

The above is the detailed content of Localize stimulus applications using I18Next. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!