GithubHelp home page GithubHelp logo

noveogroup-amorgunov / localizify Goto Github PK

View Code? Open in Web Editor NEW
28.0 28.0 3.0 3.84 MB

Easy localize your messages

License: MIT License

JavaScript 4.65% TypeScript 95.35%
i18n internationalization javascript library localization nodejs react translation

localizify's People

Contributors

noveogroup-amorgunov avatar zubb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

localizify's Issues

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?

Handling number 0 in translate _replaceData

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];
}

Unable to require <localizify>

In the Official docs of localizify...

  • 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'); 

Version 1.2.0 not working.

With the last release (1.2.0) the package stop working, require('localizify') no more returns a Singleton, but a simple Object.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.