npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@inlang/paraglide-astro

v0.3.0

Published

A fully type-safe i18n library specifically designed for partial hydration patterns like Astro's islands.

Downloads

5,474

Readme

Dead Simple i18n. Typesafe, Small Footprint, SEO-Friendly and with an IDE Integration.

Inlang-ecosystem compatibility badge

Gettting Started

npx @inlang/paraglide-js init
npm i @inlang/paraglide-astro

This will generate messages/{lang}.json files for each of your languages. That's where your translations live.

Register the Integration in astro.config.mjs:

import paraglide from "@inlang/paraglide-astro"

export default {
	// Use astro's i18n routing for deciding which language to use
	i18n: {
		locales: ["en", { code: "de", path: "deutsch" }],
		defaultLocale: "en",
	},

	integrations: [
		paraglide({
			// recommended settings
			project: "./project.inlang",
			outdir: "./src/paraglide", //where your files should be
		}),
	],
}

Passing the Language to the Client

To save bundle size the integration doesn't ship language detection code to the client. Instead, it will read the lang attribute on the <html> tag. Make sure it is set correctly.

//src/layouts/default.astro
---
import { languageTag } from "../paraglide/runtime";
---

<!doctype html>
<html lang={languageTag()} dir={Astro.locals.paraglide.dir}>
    <slot />
</html>

Usage

Adding & using messages

Messages live in messages/{lang}.json files. Add a message to get started

// messages.en.json
{
	"hello": "Hello {name}!"
}

You can edit which languages you support in project.inlang/settings.json.

{
	"languageTags": ["en", "de"],
	"sourceLanguageTag": "en"
}

Use messages in code by importing from the paraglide/messages.js file generated by the compiler.

---
import * as m from "../paraglide/messages.js";
---

<h1>{m.hello({ name: "Samuel" })}</h1>

Vite can tree-shake the messages with no extra work from you. Only messages that are used on an Island will be included in the client bundle.

Understanding Language Detection

Paraglide-Astro relies on astro:i18n's language detection. Place your page in a folder named for the language (or the path of the language) & all messages will be in that language.

src
├── pages
│   ├── en
│   │   ├── index.astro
│   │   └── about.astro
│   └── de
│       ├── index.astro
│       └── about.astro

If a page isn't in a language folder, it will use the default language.

src
├── pages
│   ├── index.astro // default language
│   ├── about.astro // default language
│   └── de
│       ├── index.astro // de
│       └── about.astro // de

You can configure which languages are available, and which is the default language in project.inlang/settings.json.

To save bundle size the integration doesn't ship language detection code to the client. Instead, it will read the lang attribute on the <html> tag. Make sure it is set correctly.

//src/layouts/default.astro
---
import { languageTag } from "$paraglide/runtime";
---

<!doctype html>
<html lang={languageTag()} dir={Astro.locals.paraglide.dir}>
    <slot />
</html>
---

You can also access the current language and text-direction via Astro.locals.paraglide.lang and Astro.locals.paraglide.dir respectively.

Linking between pages

Because pages in different languages often have different slugs there is no way to automatically generate links in all languages. You will need to define a custom function.

import type { AvailableLanguageTag } from "./paraglide/runtime.js"

type AbsolutePathname = `/${string}`

const pathnames : Record<AbsolutePathname, 
	Record<AvailableLanguageTag, AbsolutePathname>
> = {
	"/about": {
		en: "/about",
		de: "/de/ueber-uns",
	}
}

// src/linking.ts
export function localizePathname(
	pathname: AbsolutePathname, 
	locale: AvailableLanguageTag
) {
	if(pathnames[pathname]) {
		return pathnames[pathname][locale]
	}
	return pathname
}

Then use this function on your links

<a href={localizePathname("/about", languageTag())}>{m.about()}</a>

Adding Alternate Links

For SEO reasons, you should add alternate links to your page's head that point to all translations of the current page. Include the current page. Make sure these are full HREFs, including the protocol and origin, not just the path.

<head>
	<link rel="alternate" hreflang="en" href="https://acme.com/en/about" />
	<link rel="alternate" hreflang="de" href="https://acme.com/de/ueber-uns" />
</head>

Since only you know which pages correspond to each other this can't reliably be done automatically. Add these links manually.

Roadmap

  • Improved Server-Rendering support
  • Automatic Link-Map generation

Playground

Check out an example Astro project with Paraglide integration on StackBlitz