
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
simple-translation
Advanced tools
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
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
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
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.