rxaviers / globalize-webpack-plugin

Globalize.js webpack plugin
Other
33 stars 27 forks source link

Using Globalize.loadMessages() #26

Open Matt-Butler opened 8 years ago

Matt-Butler commented 8 years ago

If I want to use Globalize.loadMessages(), is that possible? Currently, I am receiving an error when trying to add a translation during runtime.

For example, I want to encapsulate some translations in a module where a consumer cannot edit them.

import Globalize from 'globalize';

Globalize.loadMessages({
  en: {
    greetings: {
      hello: "Olá",
      bye: "Tchau"
    }
  }
});

Recieving error: => Uncaught TypeError: r.default.loadMessages is not a function

I need this functionality, so I would possibly contribute.

rxaviers commented 8 years ago

Currently, I am receiving an error when trying to add a translation during runtime.

During runtime you don't have parts of the library that generates the formatters. It's meant only for executing them.

I want to encapsulate some translations in a module where a consumer cannot edit them.

Are you a library owner/maintainer? Anyway, I'm also wondering about the messages you want to encapsulate, do they change per locale? Are you going to include them for several locales, what should happen for missing locales?

I want to hear you first, but anyway I think the solution would lie (somehow) at packages themselves supplying translations and this plugin perceiving it and loading them by default. For example, let's say packages have in their package.json an entry i18n-messages which points to local translations, like i18n-messages: './i18n/[locale].json'. With that in place, this plugin could check for that (somehow) and include these messages along with the user provided messages (perhaps having the user provided ones to take precedence over the package provided ones). See what I mean?

Matt-Butler commented 8 years ago

@rxaviers , Thanks again for your diligent responses.

+---------------+            +--------------+
| /Module_1     |            |/Module_2     |
|   /messages   |            |  /messages   |
|     en.json   |            |    en.json   |
|     de.json   |            |    de.json   |
|     es.json   |            |    es.json   |
+-------------+-+            +-----+--------+
             ^                    ^
             |                    |
             |                    |
             |                    |
            ++--------------------+-+
            | /Application          |
            |   /messages           |
            |     /en.json          |
            |     /de.json          |
            |     /es.json          |
            |   webpack.config.js   |
            +-----------------------+

I want to be able to set the locale in the application. Then in my modules, I want to be able to load messages based on the locale. If the locale is 'en', I don't want to load the 'es' messages.

I thought I could do something like this:

// import jQuery Globalize
import Globalize from 'globalize';

// create string for locale messages
messages_file = '/messages/ ' + Globalize.locale().locale + '.json';

//
Globalize.loadMessages(messages_file);

Are you a library owner/maintainer?

I am creating a front-end library. It has several modules, each module being a feature. I want each module to have messages. If the module is not imported with webpack, I don't want to include the messages I don't need.

Anyway, I'm also wondering about the messages you want to encapsulate, do they change per locale? Are you going to include them for several locales, what should happen for missing locales?

Each locale will have the same messages. If a locale or translation is missing, it will currently blow up. During development, each locale and each module will be tested. I want it to blow up so it is obvious a message is missing. I do not want to fall back to a different locale if something is missing. Having mixed locales in an application can lead to major defects (especially around dates).

Thank you for the suggested solution. I will try and have the plugin identify messages in each of the modules by using i18n-messages: './i18n/[locale].json' in package.json

Matt-Butler commented 8 years ago

Just out of curiosity, what is your recommended approach for when messages/locales are missing?