-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from iteratehq/matt/translations
feat: Handle translations for prompt message and button text
- Loading branch information
Showing
3 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { NativeModules, Platform } from 'react-native'; | ||
import type { | ||
Language, | ||
Survey, | ||
TranslationItemKey, | ||
UserTraitsContext, | ||
} from './types'; | ||
import { useSelector } from 'react-redux'; | ||
import type { State } from './redux'; | ||
|
||
const availableLanguages = (survey: Survey): Language[] => { | ||
const languages = survey.translations?.map((t) => t.language) ?? []; | ||
if (survey.primary_language) { | ||
languages.unshift(survey.primary_language); | ||
} | ||
|
||
return languages; | ||
}; | ||
|
||
const getPreferredLanguage = ( | ||
survey: Survey, | ||
userTraits?: UserTraitsContext | ||
): Language => { | ||
const deviceLanguageWithRegion = | ||
Platform.OS === 'ios' | ||
? NativeModules.SettingsManager.settings.AppleLocale ?? | ||
NativeModules.SettingsManager.settings.AppleLanguages[0] // iOS 13 | ||
: NativeModules.I18nManager.localeIdentifier; | ||
const deviceLanguage = deviceLanguageWithRegion.substring(0, 2); | ||
|
||
const userTraitLanguage = userTraits?.language as Language; | ||
|
||
if ( | ||
userTraitLanguage != null && | ||
availableLanguages(survey).includes(userTraitLanguage) | ||
) { | ||
return userTraitLanguage; | ||
} | ||
|
||
if (availableLanguages(survey).includes(deviceLanguage)) { | ||
return deviceLanguage; | ||
} | ||
|
||
return 'en'; | ||
}; | ||
|
||
const getTranslationForKey = ( | ||
key: TranslationItemKey, | ||
survey: Survey, | ||
userTraits?: UserTraitsContext | ||
): string | undefined => { | ||
const language = getPreferredLanguage(survey, userTraits); | ||
const translation = survey.translations?.find((t) => t.language === language); | ||
|
||
return translation?.items?.[key]?.text; | ||
}; | ||
|
||
export const useTranslation = (key: TranslationItemKey) => { | ||
const survey = useSelector((state: State) => state.survey); | ||
const userTraits = useSelector((state: State) => state.userTraits); | ||
if (survey == null) { | ||
return undefined; | ||
} | ||
|
||
return getTranslationForKey(key, survey, userTraits); | ||
}; |
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