funktionswerk / hapi-i18n

Translation module for hapi based on mashpie's i18n module
MIT License
39 stars 22 forks source link

default locale and query parameter #41

Closed tcurdt closed 3 years ago

tcurdt commented 3 years ago

I was trying with

        {
            plugin: 'hapi-i18n',
            options: {
                // languageHeaderField: 'accept-language',
                // languageHeaderField: 'language',
                locales: ['de', 'en', 'fr'],
                defaultLocale: 'fr',
                // queryParameter: 'lang',
                directory: Path.resolve(__dirname, './server/locales')
            }
        }

From the docs I was under the impression that language below should default to fr if no headers and no query parameter are provided:

async function handler (request, _h) {
    const language = request.params.languageCode

But it's just undefined. The language is set directly on the request though

async function handler (request, _h) {
    const language = request.language

...but I don't see how to change the language via query parameter.

What am I missing?

kaywolter commented 3 years ago

Hey Torsten,

i18n will be set to 'fr' when no headers or query parameters are passed with the request, but languageCode/language on request object will still be undefined. The plugin does not change those parameters. If you want to set the language code via query parameter you need to activate queryParameter: 'lang' and send the language code with the request:

curl localhost/some-page?lang=en

I hope this answers your question.

Cheers, Kay

tcurdt commented 3 years ago

Thanks, @kaywolter. I still find the README a little misguiding - but things start to make more sense. According to your response I guess I should be able to get the i18n locale via request.i18n?

kaywolter commented 3 years ago

Yes, if everything is correct then i18n will be attached to request object.