Localization and Internationalization (i18n) Guide

Welcome to the Finch Localization and Internationalization (i18n) Guide! This guide will walk you through the steps to localize and internationalize your Finch application. Whether you're a seasoned developer or just starting, Finch offers a robust set of tools to simplify server-side web app development.

i18n in Finch

Finch provides a built-in i18n system for localizing and internationalizing your application. The i18n system allows you to define multiple languages and translate your application's text into those languages.

How to use i18n in Finch?

It is very simple to use i18n in Finch. You just need to define your languages in the en.json or other language file. Here is an example of how to define languages in the fa.json file:

{
  "dir": "rtl",
  "example.params": "نام من {name} است، سن من {age} سال است",
  "example.path": "مسیر تست از example.path برای محلی‌سازی",
  "example.tstring": "تست اشیاء TString برای i18n",
  "logo": "Finch",
  "logo.title": "Finch"
}

i18n in templates

You can use i18n in your templates by using the {{ $t('logo.title') }} syntax. For example, if you want to translate the logo.title key, you can do it like this:

<h1>{{ $t('logo.title') }}</h1>

i18n in controllers

it is very simple to use i18n in your controllers. You can use the "string".tr method to translate your text. For example, if you want to translate the logo.title key, you can do it like this:

rq.renderString(text: 'logo.title'.tr);

parameters in i18n

You can use parameters in your i18n text. For example, if you want to translate the example.params key, you can do it like this:

rq.renderString(text: 'example.params'.tr.write({'name': 'Alexandre', 'age': 30}));

and in your template you can use it like this:

<p>{{ $t('example.params', {'name': 'Alexandre', 'age': 30}) }}</p>

array parameters in i18n

You can use array parameters in your i18n text. For example, if you want to translate the example.params key, you can do it like this:

rq.renderString(text: 'example.params'.tr.writeArr(['Alexandre', 30]));

and in your template you can use it like this:

{
    "example.params": "نام من {0} است، سن من {1} سال است",
}
<p>{{ $t('example.params', ['Alexandre', 30]) }}</p>