Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
app-translator
Advanced tools
Define multiple dictionaries for your app and translate strings instantly
Define multiple dictionaries for your app and translate strings instantly at runtime.
This tool does not call external APIs to translate your strings, you must first define yours dictionaries.
npm install app-translator
Compatible with Node >=8.0.0
Index
Dictionary
Collection
AppTranslatorOptions
initTranslator
translate (alias t)
tryUseBrowserLanguage
getAvailableLanguages
setLanguage
setOptions
Define a single dictionary.
Property | Type | Description |
---|---|---|
name | string | The language name |
bcp47 | string | A valid BCP47 tag |
pairs | { [ original: string ]: string } | Pairs of original-translated strings |
Define the dictionaries collection in an array of dictionaries.
Array <Dictionary>
Define options for AppTranslator.
Property | Type | Description |
---|---|---|
caseSensitive? | boolean | Look for the string in dictionary without consider the letters case |
autoCapitalize? | boolean | Capitalize automatically the first letter |
logs? | boolean | Emit warns and non-blocking errors to the console |
Initialize App Translator with a target language, a collection and custom options. If a collection is not provided, translate() method will bypass your strings.
⚠ It throws an error if you try to initialize App Translator multiple times.
Parameter | Type | Description |
---|---|---|
language | string | The primary dictionary to use for translations |
collection? | Collection | The collection of dictionaries |
options? | AppTranslatorOptions | Define the behavior of AppTranslator |
Return the translated string in the chosen language or the original string if no translation was found
Parameter | Type | Description |
---|---|---|
originalStr | string or number | The original string in the code |
capitalize | boolean | Capitalize the first letter of the output |
Try to infer the dictionary from the browser. It compares the bcp47 tag in the dictionaries with navigator.language. If a dictionary was found, it returns the found language name and sets it as primary language. Otherwise returns null and does not change the language.
Return an array of the names of the currently loaded dictionaries.
Set a new primary language. If not present in the collection will generate a console error but will not change the language.
Override the provided new options with the old one.
⚠ It throws an error if you pass invalid options
You can import dictionaries as json or js modules. In this example I'll use the ES js modules.
src/languages/
export const italian = {
name: 'italian',
bcp47: 'it-IT',
pairs: {
'leave a comment': 'lascia un commento',
},
}
src/languages/
with all your exported languages (you can skip this step and import directly in your entry point, but this is more pratical for many dictionaries)export * from './russian'
export * from './german'
export * from './italian'
export * from './spanish'
import * as languages from './languages'
const collection = Object.values(languages)
import { initTranslator } from "app-translator";
initTranslator("italian", collection, { caseSensitive: false, autoCapitalize: true });
Now the dictionaries and options are available in window.appTranslator.
import { t } from "app-translator";
const App = () => {
return <h1>{ t("leave a Comment") }</h1>;
};
Folder structure:
src/
--main.js
--languages/
----russian.js
----german.js
----italian.js
----spanish.js
----index.js
You can use App Translator even without a module system or organize your exports as you think is best, there are no specific rules about it.
No dependencies
MIT
FAQs
Define multiple dictionaries for your app and translate strings instantly
We found that app-translator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.