Git Product home page Git Product logo

i18next-fluent's Introduction

Introduction

Travis npm version David

This changes i18n format from i18next json to fluent Spec version 1.0.0

Getting started

Source can be loaded via npm or downloaded from this repo.

# npm package
$ npm install i18next-fluent

Wiring up:

import i18next from "i18next";
import Fluent from "i18next-fluent";

i18next.use(Fluent).init(i18nextOptions);
  • As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.
  • If you don't use a module loader it will be added to window.i18nextFluent

Samples

Options

{
  bindI18nextStore: true,
  fluentBundleOptions: { useIsolating: false }
}

Options can be passed in by setting options.i18nFormat in i18next.init:

import i18next from "i18next";
import Fluent from "i18next-fluent";

i18next.use(Fluent).init({
  i18nFormat: options
});

loading .ftl fluent flavored textfiles

You can use the i18next-fluent-backend to directly load fluent files in fluent syntax from the server.

more complete sample

import i18next from "i18next";
import Fluent from "i18next-fluent";

i18next.use(Fluent).init({
  lng: "en",
  resources: {
    en: {
      translation: {
        hello: "Hello { $name }."
      }
    }
  }
});

i18next.t("hello", { name: "fluent" }); // -> Hello fluent.

Gold Sponsors

i18next-fluent's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

i18next-fluent's Issues

Using i18next-fluent with already loaded ftl string without i18next-fluent-backend

I would like to use ftl translations for my microfronted app. I do not want to use i18next-fluent-backend since ftl string is already loaded differently as a string so, I do not like to make a request once more for part of the app. From the documentation, it seems that I can use only i18next-fluent, but it doesn't properly change tags numbering. It doesn't change tags numbering to follow i18next system
<1>Test1</1><2>Test2</2> => <0>Test1</0><1>Test2</1>

Is it possible to use this repo with an already loaded ftlString?

image

[zero] is translated as [other]

Hi guys, I'm implementing a translation system in a React web app using typescript, react-18next and i18next-fluent. My problem is that in .ftl files just [one] and [other] options are considered. When I try to create an exception for 0 by specifying it in the .ftl file using [zero] or [0], the translation is the same as [other].

This is my i18n.ts file:

import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next';
import Fluent from 'i18next-fluent';
import Backend from 'i18next-fluent-backend';

i18n
  .use(Fluent)
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: 'en-US',

    ns: ['translations'],
    defaultNS: 'translations',

    debug: true,

    interpolation: {
      escapeValue: false,
    },

    react: {
      wait: true,
    },
  });

export default i18n;

And this is one example from the .ftl files:

emails =  
  { $unreadEmails ->
    [zero] You have no unread emails.
    [one] You have one unread email.
    *[other] You have { $unreadEmails } unread emails.
}

App.tsx is rendered using withTranslation HOC:

const App = withTranslation('translations')(InitialApp);
export default App;

Is this a sort of bug or should I configure zero as an exception?
And how could I manage intervals? For example I want numbers between 2 and 10 managed as [few]. Thanks in advance

react-fluent example throws error when project is ran locally

๐Ÿ› Bug Report

react-fluent example does not run due to error thrown by js2ftl.

To Reproduce

  1. Clone the react-i18next repo
  2. cd example/v9.x.x/react-fluent
  3. npm ci
  4. npm run start
js2ftl.js:24 Uncaught TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at js2ftl (js2ftl.js:24:1)
    at BundleStore.createBundle (index.js:174:1)
    at BundleStore.createBundleFromI18next (index.js:182:1)
    at index.js:202:1
    at Array.forEach (<anonymous>)
    at index.js:201:1
    at Array.forEach (<anonymous>)
    at index.js:200:1
    at EventEmitter.js:53:1
    at Array.forEach (<anonymous>)
    at I18n.emit (EventEmitter.js:52:1)
    at i18next.js:203:1
    at done (i18next.js:308:1)
    at Object.callback (i18next.js:322:1)
    at BackendConnector.js:223:1
    at Array.forEach (<anonymous>)
    at Connector.loaded (BackendConnector.js:202:1)
    at BackendConnector.js:315:1
    at BackendConnector.js:256:1
    at index.js:133:1
    at XMLHttpRequest.x.onreadystatechange (ajax.js:92:1)

Your Environment

  • runtime version: node v16.13, npm v8.1
  • i18next version: v11.17.2
  • os: osx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.