intlayer-angular
npx machina-cli add skill aymericzip/intlayer-skills/intlayer-angular --openclawIntlayer Angular Usage
Core Philosophy
Intlayer promotes Component-Level Content Declaration. Instead of a massive global translation file, content is declared in *.content.ts files adjacent to the Angular components that use them.
Workflow
To create a translated component, you need two files:
- Declaration: A content file (e.g.,
my-component.content.ts) defining the dictionary. - Implementation: An Angular component (e.g.,
my-component.component.ts) using theuseIntlayersignal.
1. Declare Content
Create a content file using t() for translations.
File: src/app/my-component/my-component.content.ts
import { t, type Dictionary } from "intlayer";
const content = {
// The 'key' must be unique and matches what you pass to useIntlayer()
key: "my-component",
content: {
text: t({
en: "Welcome",
fr: "Bienvenue",
es: "Hola",
}),
},
} satisfies Dictionary;
export default content;
Setup
Intlayer Provider
To use Intlayer in your Angular application, you need to add the provideIntlayer provider to your application configuration.
import { ApplicationConfig } from "@angular/core";
import { provideIntlayer } from "angular-intlayer";
export const appConfig: ApplicationConfig = {
providers: [provideIntlayer()],
};
useIntlayer Hook
In Angular, useIntlayer returns a Signal. You must call it as a function to access the value.
import { Component } from "@angular/core";
import { useIntlayer } from "angular-intlayer";
@Component({
selector: "app-my-component",
standalone: true,
template: `
<div>
<h1>
{{ content().text }}
</h1>
<img [src]="content().text.value" [alt]="content().text.value" />
</div>
`,
})
export class MyComponent {
content = useIntlayer("my-component");
}
Change language
To change the language, use the setLocale function from the useLocale hook.
import { Component } from "@angular/core";
import { useLocale } from "angular-intlayer";
import { Locales } from "intlayer";
@Component({
selector: "app-locale-switcher",
standalone: true,
template: `
<button (click)="setLocale(Locales.FRENCH)">
Change Language to French
</button>
`,
})
export class LocaleSwitcherComponent {
Locales = Locales;
private localeCtx = useLocale();
setLocale = this.localeCtx.setLocale;
}
Localized Link component
Ensure your application's navigation respects the current locale by using a localized link. You can create a component or use a helper.
import { Component, Input } from "@angular/core";
import { RouterModule } from "@angular/router";
import { useLocale } from "angular-intlayer";
import { getLocalizedUrl } from "intlayer";
@Component({
selector: "app-link",
standalone: true,
imports: [RouterModule],
template: `
<a [routerLink]="localizedHref()" [replaceUrl]="false">
<ng-content></ng-content>
</a>
`,
})
export class LinkComponent {
@Input() href: string = "";
private localeCtx = useLocale();
locale = this.localeCtx.locale;
localizedHref() {
return this.href.startsWith("http")
? this.href
: getLocalizedUrl(this.href, this.locale());
}
}
References
Source
git clone https://github.com/aymericzip/intlayer-skills/blob/main/skills/intlayer-angular/SKILL.mdView on GitHub Overview
This skill integrates Intlayer internationalization into Angular apps using component-level content declarations. It replaces global translation files by colocating dictionaries with their components, enabling precise translations and runtime locale switching.
How This Skill Works
Create a content file next to your Angular component that uses t() for translations and export a dictionary. Boot Intlayer with provideIntlayer() in your app configuration, then access translations in the component via the useIntlayer signal. Change languages at runtime with setLocale from useLocale.
When to Use It
- Setting up Angular i18n in a project
- Creating a translated Angular component
- Accessing translations with useIntlayer in a component
- Configuring Intlayer provider in app
- Switching language at runtime with setLocale
Quick Start
- Step 1: Create a content file (e.g., my-component.content.ts) with t() translations.
- Step 2: Add provideIntlayer() to your app configuration.
- Step 3: In your Angular component, call content = useIntlayer('my-component') and render content().text.
Best Practices
- Keep a content.ts file next to each component to maintain locality of translations
- Use t() to define per-language strings and ensure keys are unique and stable
- Ensure the dictionary 'key' exactly matches the value passed to useIntlayer()
- Render translations via the content() signal in templates and components
- Test across locales and ensure localized navigation uses getLocalizedUrl for links
Example Use Cases
- Translate a product card with en/fr/es dictionaries adjacent to its component
- Create a hero section with dynamic, component-scoped translations
- Build a localized Link component that preserves locale
- Add a header language switcher that calls setLocale
- Create a new standalone component using useIntlayer for translations