Globalize Version 1.2.3 - root/fallback Culture

204 views Asked by At

i'm using the current release version 1.2.3 and would like to use the functionality described here: https://github.com/globalizejs/globalize/blob/6d3a5a57c56fd0afb93e20340905a435d1650341/doc/api/message/load-translation.md#example

I need to inherit translations to have a fallback culture for the case that a browser culture is not defined / initialized in my loadMessages method. For example:

Globalize.loadMessages({ root: { hello: "Hi!" }, de: { hello: "Moin!" } });

This is what i expect:

Globalize( "de-DE" ).formatMessage( "hello" );

"Moin!"

Globalize( "fr" ).formatMessage( "hello" );

"Hi!" (fallback)

How can i realize this?

1

There are 1 answers

0
Rafael Xavier On

Short answer

It's missing Globalize.loadMessages({fr: {}});. The empty message tells globalize fr is a valid bundle.

Longer answer

The documentation example works just fine. Note: Use v1.2.3 documentation here: https://github.com/globalizejs/globalize/blob/1.2.3/doc/api/message/load-messages.md

In order to test I did:

npm install globalize cldr-data

Then:

var Globalize = require( "globalize" );
Globalize.load( require( "cldr-data" ).entireSupplemental() );

Globalize.loadMessages({
  root: {
    amen: "Amen"
  },
  de: {},
  en: {},
  "en-GB": {},
  fr: {},
  pt: {
    amen: "Amém"
  },
  "pt-PT": {}
});

Globalize( "de" ).formatMessage( "amen" );
// > "Amen"

Globalize( "en" ).formatMessage( "amen" );
// > "Amen"

Globalize( "en-GB" ).formatMessage( "amen" );
// > "Amen"

Globalize( "fr" ).formatMessage( "amen" );
// > "Amen"

Globalize( "pt-PT" ).formatMessage( "amen" );
// > "Amém"