Gathering detailed insights and metrics for @inlang/paraglide-js
Gathering detailed insights and metrics for @inlang/paraglide-js
Gathering detailed insights and metrics for @inlang/paraglide-js
Gathering detailed insights and metrics for @inlang/paraglide-js
@inlang/paraglide-js-adapter-sveltekit
![Dead Simple i18n. Typesafe, Small Footprint, SEO-Friendly and, with an IDE Integration.](https://cdn.jsdelivr.net/gh/opral/monorepo@latest/inlang/source-code/paraglide/paraglide-js-adapter-sveltekit/assets/header.png)
@inlang/paraglide-vite
Vite plugin for running the Paraglide i18n compiler
@inlang/paraglide-unplugin
Unplugin Plugin for runnning the Paraglide compiler.
@inlang/paraglide-sveltekit
[![Inlang-ecosystem compatibility badge](https://cdn.jsdelivr.net/gh/opral/monorepo@main/inlang/assets/md-badges/inlang.svg)](https://inlang.com)
npm install @inlang/paraglide-js
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
54 Stars
2 Commits
1 Forks
1 Watching
1 Branches
7 Contributors
Updated on 27 Nov 2024
Cumulative downloads
Total Downloads
Last day
-21.1%
3,309
Compared to previous day
Last week
-4.7%
21,286
Compared to previous week
Last month
-5.3%
89,974
Compared to previous month
Last year
28,057.6%
640,585
Compared to previous year
20
The source code is in our monorepo/inlang/source-code/paraglide
Watch the demo of Paraglide JS
Get started instantly with the following command:
1npx @inlang/paraglide-js@latest init
Treeshaking gives us superpowers. With it, each page of your app only loads the messages that it actually uses. Incremental loading like this would usually take hours of manual tweaking to get right. With Paraglide-JS you get it for free. Say goodbye to huge bundles.
You can initialize paraglide-js by running the following command in your terminal:
1npx @inlang/paraglide-js@latest init
This will:
build
scriptAdapters are framework specific integrations for Paraglide. If you are using a framework, using an adapter is recommended (but not required). If you don't use a framework, you can skip this step.
Running your build
script will generate a src/paraglide
folder. This folder contains all the code that you need to use paraglide-js.
Throughout this guide, you will see imports from ./paraglide/*
. These are all to this folder.
Tip: If you are using a bundler, you can set up an alias to
./src/paraglide
to make the imports shorter. We recommend$paraglide/*
The compiled messages are placed in ./paraglide/messages.js
. You can import them all with import * as m from "./paraglide/messages"
. Don't worry, your bundler will only bundle the messages that you actually use.
1// m is a namespace that contains all messages of your project
2// a bundler like rollup or webpack only bundles
3// the messages that are used
4import * as m from "./paraglide/messages.js"
5import { setLanguageTag } from "./paraglide/runtime.js"
6
7// use a message
8m.hello() // Hello world!
9
10// message with parameters
11m.loginHeader({ name: "Samuel" }) // Hello Samuel, please login to continue.
12
13// change the language
14setLanguageTag("de")
15
16m.loginHeader({ name: "Samuel" }) // Hallo Samuel, bitte melde dich an, um fortzufahren.
If you want to dynamically choose between a set of messages, you can create a record of messages and index into it. Note that this will not be tree-shaken by your bundler.
1import * as m from "./paraglide/messages.js" 2 3const season = { 4 spring: m.spring, 5 summer: m.summer, 6 autumn: m.autumn, 7 winter: m.winter, 8} 9 10const msg = season["spring"]() // Hello spring!
Paraglide JS provides several exports from ./paraglide/runtime.js
:
Variable | Description |
---|---|
sourceLanguageTag | The source language tag of the project |
availableLanguageTags | All language tags of the current project |
type AvailableLanguageTag | An Type representing a valid language tag |
languageTag() | Returns the language tag of the current user |
setLanguageTag() | Sets the language tag of the current user |
onSetLanguageTag() | Registers a listener that is called whenever the language tag changes |
isAvailableLanguageTag() | Checks if a value is a valid language tag |
You can set the current language tag by calling setLanguageTag()
. Any subsequent calls to either languageTag()
or a message function will return the new language tag.
1import { setLanguageTag } from "./paraglide/runtime.js" 2import * as m from "./paraglide/messages.js" 3 4setLanguageTag("de") 5m.hello() // Hallo Welt! 6 7setLanguageTag("en") 8m.hello() // Hello world!
The language tag is global, so you need to be careful with it on the server to make sure multiple requests don't interfere with each other. That's why we recommend using an adapter for your framework. Adapters integrate with the framework's lifecycle and ensure that the language tag is managed correctly.
## Adding Languages
You can define which languages you want to support in ./project.inlang/settings.json
. Just edit the languageTags
array.
1// project.inlang/settings.json 2{ 3 "languageTags": ["en", "de"] 4}
You can react to a language change by registering a callback using onSetLanguageTag()
. This function is called whenever the language tag changes.
1import { setLanguageTag, onSetLanguageTag } from "./paraglide/runtime.js" 2import * as m from "./paraglide/messages.js" 3 4onSetLanguageTag((newLanguageTag) => { 5 console.log(`The language changed to ${newLanguageTag}`) 6}) 7 8setLanguageTag("de") // The language changed to de 9setLanguageTag("en") // The language changed to en
There are a few things to know about onSetLanguageTag()
:
The main use case for onSetLanguageTag()
is to trigger a rerender of your app's UI when the language changes. Again, if you are using an adapter this is handled for you.
It's common that you need to force a message to be in a certain language, especially on the server and during tests. You can do this by passing an options object to the message function as a second parameter.
1import * as m from "./paraglide/messages.js" 2const msg = m.hello({ name: "Samuel" }, { languageTag: "de" }) // Hallo Samuel!
We provide bundler plugins to make it easier to use Paraglide with a bundler. If you are using one of these bundlers, we recommed using the corresponding plugin.
These plugins make sure to rerun the compile
script whenever you build your project. That way you don't need to modify your build script in package.json
. If you are using a bundler with a dev-server, like Vite, the plugins also make sure to rerun the compile
script whenever your messages change.
You can find many examples for how to use paraglide on codesandbox, or in our GitHub repository.
Inlang Paraglide JS leverages a compiler to emit vanilla JavaScript functions.
The emitted functions are referred to as "message functions". By emitting message functions, inlang Paraglide JS eliminates a whole class of edge cases while also being simpler, faster, and more reliable than other i18n libraries. The compiled runtime contains less than 50 LOC (lines of code) and is less than 300 bytes minified & gzipped.
Inlang Paraglide-JS consists of four main parts:
Part | Description |
---|---|
Compiler | Compiles messages into tree-shakable message functions |
Messages | The compiled tree-shakable message functions |
Runtime | A runtime that resolves the language tag of the current user |
Adapter | (optional) An adapter that adjusts the runtime for different frameworks |
The compiler loads an inlang project and compiles the messages into tree-shakable and typesafe message functions.
Input
1// messages/en.json 2{ 3 "hello": "Hello {name}!", 4 "loginButton": "Login" 5}
Output
1// src/paraglide/messages/en.js 2 3/** 4 * @param {object} params 5 * @param {string} params.name 6 */ 7export const hello = (params) => `Hello ${params.name}!` 8 9/** ... */ 10export const loginButton = () => "Login"
The compiled messages are importable as a namespace import (import * as m
).
The namespace import ensures that bundlers like Rollup, Webpack, or Turbopack can tree-shake the messages that are not used.
Three compiled message functions exist in an example project.
1// src/paraglide/messages.js 2 3/** ... */ 4export const hello = (params) => `Hello ${params.name}!` 5 6/** ... */ 7export const loginButton = () => "Login" 8 9/** ... */ 10export const loginHeader = (params) => `Hello ${params.name}, please login to continue.`
Only the message hello
is used in the source code.
1// src/my-code.js 2import * as m from "../paraglide/messages.js" 3 4console.log(m.hello({ name: "Samuel" }))
The bundler tree shakes (removes) loginButton
and loginHeader
and only includes hello
in the output.
1// dist/my-code.js 2const hello = (params) => `Hello ${params.name}!` 3 4console.log(hello({ name: "Samuel" }))
Paraglide can be adapted to any framework or environment by calling setLanguageTag()
and onSetLanguageTag()
.
The following example adapts Paraglide-JS to a fictitious metaframework like NextJS, SolidStart, SvelteKit, or Nuxt.
The goal is to provide a high-level understanding of how to adapt Paraglide to a framework. Besides this example, we recommend viewing the source-code of available adapters. In general, only two functions need to be called to adapt Paraglide to a framework:
setLanguageTag()
can be used to set a getter function for the language tag. The getter function can be used to resolve server-side language tags or to resolve the language tag from a global state management library like Redux or Vuex.onSetLanguageTag()
can be used to trigger side-effects such as updating the UI, or requesting the site in the new language from the server.1import { setLanguageTag, onSetLanguageTag } from "../paraglide/runtime.js" 2import { isServer, request, render } from "@example/framework" 3 4// On a server, the language tag needs to be resolved on a 5// per-request basis. Hence, we need to pass a getter 6// function () => string to setLanguageTag. 7// 8// Most frameworks offer a way to access the current 9// request. In this example, we assume that the language tag 10// is available in the request object. 11if (isServer) { 12 setLanguageTag(() => request.languageTag) 13} 14// On a client, the language tag could be resolved from 15// the document's html lang tag. 16// 17// In addition, we also want to trigger a side-effect 18// to request the site if the language changes. 19else { 20 setLanguageTag(() => document.documentElement.lang) 21 22 //! Make sure to call `onSetLanguageTag` after 23 //! the initial language tag has been set to 24 //! avoid an infinite loop. 25 26 // route to the page in the new language 27 onSetLanguageTag((newLanguageTag) => { 28 window.location.pathname = `/${newLanguageTag}${window.location.pathname}` 29 }) 30} 31 32// render the app 33render((page) => ( 34 <html lang={request.languageTag}> 35 <body>{page}</body> 36 </html> 37))
We are grateful for all the support we get from the community. Here are just a few of the comments we've received over the last few weeks. Of course we are open to and value criticism as well. If you have any feedback, please let us know directly on GitHub
Of course, we're not done yet! We plan on adding the following features to Paraglide JS in the upcoming weeks:
Paraglide JS is part of the inlang ecosystem, so it integrates nicely with all the other inlang compatible tools. If you are working with translators and/or designers you will find the following tools useful:
No vulnerabilities found.
No security vulnerabilities found.