localizify
localizify copied to clipboard
Unable to use localizify in constant file
Hello there, please help me. I just develop REST API with express and I want to use my constant file which refers to localized strings, but the response is always in default English (even if I change the locale in headers).
app.js
const { default: localizify } = require("localizify")
const en = require("./locales/en/lang.json")
const id = require("./locales/id/lang.json")
localizify.add("id", id).add("en", en)
app.use((req, res, next) => {
localizify.setLocale(req.headers["locale"] || "id")
next()
})
constants/dayoff.js
const { t } = require("localizify")
exports.DAY_OFF = { name: t("calendar.dayoff.label") }
routes/calendar.js
router.get("/", (req, res) => {
const dayOff = require("../constants/dayoff.js")
res.status(200).send(dayOff.DAY_OFF) // the response: { "name": "Day Off" }
// I want the response to be: { "name": "Hari Libur" } when I set "id" in the locale in header, but it didn't work.
})
But if I call "t" from the router, it works well.
routes/calendar.js
const { t } = require("localizify")
router.get("/", (req, res) => {
const name = t("calendar.dayoff.label")
res.status(200).send({name}) // the response if the locale is set to "id": { "name": "Hari Libur" }
})
I have no idea to configure it. Any advice?