Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
simple-translation
Advanced tools
Readme
For those who want to support multiple languages without a lot of complication, dependancies or ties to a framework.
export default {
language: "english",
languageCode: "en",
messages: {
exampleString: "Example string",
exampleFunction: variable => `Example function that returns a string plus a variable of ${variable}`,
},
}
Simply populate your translations in the messages object seen in the example above and save the file somewhere in your project, such as in ./translations/english.js
Messages can either be a string or a function so that you can generate strings dynamically.
For dynamic strings it will take a list of variables and return a templated string.
see ./examples/index.html
The example file shows various uses and error handling.
The translations will default to user's browser's language. If there is no translation file for that language it will default to the english translation file. Therefore an english (or other default) translation file is required at a minimum.
import SimpleTranslation from '../simple-translation.js'
import english from '../languages/english.js'
import french from '../languages/french.js'
let translate = new SimpleTranslate()
translate.registerLanguage(english)
translate.registerLanguage(french)
Option A - specify each language file individually using the registerLanguage() method
translate.registerLanguage(english)
Option B - specify the language files upon new translate, which will be automatically registered
let translate = new SimpleTranslation(english, french)
translate.message('exampleString')
returns: Example string
translate.message('exampleFunction')('test')
returns: Example function that returns a string plus a variable of test
translate.message('exampleString', 'fr')
returns: Exemple de chaîne
This package sets 'en' as the default / fallback language. If you wanted to use a different default language you can simply modify the property after instantiating simple-translation.
translate.defaultLanguage = 'de'
from the top...
import SimpleTranslation from '../simple-translation.js'
import german from '../languages/german.js'
import french from '../languages/french.js'
let translate = new SimpleTranslate(german, french)
translate.defaultLanguage = 'de'
This method would be handy if you want to, for example, render a drop down list of supported languages in your app and then allow the user to choose which language to display.
translate.getSupportedLanguages()
returns: ["en", "fr"]
This method is used internally to see if the user's default language matches any of the registered language files.
translate.isLanguageSupported('en')
returns: true / false
translate.getLocale('en')
returns: { language: "english", languageCode: "en", messages: { exampleString: "Example string", exampleFunction: variable => `Example function that returns a string plus a variable of ${variable}`, }, }
No tests have been written yet but the obvious first one is a test which you would include in your build process to ensure that for each language they all have the same keys. A likely bug to enter production would be missing translations for a given language file.
author: Richard Bettridge (ssshake)
web: http://daggasoft.com
twitter: @richbettridge
FAQs
A super basic and simple way to do language translations. Auto detects language from the browser. Supports compiled strings. Aims to be an uncomplicated translation package
The npm package simple-translation receives a total of 0 weekly downloads. As such, simple-translation popularity was classified as not popular.
We found that simple-translation 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.