Skip to content

Instantly share code, notes, and snippets.

@mutaphysis
Last active November 7, 2018 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutaphysis/a4db3927230e7ee7acd6d205c0dfa459 to your computer and use it in GitHub Desktop.
Save mutaphysis/a4db3927230e7ee7acd6d205c0dfa459 to your computer and use it in GitHub Desktop.
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