Last active
May 19, 2018 20:07
-
-
Save tylerthebuildor/5a5e8db54d95d6b1bc3d3f2dad01fd4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Copy { | |
static language = 'en'; | |
static languageMap = { | |
en: { Hello: 'Hello' }, | |
sp: { Hello: 'Hola' }, | |
}; | |
set language(language) { | |
Copy.language = language; | |
} | |
get language() { | |
return Copy.language; | |
} | |
write(text = '') { | |
const { language, languageMap } = Copy; | |
const hasMatch = languageMap[language] && languageMap[language][text]; | |
const words = hasMatch ? languageMap[language][text] : text; | |
return words; | |
} | |
} | |
// Make Copy a singleton | |
const instance = new Copy(); | |
Object.freeze(instance); | |
export default instance; | |
// Somewhere else in your code | |
import React from 'react'; | |
import Copy from './Copy'; | |
// You can set this once in a root component and it will stay the same everywhere because it is a singleton | |
Copy.language = 'sp'; | |
export default class App extends React.PureComponent { | |
render() { | |
// outputs "Hola World!" | |
return ( | |
<h1>{Copy.write('Hello')} World!</h1> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment