@inlang/paraglide-js
TODO: adapter gallery
Usage
Messages are imported as a namespace and can be used as follows:
import * as m from "@inlang/paraglide-js/messages"
import { setLanguageTag } from "@inlang/paraglide-js"
m.hello()
m.loginHeader({ name: "Samuel" })
setLanguageTag("de")
m.loginHeader({ name: "Samuel" })
Paraglide JS exports four runtime 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
Getting started
Available Adapters
Standalone
- Add paraglide as a dependency:
npm install @inlang/paraglide-js
- Add the compiler to your build script:
{
"scripts": {
+ "build": "paraglide-js compile --namespace <your project name>"
}
}
Architecture
Inlang Paraglide JS leverages a compiler to emit a use-case optimized i18n library.
By leveraging a compiler, 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 adjust the runtime for different frameworks
COMPILER
The compiler loads an inlang project and compiles the messages into tree-shakable and type safe 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 exists in an examplary 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 "@inlang/paraglide-js/<namespace>/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 @inlang/paraglide-js/<namespace>
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 request the site in the new language from the server.
Writing an Adapter
The following example adapts Paraglide-JS to a fictious 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 to view 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 "@inlang/paraglide-js/<namespace>"
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>
)