Giter Club home page Giter Club logo

i18n-node-2's Issues

nested json not work

I cannot use the following notation to access ___('contact.name')_

{
  "contact": {
      "name": "姓名"
  }

}

In i18n, this is allowed:

https://github.com/mashpie/i18n-node#object-notation

Instead of calling __("Hello") you might call __("greeting.formal") to retrieve a formal greeting from a translation document like this one:

"greeting": {
    "formal": "Hello",
    "informal": "Hi",
    "placeholder": {
        "formal": "Hello %s",
        "informal": "Hi %s"
    }
}

Extract strings beforehand

Is there a way to extract the translation strings beforehand, like with gulp or grunt? Seems foolish to translate only on page request, it would be better if it was done before build.

how to use for ejs template?

exports.index = function(req, res){
res.render('index', { title: 'Welcome' , slides: req.i18n.__('slides') });
};
<h2><%= slides %></h2>

"express": "3.1.0"

I get this error 500 TypeError: Cannot call method '__' of undefined

License?

The mashpie/i18n-node base project is MIT, but no license is found in this repo, nor even referred to in the package.json.

Can this be rectified? I can't use this in my projects without a permissive license, and certainly, no license is not permissive. 😄

Using Module for Client-side Validation

Hi,

I have been trying how to work out how I might make use of i18n-2 for Client side validation I am using Happy.js (It is on top of JQuery Validation) but essentially I am trying to access the values from the client. Have you considered this or do you have any advice?

Thanks,

Andrew

__n() treats zero as singular

Given that we have the following function:

i18n.__n('%s movie', '%s movies', 0);

We get 0 movie back. Surely, this should be 0 movies? Is there a specific reason for this behaviour?

Cheers!

i18n-node-2 for Hapi

Hey folks,

I've read both @jeresig posts about his i18n strategy (here and here) and I like what you've accomplished here. The thing is, I use Hapi instead of Express. Since this module is MIT licenced, I'm letting you know that I'm going to adapt it to work with Hapi and launch it as a plugin. Hope that's ok.

preferredLocale method is incorrect result.

I think this method has bug, my accept-language header is "ja,en-US;q=0.8,en;q=0.6,zh;q=0.4,hu;q=0.2" and locales is "['en', 'zh-CN']",console output "prefLocale" all is undefined. isPreferredLocale() always return false.

preferredLocale: function(req) {
        req = req || this.request;

        if (!req || !req.headers) {
            return;
        }

        var accept = req.headers["accept-language"] || "",
            regExp = /(^|,\s*)([a-z-]+)/gi,
            self = this,
            prefLocale;

        while ((match = regExp.exec(accept))){
            var locale = match[2];
            var parts = locale.split("-");

            if (!prefLocale) {
                if (self.locales[locale]) {
                    prefLocale = locale;
                } else if (parts.length > 1 && self.locales[parts[0]]) {
                    prefLocale = parts[0];
                }
            }
        }

        return prefLocale || this.defaultLocale;
    }

Accept-Language header parsing ignores region tags?

Currently line 224:

(accept.match(/(^|,\s*)([a-z]+)/g) || []).forEach(function(locale) {

https://github.com/jeresig/i18n-node-2/blob/master/i18n.js#L224

This only grabs the language tag, not the region tag, so it doesn't differentiate between e.g. simplified Chinese (zh-CN) and traditional (zh-TW).

@jed's locale and @mozilla's i18n-abide both have parsing code that takes region tags and even score into account.

E.g. https://github.com/mozilla/i18n-abide/blob/master/lib/i18n.js#L269-L295

Hope that helps!

Expess code from readme not working ...

Hi,

I am doing the following but this is not working for me.

At the top of my app.js, I have:

i18n = require('i18n-2');

In my app.configure method, I have at the end:

// multilanguage support
i18n.expressBind(app, {
// setup some locales - other locales default to en silently
locales: ['en', 'de'],
defaultLocale: 'en'
});

I have created a folder at the root where app.js is, named 'locales' and have a en.js file there with simply:

{
"Hello": "I am Hello in English"
}

i18n.("Hello") does not work anywhere in the app whereas in the request req.i18n.("Hello") does not work either.

The error I keep getting says something like "has not method __"

Can you help ?

Why convert locales to lowercase in methods like setLocaleFromQuery()?

Some methods like setLocaleFromQuery(), setLocaleFromCookie() will convert the locale to lower case. I see a lot of projects use uppercase letter in their i18n locales file name like en-US.json, zh-CN.json.
Also, the accept language in HTTP request header use the uppercase letter in the locale like en,zh-CN;q=0.8,zh;q=0.6.
So my question is why those methods convert locales to lowercase? Why not just keep the original case?

release 0.4.7 on npmjs.com

Hi,

I'd like to use the latest version 0.4.7 which implements the dot notation for the translation keys.

Please update the release on npmjs.com

Cheers

bind i18n in express

when we bind i18n in express
we can use __() method in our swig template.

why we could use __() methdod in our swig template ?

I have seen the i18n source code ,but I still can't figure these problem out?

readFile error checking

While deploying my express 3.X app the first time to production, I ran into an issue with readFile where it wasn't able to read the locales directory, and also didn't have permissions to write new files. Relevant code from i18n.js (https://github.com/jeresig/i18n-node-2/blob/master/i18n.js#L268):

try {
    var localeFile = fs.readFileSync(file);
    //... continues
} catch (e) {
    this.writeFile(locale);
}

The end result is that my app complains that it can't find res.locals.__. Which makes sense - the middleware for express in expressBind calls new i18n(opts) which isn't successful, and thus (silently AFAIK) dies.

I think a reasonable thing to do might be to wrap the writeFile call in a try/catch and log an error. I can create a PR if that seems reasonable to you.

(PS: I'm not asking for help with my app - I just needed to explicitly pass the directory option)

weird bug

I have translation files inside ./locales (en.js and it.js)
Sometimes the translation files itself are being modified, do you know what's causing this?

And also if I try to translate a missing key, it gets added automatically in the translation file

Edit: it seems it's a feature stated here Adds new strings on-the-fly when first used in your app
Is there an option to turn this off? it's messing my translation files.

Thank you for a very nice module, I hope you can help me with this.

Dictionaries inheritance proposal

It would be nice to have a feature like inheritance between dictionaries that could spare a lot of duplications.
Assuming we have files de-de.json and at-de.json - both extending the root de.json - we would provide the options:

{
    "locales": ["de-de", "at-de"],
    "extension": ".json",
    "parents": ".{2}\.json"
}

Where parents is a regexp for extracting parent name for each file. What do you think?

Swig Problem

Hey,

First of all great work on this module. It was exactly what i was looking for.

I'm trying to use it with swig and it's not really working

I get the following error: " 500 TypeError: Cannot read property '__' of undefined".

Not sure what else i need to do to make it work.

Thanks

Update npm package

Could you update the npm package to the latest version?
The latest version that appear in npm is 0.4.6

setLocaleFromSessionVar

  1. Why you didn't describe setLocaleFromSessionVar function in documentation?
  2. I propose to make variable in session configurable like cookieName.

Accept-Language is not used at all

prefLocale variable seems to be unused.
Or please give an example with ejs and language detection just based on accept-language header (no cookies and no subdomains).

Feature Request: General Place-holders

Would it be feasible to add support for general place-holders, where one localised string could refer to another one? This would enable common terms/phrases to be used in the content of others, without requiring parameter parsing at the code level.

For example;

{
   "contact service desk": "contact Service Desk (x3000)",
   "wrong id": "You've entered a wrong ID. Please try again, or {{contact service desk}} for further assistance",
   "db error": "Oops! Something is wrong with our servers. If this problem persists, please {{contact service desk}}"
}

In this way, any mention of contacting the Service Desk is consistent, and if the message (eg; the phone extension) changes, it's reflected across all other references.

Preferably, such a feature would be able to be enabled/disabled, and the place-holder syntax be customisable, to avoid clashes with real content syntax in templates, etc...

Here's an idea for an alternative syntax:

{
   "contact service desk": "contact Service Desk (x3000)",
   "wrong id": ["You've entered a wrong ID. Please try again, or %p for further assistance", "contact service desk"],
   "db error": ["Oops! Something is wrong with our servers. If this problem persists, please %p", "contact service desk"]
}

This syntax is similar to the "%s" place-holder syntax, but in this case, the substituted value is another i18n entry, named within the locales config.

Auto-detect supported locales from locales directory?

It would be great if I didn't have to specify the locales manually, if instead it could just be derived automatically by seeing which extension (e.g. .js) files were in directory (e.g. ./locales).

That way, you wouldn't need to maintain the list of locales in the configuration separately from the files you've had translated.

Would you be open to that? I might be able to put together a pull req if so. Thanks again for the great lib!

Splitting resources into multiple files

I would like to implement a feature of the library where localisation keys can be split amongst multiple files, which can be 'namespaced' by the file name. The reason is mainly for organisation on large sites where there may be hundreds or thousands of keys.

Things for consideration:

  • There would be the default locale file plus n 'namespaced' files
  • The default file could remain en.js whereas additional files could be countries-en.js, months-en.js, etc.
  • We would need some kind of marker to indicate which file to pull from if not the default. Something like i18n.__("countries.Germany") or i18n.__("Germany", "countries"). Either with some kind of marker in the key name as in the former example, or with a method parameter as in the latter (I'm leaning towards the latter)
  • If no file is specified, then the key is pulled from the default file
  • If a file is specified but a key name is not found, then it is searched for in the default file. If it is not found in the default file, then a key is created as in the current behaviour

Is there anything else I should consider?

register option bug

I don't use the library but was reading the code for ideas. I think the code at line 28

i18n.registerMethods.forEach(function(method) {

should be

i18n.resMethods.forEach(function(method) {

Templating i18n with dot.js

I'm trying to make it work with dotjs like the swig example

<h1>{{ __("Welcome to:") }}</h1>

Dot.js uses {{=it.object}} for rendering variables, so {{=it.__("Welcome to:") }} or {{__("Welcome to:") }} does not work.
Should I pass the entire i18n object to the template in express and render like this:?

<h1>{{ =it.i18n.welcome_message }}</h1>

using __ and __n with express-handlebars views

When trying to access a locale property inside a handlebars view like follows:

  <h1>{{ __("someProperty") }} </h1>

I get a Parse Error with the following stack trace:

Error: Parse error on line 46:
... <h1>{{ __("someProperty") }} </h1>
-----------^
Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'
    at Object.parseError (./node_modules/express-handlebars/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:218:19)
    at Object.parse (./node_modules/express-handlebars/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:287:30)
    at HandlebarsEnvironment.parse (./node_modules/express-handlebars/node_modules/handlebars/dist/cjs/handlebars/compiler/base.js:45:43)
    at compileInput (./node_modules/express-handlebars/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:477:19)
    at ret (./node_modules/express-handlebars/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:486:18)
    at ExpressHandlebars._renderTemplate ./node_modules/express-handlebars/lib/express-handlebars.js:247:12)
    at ExpressHandlebars.<anonymous> (./node_modules/express-handlebars/lib/express-handlebars.js:173:21)

I can only circumvent this by using:

  <h1>{{ __ "someProperty" }} </h1>

Is this the intended use for handlebars or am I missing some helper function ?

fallback to default locale

When in production mode and a key does not exist in the current language, the key is displayed. We have some keys that are generated so we want to avoid showing them to the customer even if a translation in the current language is not available, instead we would like to show the translation in the default language.

Would it be ok to first check the translation in the default locale before returning the key?

Something like:

    if (!this.locales[locale][singular]) {
        if (this.devMode) {
            this.locales[locale][singular] = plural ? { one: singular, other: plural } : singular;
            this.writeFile(locale);
        } else {
            locale = this.defaultLocale;
        }
    }

Locale file overwritten on parsing error

If there is a json parsing error when i18n tries to open a locale file, it will create a new file and overwrite any existing one. Needless to say, it is not a good idea if you have spent a lot of time to create a file and then lose it simply because of a parsing error, which happened to me. I suggest you add fs.existsSync(localeFile) right above this.writeFile(locale) so that it will ONLY write file if localeFile doesn't exist.

        try {
            var localeFile = fs.readFileSync(file);

            try {
                // parsing filecontents to locales[locale]
                this.initLocale(locale, JSON.parse(localeFile));

            } catch (e) {
                console.error('unable to parse locales from file (maybe ' + file +
                    ' is empty or invalid json?): ', e);
            }

        } catch (e) {
            // unable to read, so intialize that file
            // locales[locale] are already set in memory, so no extra read required
            // or locales[locale] are empty, which initializes an empty locale.json file
            // @ec1980: Do not overwrite existing locale files
            if (!fs.existsSync(localeFile))
                this.writeFile(locale);
        }

BTW, if you are scratching your head trying to find out why your perfectly okay json file causes unexpected token parsing error, check that the file is not encoded with UTF-8 BOM, which is the default format on Windows. You can disable it in Visual Studio -> File -> Advanced Save Options -> Encoding -> Unicode(UTF-8 without signature).

Using i18n-2 in models

Hi!

I would to use i18n-2 in models js files, during data valitation.
How to access i18n2 functions?

I tried with

var i18n = require('i18n-2');

var validationError = i18n.__("Validation error");

but i get "i18n.__("Validation error")
^
TypeError: undefined is not a function"

What am i doing wrong?

Thanks

Readfile on each request?

Firstoff: Nice rewrite - your thoughts would have been helpful, when contributed, although your approach acts more like middleware than as independent module.

Looking at your code ist seams like each reqeuest leads to a full read of translation files in a blocking manner. I doupt, this is intendet?

JSON Object notation

How to access text when the locale JSON file has nested text: For example:
{
"main-page": {
"title": "This is the main title"
}
}

Trying to access this via I18n.__("main-page.title") returns the string "main-page.title" instead of the intended "This is the main title".

Would you mind to clarify how to access nested JSON elements within the locale file?

/intl/.. support

Not really an issue but is there a way to use /intl/language instead of using a query string to set the language?

Setting locale form browser

Hello,

Is it possible to set the locale according to the language desired by the browser's Accept-language header ?

Testing to see if a translation exists

First of all thanks for sharing your hard work on i18n-2. Secondly I'd like to make a change but wanted to check it was something you're interested in first.

We're using the following snippet to localise 'Word document', 'Powerpoint document', etc.

req.i18n.__('fileType.' + attachment.type);                

If someone uploads a file type that has no translation for this will end up displaying 'fileType.xyz', when really we want to display nothing. We could maintain a list of known filetypes but I'd rather test to see if the translation is available.

var key = 'fileType.' + attachment.type;
req.i18n.__e(key) ? req.i18n.__(key) : '';

A further enhancement could be to pass the default value to req.i18n.__ but because of the var args I suspect this would require a new method, e.g.

req.i18n.__d(key, 'default template if key not found', 'arg1', 'arg2');

Not quite sure how this could be made to work with plurals though. Assuming you're happy with the above I'll fork and PR

Request with no "accept-language" header

"preferredLocale" function in i18n.js fails when there is no accept-language header. Proposed fix:

preferredLocale: function(req) {
        req = req || this.request;

        if (!req || !req.headers) {
            return;
        }

        var accept = req.headers["accept-language"],
            self = this,
            prefLocale;

        if (accept) {
            accept.match(/(^|,\s*)([a-z]+)/g).forEach(function(locale) {
                if (!prefLocale && self.locales[locale]) {
                    prefLocale = locale;
                }
            });
        } else {
            prefLocale = self.defaultLocale;
        }

        return prefLocale;
    }

New strings not saved

Hello,

As in the title, new strings are not saved to locales file in version 0.6.0

I added a quick fix in after line 329 like this:

if (!this.locales[locale][singular]) {
this.locales[locale][singular] = singular; // the fix
if (this.devMode) {
this.writeFile(locale);
}
}

Hope this will be fixed soon

Compatiblity with Swig's filters

It would be quite useful to able to combine i18n with Swig's filters.
So far I couldn't make it work, is it supported?

{{ __('this is a test')|title }} // Should return 'This Is A Test'

Mocha test fails because of global var match

Hi,

I noticed that our mocha tests started failing when we added this module with the following error:

Error: global leak detected: match

We've solved it by running the mocha command with --globals match but it would be nice not to have to do this.

I've looked through i18n.js and I think that this could be solved by adding a var match;right before:

225: while ((match = regExp.exec(accept))){

I could make a pull request for it.

How access translations in a structured json?

How can I access translations in a structured json?

My json:

{
    "first-level": "shown",
    "test": {
        "lorem": "ipsum",
        "foo": "bar"
    }
}
req.i18n.__('first-level') // prints "shown"
req.i18n.__('test.lorem') // prints "test.lorem"

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.