Last active
November 7, 2018 09:42
-
-
Save mutaphysis/a4db3927230e7ee7acd6d205c0dfa459 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
export function translatedStringFromObject(localizedString: ?LocalizedString): ?string { | |
if (!localizedString) return null; | |
if (typeof localizedString === 'string') { | |
return localizedString; | |
} | |
if (typeof localizedString === 'object') { | |
const expandedLocalizedString = { ...localizedString }; | |
// add locales without country | |
for (const underscoreLocale in localizedString) { | |
const withoutCountry = localeWithoutCountry(underscoreLocale); | |
expandedLocalizedString[withoutCountry] = expandedLocalizedString[underscoreLocale]; | |
} | |
const locales = currentLocales; | |
for (let i = 0; i < locales.length; i++) { | |
const translatedString = expandedLocalizedString[locales[i]]; | |
if (translatedString) return translatedString; | |
} | |
const firstAvailableLocale = Object.keys(expandedLocalizedString)[0]; | |
return expandedLocalizedString[firstAvailableLocale]; // return the untranslated string as last option | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment