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

Using Globalize.loadMessages() #26

Open
Matt-Butler opened this issue Apr 6, 2016 · 3 comments
Open

Using Globalize.loadMessages() #26

Matt-Butler opened this issue Apr 6, 2016 · 3 comments

Comments

@Matt-Butler
Copy link

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
Copy link
Owner

rxaviers commented Apr 8, 2016

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
Copy link
Author

@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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants