Socket
Book a DemoInstallSign in
Socket

@awes-io/nuxt-i18n

Package Overview
Dependencies
Maintainers
3
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@awes-io/nuxt-i18n

Nuxt i18n module for loading translations from API endpoint

0.5.0
latest
Source
npmnpm
Version published
Maintainers
3
Created
Source

Awes.io Module for Loading Translations

Based on nuxt-i18n

This modules fetches translations into your Nuxj.js project from the given endpoint, and provides automatic nuxt-i18n configuration.

Basic usage

  • Install the module yarn add @awes-io/nuxt-i18n
  • Write basic config
// nuxt.config.js

export default {
    modules: ['@awes-io/nuxt-i18n'],

    awesIo: {
        nuxtI18n: {
            locales: [
                { code: 'de-DE', domain: 'site.de' },
                { code: 'en-GB', domain: 'site.uk' }
            ]
            // for same domain it could be even shorter: ['de-DE', 'en-GB']
        }
    }
}

That's it!

This minimal config will do the following:

// nuxt.config.js

export default {
    i18n: {
        defaultLocale: 'de', // first in array
        locales: [
            {
                code: 'de',
                iso: 'de-DE',
                domain: 'site.de',
                file: '/translations-de.js' // auto-generated file
            },
            {
                code: 'en',
                iso: 'en-GB',
                domain: 'site.uk',
                file: '/translations-en.js' // auto-generated file
            }
        ],
        differentDomains: true,
        langDir: '.nuxt/awes-io/lang'
    }
}
  • Generate file with code like this (this is simplified version) for each locale:
import axios from 'axios'

export default function() {
    return new Promise((resolve) => {
        axios
            .get('/api/translations?locale={locale}')
            .then(({ data }) => resolve(data.data))
            .catch((e) => {
                console.log(e)
                resolve({})
            })
    })
}

Server response

By default, your server response should look like this:

// GET /api/translations?locale=de

{
    "data": {
        "hello": "Hallo",
        "goodbye": "Auf Wiedersehen"
    }
}

// GET /api/translations?locale=en

{
    "data": {
        "hello": "Hallo", // no translation - fallback to default language
        "goodbye": "Goodbye"
    }
}

And you can use it in templates:

<template>
    <div>{{ $t('hello') }}</div>
</template>

Configuring server endpoint

Extend your basic config like this

// nuxt.config.js

export default {
    i18n: {
        vueI18n: {
            fallbackLocale: 'de' // this will force to make additional request for default locale
        }
    },

    awesIo: {
        nuxtI18n: {
            url: '/api/langs/{locale}', // insert {locale} for locale code interpolation
            dataPath: 'my.lang' // set to 'false' for data without "wrapper", 'data' by default
            // ... some other config
        }
    }
}

The response should look like this:

// GET /api/langs/de

{
    "my": {
        "lang": {
            "hello": "Hallo",
            "goodbye": "Auf Wiedersehen"
        }
    }
}

// GET /api/langs/en

{
    "my": {
        "lang": {
            // no translation - no key in response
            "goodbye": "Goodbye"
        }
    }
}

Note!

If you provide url with different domain, like this 'https://another-site.io/api/langs/{locale}', be shure, to send Access-Control-Allow-Origin header and support HEAD request method

Ensure to write proper commit message according to Git Commit convention

Keywords

translations

FAQs

Package last updated on 21 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.