Switching between "single" and "multiple" messages with i18next

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About this article
This article was created using an automated generation workflow leveraging generative AI. We verified official documentation and fixed educational materials to outline operational and technical constraints.

Verification Status: 📘 Official information and implementation cross-checked. Physical browser testing not performed.

The terms used for counts vary depending on the language. Here, we display a count of 1 and 2 in English and Japanese.

Open live demo

Terminology to know first

  • Localeis the display language and regional setting.

  • Pluralizationrefers to the word forms adapted to the count.

Thinking through the process flowchart

Read from left to right. Anticipate how changes to "language-specific rules" affect the "display text".

flowchart LR
    A["件数"] --> B["言語ごとの規則"] --> C["表示文"]

Operating the live demo

Run with 1 and 2. Observe the English and Japanese notations.

How to read the results

English switches between item and items. Japanese changes the count display.

This example compares English and Japanese. When adding languages, test 0, 1, and 2 directly to verify the display text.

Key code points

Pass count to the translation key to separate English singular and plural resources.

The implementation can be checked in the Implementation Code of the demo screen.

When using it in production

Create translation keys by sentence. Concatenating words makes it difficult to support languages with different word orders.

The word form changes even for the same count.

First, enter 1 and execute. Expect 1 item in English and 1件 in Japanese to be displayed. Next, change to 2 and perform the same operation. In English, it becomes the plural items, and in Japanese, only the numerical value changes. Try 0 as well to check how it is displayed when there are no items.

CountExpected English DisplayExpected Japanese Display
00 items0 items
11 item1 item
22 items2 items

What is being compared here is the word form, and there is no actual aggregated data itself. Instead of manually writing the display code for 1 item and 2 items separately, the display is determined by combining the translation resource and the count. Observing that 0 becomes other in English also shows that plurals should not simply be treated with a condition of 2 or more.

Separate translation resources and counts

The learning material creates an i18next instance and prepares item_one and item_other for English, and item_other for Japanese. When passing the base key item and the count value to the translation function t, the appropriate key is selected based on the language, and the count value is embedded into the sentence. Since lng is specified to retrieve English and Japanese results simultaneously, users do not need to switch their language settings.

Official documentation explains count as an argument for determining plural forms. Depending on the language, one and other may not be sufficient. Therefore, hardcoding the condition on the UI side to treat a count of 1 as singular and anything else as plural may cause issues when adding other languages. Since the word order of translated sentences also differs by language, managing resources as complete sentences is more suitable than concatenating numbers and nouns separately.

Testing 0, 1, and 2 with Node.js Intl.PluralRules this time showed that only 1 is one in English, while all are other in Japanese. This is a reference test regarding the rules used by the library and does not actually retrieve translation results from a fixed version of i18next.

Determining the values accepted as the count

The implementation converts the input field value into a number using Number. Blank fields or strings containing whitespace may be converted to 0 in some cases. Characters like abc result in NaN. This demo does not include processing to validate the integer nature or non-negative range of the count. For a screen that actually displays the number of inquiries, it may be more natural to distinguish blank as unretrieved and 0 as no inquiries. For use cases where negative numbers or decimals are possible, define their meaning separately.

Displaying 0 items and showing a message such as "No new notifications" are separate requirements. Although i18next provides a way to set up resources dedicated to 0 items, the learning material does not include this. Add necessary translation keys according to specifications and check the display when there is no sentence dedicated to 0 items. To make it easier to detect missing translation resources, it is recommended to maintain expected sentences as tests for each language.

Applying to multilingual UI testing

When adding languages in production, prepare representative counts for each language and check the sentences rendered on the screen. If translated texts become long, buttons or notification areas might wrap. Verify separately that the grammar is correct and that the text is easy to read on the screen. Also, verify whether the counts themselves are correct through separate business testing.

If a result is not returned for a normal input of 1, check the acquisition of dependency modules and the initialization of translation resources. If the display is missing in only a specific language, verify whether the translation key, count, and lng are set to the expected values. The public demo uses only fictitious numbers. Do not enter actual customer information into translation resources or input fields.

Verification Scope

Target of learning material: i18next 24.2.3. Checked 0/1/2 using Node.js v22.16.0 Intl.PluralRules. English other/one/other, Japanese other/other/other. The core i18next 24.2.3 itself was not executed. Acquisition of external CDNs, licensing of fixed versions, actual browser rendering, and Mermaid diagram generation remain unconfirmed.

References

GitHub Sample

Implementation and Experiment Procedure

Document information

Article title
Switching between "single" and "multiple" messages with i18next
Published
Updated
Source
https://papanda925.com/?p=19125&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL