Skip to content

Commit 69ccb60

Browse files
authored
Merge pull request stefalda#97 from fbacker/overwrite
feature: overwrite content
2 parents 55c3acf + e56c49b commit 69ccb60

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

LocalizedStrings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class LocalizedStrings {
4343
this.setLanguage(interfaceLanguage);
4444
}
4545

46+
//Overwrite content, use for e.g. dynamicly reload locale from server
47+
setContent(props){
48+
this.props = props;
49+
//Set language to its default value (the interface)
50+
this.setLanguage(this.language);
51+
}
52+
4653
//Can be used from ouside the class to force a particular language
4754
//indipendently from the interface one
4855
setLanguage(language) {
@@ -93,6 +100,11 @@ class LocalizedStrings {
93100
return interfaceLanguage;
94101
}
95102

103+
//The current props (locale object)
104+
getLocaleObject() {
105+
return this.props;
106+
}
107+
96108
//Return an array containing the available languages passed as props in the constructor
97109
getAvailableLanguages() {
98110
if (!this.availableLanguages) {

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,35 @@ Then use the `strings` object literal directly in the render method accessing th
126126

127127
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.
128128

129+
#### Update / Overwrite Locale
130+
131+
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.
132+
133+
```js
134+
strings.setContent({
135+
en:{
136+
how:"How do you want your egg todajsie?",
137+
boiledEgg:"Boiled eggsie",
138+
softBoiledEgg:"Soft-boiled egg",
139+
choice:"How to choose the egg"
140+
}
141+
})
142+
```
143+
144+
You can also only overwrite a specific language using
145+
146+
```js
147+
strings.setContent(Object.assign({},strings.getLocaleObject(),
148+
{
149+
en:{
150+
how:"How do you want your egg todajsie?",
151+
boiledEgg:"Boiled eggsie",
152+
softBoiledEgg:"Soft-boiled egg",
153+
choice:"How to choose the egg"
154+
}
155+
}));
156+
```
157+
129158
## Typescript
130159
For TypeScript, your `tsconfig.json` should be something like this:
131160
```json

0 commit comments

Comments
 (0)