New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@inlang/paraglide-js

Package Overview
Dependencies
Maintainers
2
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@inlang/paraglide-js - npm Package Compare versions

Comparing version 1.2.1 to 1.2.2

default/index.d.ts

31

package.json
{
"name": "@inlang/paraglide-js",
"type": "module",
"version": "1.2.1",
"version": "1.2.2",
"license": "Apache-2.0",

@@ -10,2 +10,8 @@ "publishConfig": {

"author": "inlang <hello@inlang.com> (https://inlang.com/)",
"homepage": "https://github.com/opral/monorepo/tree/main/inlang/source-code/paraglide/paraglide-js",
"repository": {
"type": "git",
"url": "https://github.com/opral/monorepo.git",
"directory": "inlang/source-code/paraglide/paraglide-js"
},
"keywords": [

@@ -25,3 +31,7 @@ "paraglide",

"nextjs",
"next i18n"
"next i18n",
"astro",
"astro i18n",
"solid",
"solidstart"
],

@@ -33,3 +43,4 @@ "bin": {

"./dist",
"./bin"
"./bin",
"./default"
],

@@ -51,3 +62,3 @@ "dependencies": {

"@types/minimist": "1.2.3",
"@types/node": "^20.11.2",
"@types/node": "^20.11.4",
"@vitest/coverage-v8": "0.34.3",

@@ -61,9 +72,13 @@ "cross-env": "^7.0.3",

"@inlang/env-variables": "0.2.0",
"@inlang/sdk": "0.23.0",
"@inlang/telemetry": "0.3.4",
"@lix-js/client": "0.5.0",
"@lix-js/fs": "0.5.0",
"@inlang/telemetry": "0.3.9",
"@lix-js/fs": "0.6.0",
"@lix-js/client": "0.8.0",
"@inlang/sdk": "0.26.1",
"@inlang/plugin-message-format": "2.0.0"
},
"exports": {
".": {
"import": "./default/index.js",
"types": "./default/index.d.ts"
},
"./internal": {

@@ -70,0 +85,0 @@ "import": "./dist/index.js",

@@ -0,13 +1,9 @@

[<img src="https://cdn.loom.com/sessions/thumbnails/a8365ec4fa2c4f6bbbf4370cf22dd7f6-with-play.gif" width="100%" /> Watch the demo of Paraglide JS](https://www.youtube.com/watch?v=-YES3CCAG90)
<!-- ![Paraglide JS header image](https://cdn.jsdelivr.net/gh/opral/monorepo@latest/inlang/source-code/paraglide/paraglide-js/assets/paraglide-js-header.png) -->
[<img src="https://cdn.loom.com/sessions/thumbnails/a8365ec4fa2c4f6bbbf4370cf22dd7f6-with-play.gif" width="100%" /> Watch the pre-release demo of Paraglide JS](https://www.youtube.com/watch?v=-YES3CCAG90)
# Simple, adaptable, and tiny i18n library for JS
Attention: The following features are missing and will be added in the upcoming weeks:
Get started instantly with the following command:
- [ ] Support for pluralization
# The i18n library that you can set up within 3 lines of code
Type in the following command into your terminal to get started immediately:
```bash

@@ -21,3 +17,3 @@ npx @inlang/paraglide-js@latest init

<doc-feature title="No unused translations" image="https://cdn.jsdelivr.net/gh/opral/monorepo@latest/inlang/source-code/paraglide/paraglide-js/assets/unused-translations.png"></doc-feature>
<doc-feature title="Reduced payload" image="https://cdn.jsdelivr.net/gh/opral/monorepo@latest/inlang/source-code/paraglide/paraglide-js/assets/reduced-payload.png"></doc-feature>
<doc-feature title="Minimal payload" image="https://cdn.jsdelivr.net/gh/opral/monorepo@latest/inlang/source-code/paraglide/paraglide-js/assets/reduced-payload.png"></doc-feature>
<doc-feature title="Typesafety" image="https://cdn.jsdelivr.net/gh/opral/monorepo@latest/inlang/source-code/paraglide/paraglide-js/assets/typesafe.png"></doc-feature>

@@ -33,3 +29,2 @@ </doc-features>

# Getting started

@@ -45,9 +40,15 @@

### 2. Select an adapter (if required)
This will:
Having an adapter is recommended but not required if you want to use paraglide-js with a framework. If you don't use a framework, you can skip this step.
1. Install the necessary dependencies
2. Call the Paraglide compiler in your `build` script
3. Set up any necessary configuration files
### 2. Set up an adapter (optional)
Adapters 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.
<doc-links>
<doc-link title="Adapter for Svelte" icon="simple-icons:svelte" href="https://github.com/opral/monorepo/tree/main/inlang/source-code/paraglide/paraglide-js-adapter-svelte/example" description="Go to GitHub example"></doc-link>
<doc-link title="Adapter for SolidJS" icon="tabler:brand-solidjs" href="https://inlang.com/m/n860p17j/library-inlang-paraglideJsAdapterSolidStart" description="Go to Adapter"></doc-link>
<doc-link title="Adapter for SvelteKit" icon="simple-icons:svelte" href="https://inlang.com/m/dxnzrydw/library-inlang-paraglideJsAdapterSvelteKit" description="Go to Library"></doc-link>
<doc-link title="Adapter for SolidJS" icon="tabler:brand-solidjs" href="https://inlang.com/m/n860p17j/library-inlang-paraglideJsAdapterSolidStart" description="Go to Library"></doc-link>
<doc-link title="Adapter for NextJS" icon="tabler:brand-nextjs" href="https://github.com/opral/monorepo/tree/main/inlang/source-code/paraglide/paraglide-js-adapter-next" description="Go to GitHub example"></doc-link>

@@ -59,20 +60,5 @@ <doc-link title="Adapter for Vite" icon="tabler:brand-vite" href="https://github.com/opral/monorepo/tree/main/inlang/source-code/paraglide/paraglide-js-adapter-vite" description="Go to GitHub"></doc-link>

### 3. Add the `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.
```json
{
"scripts": {
"compile": "paraglide-js compile --project ./project.inlang",
"watch": "paraglide-js compile --project ./project.inlang --watch"
}
}
```
# Usage
Running the `compile` script will generate a `src/paraglide` folder. This folder contains all the code that you need to use paraglide-js.
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.

@@ -90,4 +76,4 @@

// the messages that are used
import * as m from "./paraglide/messages"
import { setLanguageTag } from "./paraglide/runtime"
import * as m from "./paraglide/messages.js"
import { setLanguageTag } from "./paraglide/runtime.js"

@@ -109,3 +95,3 @@ // use a message

```js
import * as m from "./paraglide/messages"
import * as m from "./paraglide/messages.js"

@@ -122,20 +108,21 @@ const season = {

Paraglide JS provides five exports in `./paraglide/runtime.js`:
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 |
| `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 |
| 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 |
## Setting the language
You can set the current [language tag](/m/8y8sxj09/library-inlang-languageTag) by calling `setLanguageTag()`. Any subsequent calls to either `languageTag()` or a message function will return the new language tag.
You can set the current [language tag](https://www.inlang.com/m/8y8sxj09/library-inlang-languageTag) by calling `setLanguageTag()`. Any subsequent calls to either `languageTag()` or a message function will return the new language tag.
```js
import { setLanguageTag } from "./paraglide/runtime"
import * as m from "./paraglide/messages"
import { setLanguageTag } from "./paraglide/runtime.js"
import * as m from "./paraglide/messages.js"

@@ -149,11 +136,22 @@ setLanguageTag("de")

The [language tag](/m/8y8sxj09/library-inlang-languageTag) 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.
The [language tag](https://www.inlang.com/m/8y8sxj09/library-inlang-languageTag) 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.
```json
// project.inlang/settings.json
{
"languageTags": ["en", "de"]
}
```
## Reacting to a language change
You can react to a language change by calling `onSetLanguageTag()`. This function is called whenever the [language tag](/m/8y8sxj09/library-inlang-languageTag) changes.
You can react to a language change by registering a callback using `onSetLanguageTag()`. This function is called whenever the [language tag](https://www.inlang.com/m/8y8sxj09/library-inlang-languageTag) changes.
```js
import { setLanguageTag, onSetLanguageTag } from "./paraglide/runtime"
import * as m from "./paraglide/messages"
import { setLanguageTag, onSetLanguageTag } from "./paraglide/runtime.js"
import * as m from "./paraglide/messages.js"

@@ -177,7 +175,7 @@ onSetLanguageTag((newLanguageTag) => {

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
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.
```js
import * as m from "./paraglide/messages"
import * as m from "./paraglide/messages.js"
const msg = m.hello({ name: "Samuel" }, { languageTag: "de" }) // Hallo Samuel!

@@ -188,3 +186,3 @@ ```

We provide a few bundler plugins to make it easier to use paraglide-js with a bundler. If you
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.

@@ -200,6 +198,7 @@

You can find many examples for how to use paraglide on codesandbox:
You can find many examples for how to use paraglide on codesandbox, or in [our GitHub repository](https://github.com/opral/monorepo/tree/main/inlang/source-code/paraglide).
<doc-links>
<doc-link title="Svelte + Paraglide JS" icon="lucide:codesandbox" href="https://dub.sh/paraglide-playground-svelte" description="Play around with Svelte and Paraglide JS"></doc-link>
<doc-link title="Svelte + Paraglide JS" icon="lucide:codesandbox" href="https://stackblitz.com/~/github.com/LorisSigrist/paraglide-sveltekit-example" description="Play around with Svelte and Paraglide JS"></doc-link>
<doc-link title="Astro + Paraglide JS" icon="lucide:codesandbox" href="https://stackblitz.com/~/github.com/LorisSigrist/paraglide-astro-example" description="Play around with Astro and Paraglide JS"></doc-link>
</doc-links>

@@ -211,3 +210,3 @@

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.
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.

@@ -218,8 +217,8 @@ ![paraglide JS architecture](https://cdn.jsdelivr.net/gh/opral/monorepo@latest/inlang/source-code/paraglide/paraglide-js/assets/architecture.svg)

| 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](/m/8y8sxj09/library-inlang-languageTag) of the current user |
| **Adapter** | (if required) An adapter that adjusts the runtime for different frameworks |
| 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](https://www.inlang.com/m/8y8sxj09/library-inlang-languageTag) of the current user |
| **Adapter** | (optional) An adapter that adjusts the runtime for different frameworks |

@@ -245,3 +244,3 @@ ## Compiler

```js
// src/paraglide/messages.js
// src/paraglide/messages/en.js

@@ -252,9 +251,6 @@ /**

*/
function hello({ name }) {
return `Hello ${name}!`
}
export const hello = (params) => `Hello ${params.name}!`
function loginButton() {
return "Login"
}
/** ... */
export const loginButton = () => "Login"
```

@@ -275,13 +271,10 @@

export function hello(params) {
return `Hello ${params.name}!`
}
/** ... */
export const hello = (params) => `Hello ${params.name}!`
export function loginButton() {
return "Login"
}
/** ... */
export const loginButton = () => "Login"
export function loginHeader(params) {
return `Hello ${params.name}, please login to continue.`
}
/** ... */
export const loginHeader = (params) => `Hello ${params.name}, please login to continue.`
```

@@ -292,6 +285,5 @@

```js
// source/index.js
// src/my-code.js
import * as m from "../paraglide/messages.js"
import * as m from "./paraglide/messages"
console.log(m.hello({ name: "Samuel" }))

@@ -303,33 +295,21 @@ ```

```js
// output/index.js
// dist/my-code.js
const hello = (params) => `Hello ${params.name}!`
function hello(params) {
return `Hello ${params.name}!`
}
console.log(hello({ name: "Samuel" }))
```
## Runtime
# Writing an Adapter
View the source of `./paraglide/runtime.js` to find the latest runtime API and documentation.
Paraglide can be adapted to any framework or environment by calling `setLanguageTag()` and `onSetLanguageTag()`.
## Adapter
The following example adapts Paraglide-JS to a fictitious metaframework like NextJS, SolidStart, SvelteKit, or Nuxt.
Paraglide-JS can be adapted to any framework or environment by calling `setLanguageTag()` and `onSetLanguageTag()`.
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:
1. `setLanguageTag()` can be used to set a getter function for the [language tag](/m/8y8sxj09/library-inlang-languageTag). 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.
1. `setLanguageTag()` can be used to set a getter function for the [language tag](https://www.inlang.com/m/8y8sxj09/library-inlang-languageTag). 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.
2. `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:
1. `setLanguageTag()`: to set the [language tag](/m/8y8sxj09/library-inlang-languageTag)
2. `onSetLanguageTag()`: to trigger a side-effect when the language changes
```tsx
import { setLanguageTag, onSetLanguageTag } from "./paraglide/runtime"
import { setLanguageTag, onSetLanguageTag } from "../paraglide/runtime.js"
import { isServer, request, render } from "@example/framework"

@@ -379,2 +359,3 @@

<doc-comments>
<doc-comment text="Just tried Paraglide JS from @inlangHQ. This is how i18n should be done! Totally new level of DX for both implementation and managing translations! Superb support for SvelteKit as well ⭐" author="Patrik Engborg" icon="mdi:twitter" data-source="https://twitter.com/patrikengborg/status/1747260930873053674"></doc-comment>
<doc-comment text="The lib is great guys!" author="ktarmyshov" icon="mdi:github"></doc-comment>

@@ -386,2 +367,10 @@ <doc-comment text="Thank you for that huge work you have done and still doing!" author="ZerdoX-x" icon="mdi:github"></doc-comment>

# Roadmap
Of course, we're not done yet! We plan on adding the following features to Paraglide JS in the upcoming weeks:
- [ ] Pluralization ([Join the Discussion](https://github.com/opral/monorepo/discussions/2025))
- [ ] Formatting of numbers and dates ([Join the Discussion](https://github.com/opral/monorepo/discussions/992))
- [ ] Markup Placeholders ([Join the Discussion](https://github.com/opral/monorepo/discussions/913))
# Talks

@@ -393,1 +382,5 @@

- [Svelte London January 2024](https://www.youtube.com/watch?v=eswNQiq4T2w&t=646s)
# Pricing
<doc-dev-tool-pricing></doc-dev-tool-pricing>

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc