In this guide you will lean how to add internationalised routing to your Remix App. We will use ParaglideJS to manage our messages and the Remix-paraglideJS for internationalised routing.
Paraglide is a great fit for the Remix App because it uses a compiler to generate tree-shakeable messages. That way your client bundle only includes the messages that are used in client components on any given page.
This guide assumes that you have a Remix project set up. If you don't, you can follow the Remix Getting Started Guide.
The recommended way to install ParaglideJS is via the cli. This will create any files that are required for Paraglide.js to work and install the required dependencies.
In your project root, run the following commands and follow the instructions.
npx @inlang/paraglide-js init
npm i --save remix-paraglidejs @inlang/paraglide-vite
This will have done a few things:
Created an inlang project in your project root
Added the required devDependencies to your package.json
In your vite.config.ts, import @inlang/paraglide-vite and apply it to your config.
You will need to pass it some config. The location of the Inlang Project & the output directory that messages should be compiled to. You should stick with the defaults unless you have a reason not to.
import { vitePlugin as remix } from"@remix-run/dev";
import { installGlobals } from"@remix-run/node";
import { defineConfig } from"vite";
import tsconfigPaths from"vite-tsconfig-paths";
/* --- INCLUDE THIS --- */import { paraglide } from"@inlang/paraglide-vite";
/* ----------------- */installGlobals();
exportdefaultdefineConfig({
plugins: [
remix(),
tsconfigPaths(),
/* --- INCLUDE THIS --- */paraglide({
project: "./project.inlang", //Path to your inlang projectoutdir: "./paraglide", //Where you want the generated files to be placed
}),
/* ----------------- */
],
});
This Plugin will make sure to automatically recompile messages whenever they change.
Let's create a messages folder in our project root. This is where we will store our messages. Then add an en.json file to it.
// messages/en.json{"$schema":"https://inlang.com/schema/inlang-message-format","title":"Remix web example"}
If you now run start your dev server you should see a new folder appear in your src folder called paraglide. This folder contains the compiled messages and any runtime code required by paraglide. Your messages live at ./src/paraglide/messages.js, you will be importing from there.
Let's use the title message on our homepage. Create a route ($lang)._index.tsx, import all messages from paraglide/messages.js and use the title message in the h1 tag.
import { Link } from"@remix-run/react";
import * as m from"<YOUR_PARAGLIDE_DIR>/messages";
exportdefaultfunctionIndex() {
return (
<div><h1>{m.title()}</h1></div>
);
}
You should now see the message "Remix web example" on your homepage!
Note: If you are using Visual Studio Code, you should install Sherlock (VS Code extension). It will give you inline previews of messages and allow you to edit them right in your source code.
You should now have a fully functional multilingual Remix app using ParaglideJS.
You can check out the full source code of this example here.
If you want to learn more about ParaglideJS, check out the ParaglideJS Documentation. If you need help or have some ideas, feel free to reach out to us on Discord or open a Discussion on GitHub.