noveogroup-amorgunov / localizify Goto Github PK
View Code? Open in Web Editor NEWEasy localize your messages
License: MIT License
Easy localize your messages
License: MIT License
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?
Translating with a data parameter of value 0 does not get output as expected.
test case: t('Likes: {count}', { count: 0 })
expected: Likes: 0
actual: Likes: {count}
I chased this down to the _replaceData
function using ||
for fallback handling.
This makes sense to prevent null
or undefined
values in the output, but as 0
is also interpreted as false, it falls-thru as-well.
const replaceTo = data[term] || this.getStore().interpolations[term] || _term;
https://github.com/noveogroup-amorgunov/localizify/blob/master/src/localizify.js#L180
Possible solution, simply check for 0
and allow it thru.
// find first useful value, or default to last value
function coalesce(...values) {
const foundIdx = values.findIndex(value => value === 0 || value);
return foundIdx > -1 ? values[foundIdx] : values[values.length - 1];
}
const replaceTo = firstValue(data[term], this.getStore().interpolations[term], _term);
Of course there exists a similar limitation for false
, and objects getting output as [object Object]
, so the check could be made specific to types:
// allow non-empty string, and any number or boolean
// disallow object, array, null, undefined
function acceptedType(value) {
return value !== '' && ['string', 'number', 'boolean'].includes(typeof value);
}
function coalesce(...values) {
const foundIdx = values.findIndex(acceptedType);
return foundIdx > -1 ? values[foundIdx] : values[values.length - 1];
}
When user follow the README.md file for integration of localizify into there project/local machine.
They do not able to require and additionaly the method that are in localizify, They can't use it.
such that I suggest that we should change the README.md file, and update it as necessary.
const { default: localizify } = require('localizify');
With the last release (1.2.0) the package stop working, require('localizify') no more returns a Singleton, but a simple Object.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.