summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorINOUE Daisuke <inoue.daisuke@gmail.com>2025-04-08 22:18:39 +0900
committerINOUE Daisuke <inoue.daisuke@gmail.com>2025-04-08 22:18:39 +0900
commit1b57d9ea6513b81e538677f9ebf221d0c635f482 (patch)
tree14b748f14f2d123c9dc1e9ae73978e6d618839b8 /src
parent90bdf104d76674d307cbd50dc1cf3d973b663471 (diff)
Plural with i18next
Diffstat (limited to 'src')
-rwxr-xr-xsrc/app.ts32
1 files changed, 14 insertions, 18 deletions
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);