i18n Plugin

The i18n plugin adds basic string translation to your rfw app. It lets you define translations per language and switch languages at runtime with reactive updates.

Features

  • Define translations for multiple languages.
  • Bind translations as reactive signals in components.
  • Switch language dynamically with SetLang.
  • Works in Go and provides JS helpers (t, setLang).

Usage

Register the plugin with your app:

import "github.com/rfwlab/rfw/v2/plugins/i18n"

core.RegisterPlugin(i18n.New(map[string]map[string]string{
    "en": {"hello": "Hello"},
    "it": {"hello": "Ciao"},
}))

Translate in Components

import (
    core "github.com/rfwlab/rfw/v2/core"
    "github.com/rfwlab/rfw/v2/composition"
    "github.com/rfwlab/rfw/v2/plugins/i18n"
)

func NewGreeter() *core.HTMLComponent {
    hello := i18n.Signal("hello")
    cmp := composition.Wrap(core.NewComponent("Greeter", tpl, nil))
    cmp.Prop("hello", hello)
    cmp.On("setIT", func() { i18n.SetLang("it") })
    return cmp.Unwrap()
}
<p>@signal:hello</p>
<button @on:click:setIT>IT</button>

When the button is clicked, the language switches to Italian and the text updates automatically.

API Reference

Function Description
i18n.Signal(key) Returns a reactive signal for the translation of key.
i18n.SetLang(lang) Sets the active language and refreshes all signals.

Notes

  • You must register handlers with cmp.On for @on: events to work.
  • JavaScript helpers are also available: t(key) and setLang(lang).
  • Only strings provided in the initial map are available for translation.