TODO: adapter gallery
Getting started
- Run the following command in your terminal:
npx @inlang/paraglide-js@latest init
- (If required) select an adapter.
Available Adapters
Usage
Messages are imported as a namespace and can be used as follows:
import * as m from "./paraglide-js/messages"
import { setLanguageTag } from "./paraglide-js/runtime"
m.hello()
m.loginHeader({ name: "Samuel" })
setLanguageTag("de")
m.loginHeader({ name: "Samuel" })
Paraglide JS exports four variables and functions via "@inlang/paraglide-js":
sourceLanguageTag
: the source language tag of the projectlanguageTags
: all language tags of the current projectlanguageTag()
: returns the language tag of the current usersetLanguageTag()
: sets the language tag of the current user
Architecture
Inlang Paraglide JS leverages a compiler to emit vanilla JavaScript functions.
The emitted functions are often referred to as "message functions". By emitting message functions, inlang Paraglide JS eliminates a 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 1kb gzipped.
Inlang Paraglide-JS consists of four main parts:
COMPILER
: compiles messages into tree-shakable message functionsMESSAGES
: the compiled tree-shakable message functionsRUNTIME
: a runtime that resolves the language tag of the current userADAPTER
: (if required) an adapter that adjusts the runtime for different frameworks
COMPILER
The compiler loads an inlang project and compiles the messages into tree-shakable and typesafe message functions.
Example
Input
hello: "Hello {name}!"
loginButton: "Login"
Output
function hello({ name }) {
return `Hello ${name}!`
}
function loginButton() {
return "Login"
}
MESSAGES
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.
Example
Three compiled message functions exist in an example project.
export function hello(params) {
return `Hello ${params.name}!`
}
export function loginButton() {
return "Login"
}
export function loginHeader(params) {
return `Hello ${params.name}, please login to continue.`
}
Only the message hello
is used in the source code.
import * as m from "./praglide-js/messages"
console.log(m.hello({ name: "Samuel" }))
The bundler tree shakes (removes) loginButton
and loginHeader
and only includes hello
in the output.
function hello(params) {
return `Hello ${params.name}!`
}
console.log(hello({ name: "Samuel"}))
RUNTIME
View the source of your imports from ./paraglide-js/
to find the latest runtime API and documentation.
ADAPTER
Paraglide-JS can be adapted to any framework or environment by calling setLanguageTag()
and onSetLanguageTag()
.
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.
Writing an Adapter
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-JS 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-JS to a framework:
setLanguageTag()
: to set the language tagonSetLanguageTag()
: to trigger a side-effect when the language changes
import { setLanguageTag, onSetLanguageTag } from "./paraglide-js/runtime"
import { isServer, request, render } from "@example/framework"
if (isServer){
setLanguageTag(() => request.languageTag)
}
else {
setLanguageTag(() => document.documentElement.lang)
onSetLanguageTag((newLanguageTag) => {
window.location.pathname = `/${newLanguageTag}${window.location.pathname}`
})
}
render((page) =>
<html lang={request.languageTag}>
<body>
{page}
</body>
</html>
)