From 1b57d9ea6513b81e538677f9ebf221d0c635f482 Mon Sep 17 00:00:00 2001 From: INOUE Daisuke Date: Tue, 8 Apr 2025 22:18:39 +0900 Subject: Plural with i18next --- src/app.ts | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 5fe0100..ddfc101 100755 --- a/src/app.ts +++ b/src/app.ts @@ -1,5 +1,6 @@ import express from "express"; -import hbs from "express-handlebars"; +import hbs, { ExpressHandlebars } from "express-handlebars"; +import Handlebars from 'handlebars'; import cookieParser from "cookie-parser"; import i18next from "i18next"; import Backend from "i18next-fs-backend"; @@ -98,31 +99,20 @@ async function initializeApp() { }); // View engine // - const hbsInstance = hbs.create({ + const hbsInstance: ExpressHandlebars = hbs.create({ defaultLayout: "main", partialsDir: ["views/partials/"], layoutsDir: "views/layouts/", helpers: { - plural: function (number: number, text: string) { - var singular = number === 1; - // If no text parameter was given, just return a conditional s. - if (typeof text !== "string") return singular ? "" : "s"; - // Split with regex into group1/group2 or group1(group3) - var match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); - // If no match, just append a conditional s. - if (!match) return text + (singular ? "" : "s"); - // We have a good match, so fire away - return ( - (singular && match[1]) || // Singular case - match[2] || // Plural case: 'bagel/bagels' --> bagels - match[1] + (match[3] || "s") - ); // Plural case: 'bagel(s)' or 'bagel' --> bagels - }, json: function (context: any) { return JSON.stringify(context); }, // i18nextヘルパーを追加 - ...getI18nHelpers() + ...getI18nHelpers(), + plural: function (key: string, count: number, options: any) { // ★plural ヘルパーを登録 + const translation = i18next.t(key, { count: count }); + return translation; + } }, }); @@ -135,6 +125,12 @@ async function initializeApp() { console.error('handlebars-i18next helper is not properly loaded'); } + + (hbsInstance.handlebars as typeof Handlebars).registerHelper('pluralize', function(count: number, key: string, options: any) { + const translation = i18next.t(key, { count: count }); + return translation; + }); + app.engine("handlebars", hbsInstance.engine); app.set("view engine", "handlebars"); app.set("hbsInstance", hbsInstance); -- cgit v1.2.3