Localize stimulus applications using 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
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>
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" } ]
Now create a new src/controllers/users_controller.js file:
import { Controller } from "stimulus" export default class extends Controller { connect() { this.loadUsers() } }
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) }) }
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 }
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
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 }
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 asadmin
,cart
,profile
wait. A separate translation file should be created for each namespace. -
defaultNS
Setsusers
as the default namespace. -
fallbackNS
Disable namespace fallback. -
debug
允许在浏览器的控制台中显示调试信息。具体来说,它会说明加载哪些翻译文件、选择哪种语言等。您可能希望在将应用程序部署到生产环境之前禁用此设置。 -
backend
为 I18nXHR 插件提供配置并指定从何处加载翻译。请注意,路径应包含区域设置的标题,而文件应以命名空间命名并具有 .json 扩展名 -
function(err, t)
是当 I18next 准备好时(或当出现错误时)运行的回调。
接下来,让我们制作翻译文件。俄语翻译应放入 public/i18n/ru/users.json 文件中:
{ "login": "Логин" }
login
这里是翻译键,而 Логин
是要显示的值。
英文翻译应该转到 public/i18n/en/users.json 文件:
{ "login": "Login" }
为了确保 I18next 正常工作,您可以将以下代码行添加到 i18n/config.js 文件内的回调中:
// config goes here... function(err, t) { if (err) return console.error(err) console.log(i18n.t('login')) }
在这里,我们使用一个名为 t
的方法,意思是“翻译”。该方法接受翻译键并返回相应的值。
但是,我们可能有很多 UI 部分需要翻译,而使用 t
方法来翻译会非常乏味。相反,我建议您使用另一个名为 loc-i18next 的插件,它允许您一次翻译多个元素。
一次性翻译
安装loc-i18next插件:
yarn add loc-i18next
将其导入src/i18n/config.js文件的顶部:
import locI18next from 'loc-i18next'
现在提供插件本身的配置:
// other config const loci18n = locI18next.init(i18n, { selectorAttr: 'data-i18n', optionsAttr: 'data-i18n-options', useOptionsAttr: true }); export { loci18n as loci18n, i18n as i18n }
这里有几点需要注意:
-
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') }) }
编写 localize()
方法本身的代码:
localize() { loci18n('.users') }
如您所见,我们只需要将选择器传递给 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 }
不错!重新加载页面,等待两秒钟,并确保每个用户都显示“登录”字样。
复数和性别
我们对部分界面进行了本地化,这真的很酷。尽管如此,每个用户还有两个字段:上传的照片数量和性别。由于我们无法预测每个用户将拥有多少张照片,因此应根据给定的数量将“照片”一词正确地复数化。为此,我们需要之前配置的 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 }
现在更新英文翻译:
{ "login": "Login", "uploaded": "Has uploaded", "photos": "one photo", "photos_plural": "{{count}} photos" }
这里没什么复杂的。由于对于英语,我们不关心性别信息(即上下文),因此翻译键应该只是 uploaded
。为了提供正确的复数翻译,我们使用 photos
和 photos_plural
键。 {{count}}
部分为插值,将替换为实际数字。
至于俄语,事情就更复杂了:
{ "login": "Логин", "uploaded_male": "Загрузил уже", "uploaded_female": "Загрузила уже", "photos_0": "одну фотографию", "photos_1": "{{count}} фотографии", "photos_2": "{{count}} фотографий" }
首先,请注意,我们有两个可能的上下文的 uploaded_male
和 uploaded_female
键。接下来,俄语中的复数规则也比英语中更复杂,因此我们必须提供不是两个而是三个可能的短语。 I18next 支持多种开箱即用的语言,这个小工具可以帮助您了解应该为给定语言指定哪些复数键。
切换区域设置
我们已经完成了应用程序的翻译,但用户应该能够在区域设置之间切换。因此,向 public/index.html 文件添加一个新的“语言切换器”组件:
<ul data-controller="languages" class="switcher"></ul>
在 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('') } }
这里我们使用 initialize()
回调来显示支持的语言列表。每个 li
都有一个 data-action
属性,该属性指定单击元素时应触发的方法(在本例中为 switchLanguage
)。
现在添加 switchLanguage()
方法:
switchLanguage(e) { this.currentLang = e.target.getAttribute("data-lang") }
它只是获取事件的目标并获取 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() } }
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")) }) }
这里我们迭代区域设置切换器的数组,并将它们的 data-lang
属性的值与当前使用的区域设置的值进行比较。如果值匹配,则为切换器分配 current
CSS 类,否则删除该类。
为了使 this.switcherTargets
构建工作,我们需要按以下方式定义刺激目标:
static targets = [ "switcher" ]
此外,为 li
s 添加值为 switcher
的 data-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('') // ... }
另一个需要考虑的重要事项是翻译文件可能需要一些时间来加载,我们必须等待此操作完成才能允许切换区域设置。因此,让我们利用 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 }) }
最后,不要忘记从 loadUsers()
方法中删除 setTimeout
:
loadUsers() { fetch(this.data.get("url")) .then(response => response.text()) .then(json => { this.renderUsers(json) this.localize() }) }
在 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() } }
检测区域设置
我们今天要实现的最后一件事是能够根据用户的偏好设置区域设置。一个名为 LanguageDetector 的插件可以帮助我们解决这个任务。添加新的 Yarn 包:
yarn add i18next-browser-languagedetector
在 i18n/config.js 文件中导入 LanguageDetector
:
import LngDetector from 'i18next-browser-languagedetector'
现在调整配置:
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) });
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo(); function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

Are you looking for ways to automate your WordPress website and social media accounts? With automation, you will be able to automatically share your WordPress blog posts or updates on Facebook, Twitter, LinkedIn, Instagram and more. In this article, we will show you how to easily automate WordPress and social media using IFTTT, Zapier, and Uncanny Automator. Why Automate WordPress and Social Media? Automate your WordPre
