1.3.2 • Published 16 days ago

@ai4bharat/indic-transliterate v1.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
16 days ago

Transliteration component for React with support for 21 Indic languages. Uses API from AI4Bharat IndicXlit.

NPM

Table of contents

1. About

This is a frontend library to enable your users to type in many different languages of South Asia, and can be integrated into any React-based application. This library is a fork of react-transliterate, which uses Google Transliterate API which supports around 40 languages across the globe. In this module, our focus is to provide high-quality transliteration-suggestions for Indic languages, especially for low-resource languages like Kashmiri, Manipuri, etc. (which are not supported by Google). For more details about the AI system behind this, please check AI4Bhārat Indic-Xlit.

2. Demo

Click-here to try our demo!

Source of this demo is available in the example folder of the repo.

3. Install

npm install --save @ai4bharat/indic-transliterate

OR

yarn add @ai4bharat/indic-transliterate

4. Usage

4.1. Basic example

import React, { useState } from "react";

import { IndicTransliterate } from "@ai4bharat/indic-transliterate";
import "@ai4bharat/indic-transliterate/dist/index.css";

const App = () => {
  const [text, setText] = useState("");

  return (
    <IndicTransliterate
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang="hi"
    />
  );
};

export default App;

4.2. With custom component

import React, { useState } from "react";

import { IndicTransliterate } from "@ai4bharat/indic-transliterate";
import "@ai4bharat/indic-transliterate/dist/index.css";

const App = () => {
  const [text, setText] = useState("");

  return (
    <IndicTransliterate
      renderComponent={(props) => <textarea {...props} />}
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang="hi"
    />
  );
};

export default App;

4.3. With TypeScript

import React, { useState } from "react";

import { IndicTransliterate, Language } from "@ai4bharat/indic-transliterate";
import "@ai4bharat/indic-transliterate/dist/index.css";

const App = () => {
  const [text, setText] = useState("");
  const [lang, setLang] = useState<Language>("hi");

  return (
    <IndicTransliterate
      renderComponent={(props) => <textarea {...props} />}
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang={lang}
    />
  );
};

export default App;

4.4. With Material-UI

import React, { useState } from "react";

import { IndicTransliterate, Language } from "@ai4bharat/indic-transliterate";
import "@ai4bharat/indic-transliterate/dist/index.css";

import Input from "@material-ui/core/Input";

const App = () => {
  const [text, setText] = useState("");
  const [lang, setLang] = useState<Language>("hi");

  return (
    <IndicTransliterate
      renderComponent={(props) => {
        const inputRef = props.ref;
        delete props["ref"];
        return <Input {...props} inputRef={inputRef} />;
      }}
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang={lang}
    />
  );
};

export default App;

5. Get transliteration suggestions

5.1. Standalone example

import { getTransliterateSuggestions } from "@ai4bharat/indic-transliterate";

const data = await getTransliterateSuggestions(
  word, // word to fetch suggestions for
  {
    numOptions: 5, // number of suggestions to fetch
    showCurrentWordAsLastSuggestion: true, // add the word as the last suggestion
    lang: "hi", // target language
  },
);

5.2. Custom trigger keys

Keys which when pressed, input the current selection to the textbox

Indic Transliterate uses the event.keycode property to detect keys. Here are some predefined keys you can use. Or, you can enter the integer codes for any other key you'd like to use as the trigger

import React, { useState } from "react";

import { IndicTransliterate, TriggerKeys } from "@ai4bharat/indic-transliterate";
import "@ai4bharat/indic-transliterate/dist/index.css";

import Input from "@material-ui/core/Input";

const App = () => {
  const [text, setText] = useState("");

  return (
    <IndicTransliterate
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang="hi"
      triggerKeys={[
        TriggerKeys.KEY_RETURN,
        TriggerKeys.KEY_ENTER,
        TriggerKeys.KEY_SPACE,
        TriggerKeys.KEY_TAB,
      ]}
    />
  );
};

export default App;

5.3. Props

PropRequired?DefaultDescription
onChangeTextYesListener for the current value from the component. (text: string) => void
valueYesvalue prop to pass to the component
enabledtrueControl whether suggestions should be shown
renderComponent(props) => <input {...props} />Component to render. You can pass components from your component library as this prop
langhiLanguage you want to transliterate. See the following section for language codes
maxOptions5Maximum number of suggestions to show in helper
offsetY0Extra space between the top of the helper and bottom of the caret
offsetX0Extra space between the caret and left of the helper
containerClassNameempty stringClassname passed to the container of the component
containerStyles{}CSS styles object passed to the container
activeItemStyles{}CSS styles object passed to the active item <li> tag
hideSuggestionBoxOnMobileDevicesfalseShould the suggestions be visible on mobile devices since keyboards like Gboard and Swiftkey support typing in multiple languages
hideSuggestionBoxBreakpoint450type: number. To be used when hideSuggestionBoxOnMobileDevices is true. Suggestion box will not be shown below this device width
triggerKeysKEY_SPACE, KEY_ENTER, KEY_TAB, KEY_RETURNKeys which when pressed, input the current selection to the textbox
insertCurrentSelectionOnBlurtrueShould the current selection be inserted when blur event occurs
showCurrentWordAsLastSuggestiontrueShow current input as the last option in the suggestion box

6. Languages

6.1. Get supported languages

import { getTransliterationLanguages } from "@ai4bharat/indic-transliterate";

const data = await getTransliterationLanguages();

6.2. List of language codes

Currently supports the following 21 languages from the Indian subcontinent:

ISO 639 codeLanguage
asAssamese - অসমীয়া
bnBangla - বাংলা
brxBoro - बड़ो
guGujarati - ગુજરાતી
hiHindi - हिंदी
knKannada - ಕನ್ನಡ
ksKashmiri - كٲشُر
gomKonkani Goan - कोंकणी
maiMaithili - मैथिली
mlMalayalam - മലയാളം
mniManipuri - ꯃꯤꯇꯩꯂꯣꯟ
mrMarathi - मराठी
neNepali - नेपाली
orOriya - ଓଡ଼ିଆ
paPanjabi - ਪੰਜਾਬੀ
saSanskrit - संस्कृतम्
sdSindhi - سنڌي
siSinhala - සිංහල
taTamil - தமிழ்
teTelugu - తెలుగు
urUrdu - اُردُو

7. License

MIT © ai4bharat

Sincere thanks to burhanuday for making his work open-source!

1.3.2

16 days ago

1.3.1

18 days ago

1.3.0

2 months ago

1.2.15

1 year ago

1.2.12

2 years ago

1.2.13

2 years ago

1.2.14

2 years ago

1.2.11

2 years ago

1.2.0

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.9

2 years ago

1.2.10

2 years ago

1.1.9

2 years ago