Last active
April 16, 2017 16:02
-
-
Save Krisztiaan/e22b63b0e22671f83cb3e81ed6b19553 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
/* ./strings.js */ | |
/* @flow */ | |
'use strict' | |
import English from './translations/en' | |
import Hungarian from './translations/hu' | |
export const Languages = { | |
EN: 'en', | |
HU: 'hu' | |
} | |
export type Language = 'en' | 'hu' | |
var language: Language = Languages.EN | |
var translations = English | |
export const setLanguage = (newLng: Language = Languages.EN) => { | |
language = newLng | |
switch(language) { | |
case Languages.EN: | |
translations = English | |
break | |
case Languages.HU: | |
translations = Hungarian | |
break | |
} | |
} | |
export function s(stringId: string, ...params: Array<string>): string { | |
if(!translations.hasOwnProperty(stringId)) { | |
console.error("No translation for " + stringId + " in language '" + language + "'!") | |
} | |
return template(translations[stringId])(params) | |
} | |
function template(strings, ...keys) { | |
return (function(...values) { | |
var dict = values[values.length - 1] || {}; | |
var result = [strings[0]]; | |
keys.forEach(function(key, i) { | |
var value = Number.isInteger(key) ? values[key] : dict[key]; | |
result.push(value, strings[i + 1]); | |
}); | |
return result.join(''); | |
}); | |
} | |
/* ./en/index.js */ | |
/* @flow */ | |
export default { | |
profile_title: "Profile", | |
profile_subtitle: "Profile subtitle", | |
profile_welcome: `Welcome dear ${0}!` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment