Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
localized-strings
Advanced tools
Simple module to localize the strings of any JS based program using the same syntax used in the ReactLocalization and ReactNativeLocalization module, use 'npm run build' before publishing
Simple module to localize the strings of any JS based program using the same syntax used in the ReactLocalization module and ReactNativeLocalization module libraries.
The library uses the current interface language, then it loads and displays the strings matching the current interface locale or the default language (the first one if a match is not found) if a specific localization can't be found.
It's possible to force a language different from the interface one.
It's possible to configure the library to use a specific routine that return the interface language, so it's possible to extend it in any possible environment.
npm install --save localized-strings
In the class that you want to localize require the library and define the strings object passing to the constructor a simple object containing a language key (i.e. en, it, fr..) and then a list of key-value pairs with the needed localized strings.
\\ES6 module syntax
import LocalizedStrings from 'localized-strings';
let strings = new LocalizedStrings({
en:{
how:"How do you want your egg today?",
boiledEgg:"Boiled egg",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
},
it: {
how:"Come vuoi il tuo uovo oggi?",
boiledEgg:"Uovo sodo",
softBoiledEgg:"Uovo alla coque",
choice:"Come scegliere l'uovo"
}
});
Then use the strings
object literal directly in the render method accessing the key of the localized string.
console.log ("HOW: " + strings.how);
The first language is considered the default one, so if a translation is missing for the selected language, the default one is shown and a line is written to the log as a reminder.
You might have default localized in the build but then download the latest localization strings from a server. Use setContent to overwrite the whole object.
NOTE that this will remove all other localizations if used.
strings.setContent({
en:{
how:"How do you want your egg todajsie?",
boiledEgg:"Boiled eggsie",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
}
})
You can also only overwrite a specific language using:
strings.setContent(Object.assign({},strings.getContent(),
{
en:{
how:"How do you want your egg todajsie?",
boiledEgg:"Boiled eggsie",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
}
}));
You can pass a custom method to get the current interface based on the context.
The default method check the browser language but it could be replaced with other check if you are in a Server, ReactNative or Nativescript project.
The getInterfaceLanguage method can be as simple as:
function getCustomInterfaceLanguage(){
return "it-IT";
}
let strings = new LocalizedStrings({
en:{
how:"How do you want your egg today?",
boiledEgg:"Boiled egg",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg"
},
it: {
how:"Come vuoi il tuo uovo oggi?",
boiledEgg:"Uovo sodo",
softBoiledEgg:"Uovo alla coque",
choice:"Come scegliere l'uovo"
}
}, getCustomInterfaceLanguage)
This is how you can use the library in a Nativescript project
const platform = require("platform");
this.strings = new LocalizedStrings({
it: {
score: "Punti",
time: "Tempo"
},
en: {
score: "Score",
time: "Time"
}
}, () => {
return platform.device.language;
}
);
en:{
bread:"bread",
butter:"butter",
question:"I'd like {0} and {1}, or just {0}"
...
login: 'login',
onlyForMembers: 'You have to {0} in order to use our app',
bold: 'bold',
iAmText: 'I am {0} text',
...
january: 'January',
currentDate: 'The current date is {month} {day}, {year}!'
}
...
strings.formatString(strings.question, strings.bread, strings.butter)
// Named tokens can also be used to give some extra context to the format strings
// You cannot mix tokens, something like formatString('{0}, {name}', 'Hello', {name: 'Bob'}) won't work
strings.formatString(strings.currentDate, {
month: strings.january,
day: 12,
year: 2018
})
Beware: do not define a string key as formatString!
To force a particular language use something like this:
_onSetLanguageToItalian() {
strings.setLanguage('it');
this.setState({});
}
Because of the dynamically generated class properties, it's a little tricky to have the autocomplete functionality working.
Anyway it's possible to gain the desired results by:
This is the suggested solution to work with Typescript:
export interface IStrings extends LocalizedStringsMethods{
score:string;
time: String;
}
public strings: IStrings;
this.strings = new LocalizedStrings({
it: {
score: "Punti",
time: "Tempo"
},
en: {
score: "Score",
time: "Time"
}
});
Feel free to contact me on Twitter or open an issue.
FAQs
Simple module to localize the strings of any JS based program using the same syntax used in the ReactLocalization and ReactNativeLocalization module, use 'npm run build' before publishing
The npm package localized-strings receives a total of 24,480 weekly downloads. As such, localized-strings popularity was classified as popular.
We found that localized-strings 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.