Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@inlang/paraglide-js
Advanced tools
<!-- ![Paraglide JS header image](https://cdn.jsdelivr.net/gh/inlang/monorepo@latest/inlang/source-code/paraglide/paraglide-js/assets/paraglide-js-header.png) -->
Watch the pre-release demo of Paraglide JS
Attention: The following features are missing and will be added in the upcoming weeks:
Type in the following command into your terminal to get started immediately:
npx @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:
npx @inlang/paraglide-js@latest init
Having an adapter is only required if you want to use paraglide-js with a framework. If you don't use a framework, you can skip this step.
compile
script to your package.json
If you are using
@inlang/paraglide-js-adapter-vite
, you can skip this step.
You can customize the compile
script to your needs. For example, you can add a --watch
flag to watch for changes, if you have installed a watcher.
{
"scripts": {
"compile": "paraglide-js compile --project ./project.inlang",
"watch": "paraglide-js compile --project ./project.inlang --watch"
}
}
Running the compile
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.
// m is a namespace that contains all messages of your project
// a bundler like rollup or webpack only bundles
// the messages that are used
import * as m from "./paraglide/messages"
import { setLanguageTag } from "./paraglide/runtime"
// use a message
m.hello() // Hello world!
// message with parameters
m.loginHeader({ name: "Samuel" }) // Hello Samuel, please login to continue.
// change the language
setLanguageTag("de")
m.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.
import * as m from "./paraglide/messages"
const season = {
spring: m.spring,
summer: m.summer,
autumn: m.autumn,
winter: m.winter,
}
const msg = season["spring"]() // Hello spring!
Paraglide JS provides five exports in ./paraglide/runtime.js
:
Variable | Description |
---|---|
sourceLanguageTag | The source language tag of the project |
availableLanguageTags | All language tags of the current project |
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.
import { setLanguageTag } from "./paraglide/runtime"
import * as m from "./paraglide/messages"
setLanguageTag("de")
m.hello() // Hallo Welt!
setLanguageTag("en")
m.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.
You can react to a language change by calling onSetLanguageTag()
. This function is called whenever the language tag changes.
import { setLanguageTag, onSetLanguageTag } from "./paraglide/runtime"
import * as m from "./paraglide/messages"
onSetLanguageTag((newLanguageTag) => {
console.log(`The language changed to ${newLanguageTag}`)
})
setLanguageTag("de") // The language changed to de
setLanguageTag("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. You can do this by passing an options object to the message function as a second parameter.
import * as m from "./paraglide/messages"
const msg = m.hello({ name: "Samuel" }, { languageTag: "de" }) // Hallo Samuel!
We provide a few bundler plugins to make it easier to use paraglide-js 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:
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:
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 | (if required) 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
// messages/en.json
{
"hello": "Hello {name}!",
"loginButton": "Login"
}
Output
// src/paraglide/messages.js
/**
* @param {object} params
* @param {string} params.name
*/
function hello({ name }) {
return `Hello ${name}!`
}
function loginButton() {
return "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.
// src/paraglide/messages.js
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.
// source/index.js
import * as m from "./paraglide/messages"
console.log(m.hello({ name: "Samuel" }))
The bundler tree shakes (removes) loginButton
and loginHeader
and only includes hello
in the output.
// output/index.js
function hello(params) {
return `Hello ${params.name}!`
}
console.log(hello({ name: "Samuel" }))
View the source of ./paraglide/runtime.js
to find the latest runtime API and documentation.
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.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 changesimport { setLanguageTag, onSetLanguageTag } from "./paraglide/runtime"
import { isServer, request, render } from "@example/framework"
// On a server, the language tag needs to be resolved on a
// per-request basis. Hence, we need to pass a getter
// function () => string to setLanguageTag.
//
// Most frameworks offer a way to access the current
// request. In this example, we assume that the language tag
// is available in the request object.
if (isServer) {
setLanguageTag(() => request.languageTag)
}
// On a client, the language tag could be resolved from
// the document's html lang tag.
//
// In addition, we also want to trigger a side-effect
// to request the site if the language changes.
else {
setLanguageTag(() => document.documentElement.lang)
//! Make sure to call `onSetLanguageTag` after
//! the initial language tag has been set to
//! avoid an infinite loop.
// route to the page in the new language
onSetLanguageTag((newLanguageTag) => {
window.location.pathname = `/${newLanguageTag}${window.location.pathname}`
})
}
// render the app
render((page) => (
<html lang={request.languageTag}>
<body>{page}</body>
</html>
))
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
FAQs
[![Inlang-ecosystem compatibility badge](https://cdn.jsdelivr.net/gh/opral/monorepo@main/inlang/assets/md-badges/inlang.svg)](https://inlang.com)
The npm package @inlang/paraglide-js receives a total of 16,547 weekly downloads. As such, @inlang/paraglide-js popularity was classified as popular.
We found that @inlang/paraglide-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.