lephyrus / ngx-translate-messageformat-compiler

Advanced pluralization (and more) for ngx-translate, using standard ICU syntax which is compiled with the help of messageformat.js.
MIT License
93 stars 29 forks source link

Disable first parsing and compiling in default locale (EN) #40

Closed artuska closed 5 years ago

artuska commented 5 years ago

Messagesformat's default locale is EN. When ngx-translate-messageformat-compiler loads my locale JSON file it first parses it with EN locale and throws me an errors like this:

ERROR Error: Invalid key `few` for argument `count`. Valid plural keys for this locale are `one`, `other`, and explicit keys like `=0`.

This is my module config:

@NgModule({
    imports: [
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useClass: TranslationLoader,
                deps: [HttpClient]
            },
            compiler: {
                provide: TranslateCompiler,
                useClass: TranslateMessageFormatCompiler,
            }
        })
    ],
    providers: [
        {
            provide: MESSAGE_FORMAT_CONFIG,
            useValue: {
                defaultLocale: 'ru'
            }
        }
    ]
})

As you see i have explicitly set defaultLocale to ru but with no success — i still got console errors about invalid plural keys.

artuska commented 5 years ago

I also tried this:

    providers: [
        {
            provide: MESSAGE_FORMAT_CONFIG,
            useValue: {
                locales: ['en', 'ru']
            }
        }
    ]

And still got console errors about invalid plural keys.

Then I tried this:

    providers: [
        {
            provide: MESSAGE_FORMAT_CONFIG,
            useValue: {
                locales: ['ru', 'en']
            }
        }
    ]

And it works... but it is just a dirty workaround.

lephyrus commented 5 years ago
  1. It's documented that the first locale in the locales array will be the default locale. I will mention this explicitely in the README though, so thank you for the feedback.
  2. If you read the README if this library, you will see that there is no defaultLocale configuration option. I don't think adding it makes sense - the value of locales would override it anyway.
  3. I'm not sure why ngx-translate initially compiles your messages with the messageformat default language. The default language (for messageformat, not ngx-translate) should only matter if the compile() method is called with an undefined language, which shouldn't be the case with ngx-translate.

@artuska If that still bothers you, please debug the problem some more (see point 3 above) - otherwise feel free to close this issue.