Giter Club home page Giter Club logo

next-intl-translation-bundler-example's Introduction

What is this?

This is a somewhat rudimentary/hacky example of how to do per entrypoint translation bundling/tree-shaking using next-intl.

The core logic of the example is in the TranslationsPlugin.js file.

Running

First, run the development server:

npm run build
npm run start

Open http://localhost:3000 with your browser to see the result. To see the language changing happening use the select dropdown at the bottom of the page to change the language before clicking a link. The supported languages are "en-US" and "fr"

How it works

We use the webpack JavascriptParser hooks to find evaluateExpression calls to useTranslations such as:

const t = useTranslations();

and tag the variable declarator t so we can parse out the keys from later uses of t like:

t("someKey");

For every module we track the module identifier and the translation keys used in that module in a translationsMap

Then for entrypoint (in a NextJS app that is every page) we identify all keys used in that entrypoint by iterating over it's modules, and emit a bundle for every language in the .next/i18n directory in a structure that mimics the page structure of the application.

Lastly at runtime in the getServerSideProps method of a page we identify which bundle to serve based on request path and request locale.

Author: Yash Vesikar

next-intl-translation-bundler-example's People

Contributors

yashvesikar avatar

Stargazers

 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.