Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug - Date not localized on Node V7.7.1 #34

Closed
ManuelOverdijk opened this issue May 22, 2017 · 8 comments

Comments

Projects
None yet
2 participants
@ManuelOverdijk
Copy link

commented May 22, 2017

I've noticed that es2015-i18n-tag does not properly localize dates on node V7.7.1. I'm not sure if it's related to node, but in the browser (chrome & chrome Canary) it works just fine with the same setup.

Setup:

 i18nConfig({
        locales: 'nl-NL',
        number: {currency: 'EUR'},
        translations: translations['nl-NL'] || {}
    });

In the browser the expected output is for

i18n`${new Date()}:t(D)`

is "maandag 22 mei 2017", which is properly localized in the set locale. In Node v7.7.1 however, the output is till in English. Translating a string in node from my translations object works just fine, giving the suspicion the locale is properly set.

@skolmer

This comment has been minimized.

Copy link
Owner

commented May 22, 2017

In a Node.js environment you have to use the Intl Polyfill to add support for additional locales. If you haven't already installed Intl.js this should work:

npm install intl

import 'intl';  // import polyfill for ECMAScript Internationalization API
import 'intl/locale-data/jsonp/nl.js';  // import localization data for NL
@ManuelOverdijk

This comment has been minimized.

Copy link
Author

commented May 22, 2017

Thanks for the quick response! Unfortunately the same result, also tried 'intl-locales-supported' which did not help

@skolmer

This comment has been minimized.

Copy link
Owner

commented May 22, 2017

To better understand the issue can you run Intl.NumberFormat.supportedLocalesOf("nl-NL") on application start and post the result here?

@ManuelOverdijk

This comment has been minimized.

Copy link
Author

commented May 22, 2017

The result is an empty Array.

Full test code:

var i18nLibrary = require('es2015-i18n-tag');
var i18n = i18nLibrary.default;
var i18nConfig = i18nLibrary.i18nConfig;

i18nConfig({
    locales: 'nl',
    number: { currency: 'EUR' },
    translations: {}
});

require('intl');
require('intl/locale-data/jsonp/nl-NL.js');
Intl.NumberFormat.supportedLocalesOf("nl-NL");
@skolmer

This comment has been minimized.

Copy link
Owner

commented May 22, 2017

It looks like the locale data is not loaded as expected. Does it change if you use

var IntlPolyfill = require('intl');
require('intl/locale-data/jsonp/nl-NL.js');

Intl.NumberFormat   = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat; 

Intl.NumberFormat.supportedLocalesOf("nl-NL");

or

global.Intl = require('intl');
require('intl/locale-data/jsonp/nl-NL.js');

Intl.NumberFormat.supportedLocalesOf("nl-NL");
@ManuelOverdijk

This comment has been minimized.

Copy link
Author

commented May 22, 2017

Thanks again for the quick input, really appreciated! With both of your provided options the output of Intl.NumberFormat.supportedLocalesOf("nl-NL") is [ 'nl-NL' ], however i18n still does not properly localise a date :(

@skolmer

This comment has been minimized.

Copy link
Owner

commented May 22, 2017

I just looked in to my tests where I'm using the Intl prolyfill and found this interesting line of code: https://github.com/skolmer/es2015-i18n-tag/blob/master/__tests__/es2015-i18n-tag.test.js#L8

It looks like I had to patch the "toLocaleString" functions of Date and Number which are used by the i18n lib to format those types. Can you give it a last try with the following code?

global.Intl = require('intl');
require('intl/locale-data/jsonp/nl-NL.js');

Intl.__applyLocaleSensitivePrototypes();
@ManuelOverdijk

This comment has been minimized.

Copy link
Author

commented May 22, 2017

Perfect, thank you very much!

For anyone experiencing the same issues, here is a full working example for Dutch:

global.Intl = require('intl');
require('intl/locale-data/jsonp/nl-NL.js');

Intl.__applyLocaleSensitivePrototypes();

var i18nLibrary = require('es2015-i18n-tag');
var i18n = i18nLibrary.default;
var i18nConfig = i18nLibrary.i18nConfig;

i18nConfig({
    locales: 'nl-NL',
    number: {currency: 'EUR'},
    translations: {}
});
i18n`${new Date()}:t(D)` // should fully work now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.