Coder Social home page Coder Social logo

simplelocalize / simplelocalize-next-translate Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 2.59 MB

Connect next-translate plugin for i18n with SimpleLocalize ๐ŸŒ

Home Page: https://simplelocalize.io/docs/integrations/next-translate/

JavaScript 2.08% TypeScript 36.67% CSS 61.25%
react react-i18n i18n next-translate nextjs nextjs-example localization static-site

simplelocalize-next-translate's Introduction

next-i18next and SimpleLocalize example

This is a Next.js project bootstrapped with create-next-app.

Translations location

Translations are placed in /locales/{lang}/{ns}.json

  • {ns} - namespace, allows you to split translation keys into multiple files
  • {lang} - language

In this example there are two namespaces: common and home and 4 locales: en, es, fr, pl.

.
โ”œโ”€โ”€ en
โ”‚   โ”œโ”€โ”€ common.json
โ”‚   โ””โ”€โ”€ home.json
โ”œโ”€โ”€ es
โ”‚   โ”œโ”€โ”€ common.json
โ”‚   โ””โ”€โ”€ home.json
โ”œโ”€โ”€ pl
โ”‚   โ”œโ”€โ”€ common.json
โ”‚   โ””โ”€โ”€ home.json
โ””โ”€โ”€ fr
    โ”œโ”€โ”€ common.json
    โ””โ”€โ”€ home.json

next-translate configuration

Install next-translate for NextJS

npm install --save next-translate

Create a configuration file in project root.

๐Ÿ“ฆ file: ./i18n.json

{
  "locales": ["en", "es", "fr", "pl"],
  "defaultLocale": "en",
  "pages": {
    "*": ["common"],
    "/": ["home"]
  }
}

NextJS + i18n configuration

Import i18next configuration file into next.config.js

// ๐Ÿ“ฆ file: ./next.config.js
const nextTranslate = require('next-translate')

module.exports = nextTranslate({
  webpack: (config, { isServer, webpack }) => {
    return config;
  }
})

SimpleLocalize configuration

๐Ÿ’ฟ Install SimpleLocalize CLI

curl -s https://get.simplelocalize.io/2.0/install | bash

๐Ÿงท Create configuration file

# ๐Ÿ“ฆ file: ./simplelocalize.yml
apiKey: YOUR_PROJECT_API_KEY
downloadFormat: single-language-json
downloadPath: ./locales/{lang}/{ns}.json

uploadFormat: single-language-json
uploadPath: ./locales/{lang}/{ns}.json

โคต๏ธ Download translations to ./locales directory

simplelocalize download

โคด๏ธ Upload translations from ./locales directory

simplelocalize upload

Usage

Example usage can be found in pages/index.tsx.

import useTranslation from 'next-translate/useTranslation'

//translations from common.json
const { t } = useTranslation('common')
console.log(t('LEARN_MORE')) // output: Learn more

//translations from home.json
const {t: homeT} = useTranslation('home');
console.log(homeT('HELLO_WORLD')) // output: Hello world

Try out this demo

First, run the development server:

npm run dev

Open http://localhost:3000 with your browser to see the result.

You can start editing the page by modifying pages/index.tsx. The page auto-updates as you edit the file.

simplelocalize-next-translate's People

Contributors

jpomykala avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.