11

GitHub - fable-compiler/Fable.I18Next: Fable bindings and helpers for [i18next](...

 2 years ago
source link: https://github.com/fable-compiler/Fable.I18Next
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Fable.I18Next

Fable bindings and helpers for i18next. The bindings work with Fable and on .NET Core for ServerSide-Rendering.

Created by @atheck.

Installation

npm install --save i18next # or
yarn add i18next

paket add Fable.I18Next --project [yourproject]

Usage

Make sure your Fable project .fsproj has the FABLE_COMPILER property set:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <DefineConstants>FABLE_COMPILER</DefineConstants>
  </PropertyGroup>
  <ItemGroup>
    // ...
    <Compile Include="App.fs" />
  </ItemGroup>
</Project>

Create a translations.json file like the following:

{
    "de": {
        "translation": {
            "MyKey": "Das ist ein deutscher Text",
        },
    },
    "en": {
        "translation": {
        "MyKey": "This is a english text",
        }
    }
}

Hook in Fable.I18Next in your Elmish Program inside App.fs:

module App

open Fable.Core.JsInterop

// ...

let resources : obj = import "*" "./translations.json"

promise {
    do! I18n.Init(resources,"en")
    Program.run program
}
|> Promise.start

If you want to access the translation then just use:

open Fable.I18Next

I18n.Translate "MyKey"

If you want to switch the language then use the I18n.ChangeLanguage. If you use Elmish then you can put it into a Cmd:

open Fable.I18Next

let update msg model =
    match msg with
    // ...
    | ChangeLanguage newLanguage ->
        model, Cmd.OfPromise.either I18n.ChangeLanguage newLanguage LanguageChanged Error
    // ...

Usage on .NET Core

let resourceFileName =
    let app = System.Reflection.Assembly.GetExecutingAssembly().Location
    let startupPath = System.IO.Path.GetDirectoryName app
    Path.Combine(startupPath, "translations.json")

I18n.Init(resourceFileName, initialLang)

Please read the i18next docs for more sophisticated examples.

Release process

After installing dependencies with yarn install, run yarn run build publish to publish a new package


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK