Gathering detailed insights and metrics for @assembless/react-littera
Gathering detailed insights and metrics for @assembless/react-littera
Gathering detailed insights and metrics for @assembless/react-littera
Gathering detailed insights and metrics for @assembless/react-littera
🌐 Modern react library for managing translations.
npm install @assembless/react-littera
Typescript
Module System
Node Version
NPM Version
TypeScript (89.72%)
JavaScript (10.28%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
20 Stars
241 Commits
1 Forks
2 Watchers
15 Branches
3 Contributors
Updated on Nov 23, 2024
Latest Version
2.4.0
Package Id
@assembless/react-littera@2.4.0
Unpacked Size
1.00 MB
Size
361.92 kB
File Count
33
NPM Version
9.3.1
Node Version
19.5.0
Published on
Feb 07, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
39
🌐 Modern react library for managing translations.
Littera was created to make maintaining and managing translations easier. It allows placing translations right beside your component as well as storing translations globally. Littera's structure was inspired by react-jss.
Here below we have a translations object which is accepted by the core translate
function, which then returns the translated string for the correct language. It can be passed to the useLittera
hook or withLittera
HOC.
1{ 2 welcome: { 3 en_US: "Welcome", 4 pl_PL: "Witamy", 5 de_DE: "Willkommen" 6 } 7}
Let's say the active language is en_US
(English), the output will be:
1{ 2 welcome: "Welcome" 3}
Let's assume you want to have a translations system in your React app that updates all the text when the language changes. Bam! All you need to do is: define a simple object that lists all translated strings for each language. Then pass it to a hook and it will return a reduced object with translations only for active language. Display it like any other string. Ready.
via npm
npm install @assembless/react-littera
via yarn
yarn add @assembless/react-littera
or clone/download the repository.
First you have to wrap your components with a provider and feed it with a list of available languages.
1import React, { useState } from "react"; 2import ReactDOM from "react-dom"; 3 4import { LitteraProvider } from "@assembless/react-littera"; 5 6function App() { 7 return ( 8 <div className="App"> 9 <LitteraProvider locales={[ "en_US", "pl_PL", "de_DE" ]}> 10 <YourApp /> 11 </LitteraProvider> 12 </div> 13 ); 14} 15 16const rootElement = document.getElementById("root"); 17ReactDOM.render(<App />, rootElement);
Now you can make use of Littera by adding translations directly into your component.
Here we have two options:
1import React from "react"; 2import { useLittera } from "@assembless/react-littera"; 3 4// Object containing translations for each key... 5const translations = { 6 example: { 7 en_US: "Example", 8 pl_PL: "Przykład", 9 de_DE: "Beispiel" 10 } 11}; 12 13const ExampleComponent = () => { 14 // Obtain our translated object. 15 const translated = useLittera(translations); 16 // Get access to global littera methods for currect context. 17 const methods = useLitteraMethods(); 18 19 const handleLocaleChange = () => { 20 // Change language to German. 21 methods.setLocale("de_DE"); 22 } 23 24 return <button onClick={handleLocaleChange}>{translated.example}</button>; 25}; 26 27export default ExampleComponent;
1import React from "react"; 2import { useLittera } from "@assembless/react-littera"; 3 4const translations = { 5 // Use a function for variable translations. 6 hello: (name) => ({ 7 en_US: `Hello ${name}`, 8 pl_PL: `Cześć ${name}`, 9 de_DE: `Hallo ${name}` 10 }) 11}; 12 13const ExampleComponent = () => { 14 // Obtain our translated object. 15 const translated = useLittera(translations); 16 17 // Call the method obtained from our translated object with required arguments. 18 const varTranslation = translated.hello("Mike"); 19 20 return <button onClick={handleLocaleChange}>{varTranslation}</button>; 21}; 22 23export default ExampleComponent;
1import React from "react"; 2import { useLittera } from "@assembless/react-littera"; 3 4const translations = { 5 greetings: [ 6 { 7 de_DE: "Guten Tag", 8 en_US: "Good morning" 9 }, 10 { 11 de_DE: "Hallo", 12 en_US: "Hello" 13 }, 14 ] 15}; 16 17const ExampleComponent = () => { 18 // Obtain our translated object. 19 const translated = useLittera(translations); 20 21 // Get the translated strings from the array. 22 const varTranslation = translated[0]; // => Good morning 23 24 return <button onClick={handleLocaleChange}>{varTranslation}</button>; 25}; 26 27export default ExampleComponent;
1import React from "react"; 2import { withLittera } from "@assembless/react-littera"; 3 4// Object containing translations for each key... 5const translations = { 6 example: { 7 en_US: "Example", 8 pl_PL: "Przykład", 9 de_DE: "Beispiel" 10 } 11}; 12 13class ExampleComponent extends React.Component { 14 15 handleLocaleChange() { 16 const { setLocale } = this.props; 17 18 setLocale("de_DE"); 19 } 20 21 render() { 22 const { translated } = this.props; 23 24 return <button onClick={this.handleLocaleChange}>{translated.example}</button>; 25 } 26} 27 28export default withLittera(translation)(ExampleComponent);
type: ReactContext<ILitteraProvider>
Component providing the core context. To use withLittera
and useLittera
properly, you have to wrap your components with this provider.
Key | Description | Type | Default |
---|---|---|---|
initialLocale | Initial language. | string | |
locales | List of available languages. | Array<string> | [ "en_US" ] |
setLocale | Callback called when active language changes. | (locale: string) => void | |
preset | Preset of translations. | { [key: string]: { [locale: string]: string } } | {} |
pattern | Locale pattern. Default format is xx_XX. | RegExp | /[a-z]{2}_[A-Z]{2}/gi |
detectLocale | Tries to detect the browser language. Overriding initialLocale if detected. Not available yet for React Native! | boolean | false |
type: (translations: ITranslations) => (Component: React.FunctionComponent) => JSX.Element
A HOC, you feed it with translations
(ITranslations) and a component which then gets the translated
object passed via prop (e.g. withLittera(translations)(Component)
).
Key | Description | Type | Default |
---|---|---|---|
translated | Translated object | ITranslated | |
setLocale | Changes active language | (locale: string) => void | |
preset | Preset of translations | { [key: string]: { [locale: string]: string } } | {} |
locale | Active language | string | en_US |
type: (translations: ITranslations) => ITranslated
A Hook, you feed it with translations
(ITranslations) and it returns translated
(ITranslated).
type: () => { see methods below }
This hook exposes following methods:
Key | Description | Type |
---|---|---|
locale | Active language | string |
locales | List of all locales | string[] |
setLocale | Changes active language | (locale: string) => void |
validateLocale | Validates locale with pattern | (locale: string, pattern?: RegExp) => boolean |
preset | Preset object previously passed to the provider | ITranslations |
translate | Core translate method | (translations: T, locale: string) => ITranslated |
translateSingle | Core method for translating a single key | <T>(translation: T, locale: string) => ISingleTranslated<T> |
{ [locale: string]: string }
1{ 2 de_DE: "Einfach", 3 en_US: "Simple" 4}
(...args: (string | number)[]) => ITranslation
1(name) => ({ 2 de_DE: `Hallo ${name}`, 3 en_US: `Hello ${name}` 4})
ITranslation[]
1[ 2 { 3 de_DE: "Beispiel", 4 en_US: "Example" 5 }, 6]
{ [key: string]: ITranslation | ITranslationVarFn }
1{ 2 simple: { 3 de_DE: "Einfach", 4 en_US: "Simple" 5 }, 6 hello: (name) => ({ 7 de_DE: `Hallo ${name}`, 8 en_US: `Hello ${name}` 9 }), 10 greetings: [ 11 { 12 de_DE: "Guten Tag", 13 en_US: "Good morning" 14 }, 15 { 16 de_DE: "Hallo", 17 en_US: "Hello" 18 }, 19 ] 20}
{ [key: string]: string | ((...args: (string | number)[]) => string) | string[] }
1{ 2 simple: "Simple", 3 hello: (name) => "Hello Mike", // Run this function to get variable translation. 4 greetings: [ "Good morning", "Hello" ] 5}
After cloning the repo, install all dependencies using npm install
.
Build:
npm run build
Test the library:
npm test
The migration process is straightforward. You have to rename some properties and change the way you use useLittera
.
language
=> locale
setLanguage
=> setLocale
Mainly pay attention to LitteraProvider
and withLittera
props naming.
The provider accepts 2 new props locales: string[]
and initialLocale?: string
. You don't need to use your own state from now, the provider will handle it by itself. That makes the locale
and setLocale
props not required.
1// v1.X 2import LitteraProvider from "react-littera"; 3 4const App = () => { 5 const [language, setLanguage] = useState("en_US"); 6 7 return <LitteraProvider language={language} setLanguage={setLanguage}> 8 ... 9 </LitteraProvider> 10} 11 12// v2.X 13import { LitteraProvider } from "@assembless/react-littera"; 14 15const App = () => { 16 17 return <LitteraProvider locales={["en_US", "de_DE", "pl_PL"]}> 18 ... 19 </LitteraProvider> 20}
The hook returns only the translated object now. Use useLitteraMethods
to get/set locale, set pattern etc.
1// The translations object remains the same. 2const translations = { 3 example: { 4 "en_US": "Example", 5 "de_DE": "Beispiel", 6 "pl_PL": "Przykład" 7 } 8} 9 10// v1.X 11const [translated, locale, setLanguage] = useLittera(translations) 12 13// v2.X 14const translated = useLittera(translations); 15const { locale, setLocale, pattern, setPattern, validateLocale } = useLitteraMethods();
Yes, we have not implemented a translator to keep this package simple and lightweight also providing the translations manually guarantees a better user experience.
React Native compatibility has not been tested but the community reported 100% usability.
Just define the translations object in your components file or directory. It will travel with your component, just remember to add @assembless/react-littera as a dependency!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 3/21 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
83 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More