Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Handle translations for prompt message and button text #267

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/components/Prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Iterate from '../../iterate';
import markdown from '../../markdown';
import { InteractionEvents } from '../../interaction-events';
import type { InteractionEventSource } from '../../interaction-events';
import { useTranslation } from '../../language';

type Props = {
dispatchShowSurvey: (Survey: Survey) => void;
Expand Down Expand Up @@ -147,6 +148,11 @@ const Prompt: (Props: Props) => JSX.Element = ({
: null,
];

const promptText =
useTranslation('survey.prompt.text') ?? survey?.prompt?.message;
const promptButtonText =
useTranslation('survey.prompt.buttonText') ?? survey?.prompt?.button_text;

return (
<Animated.View
style={{
Expand All @@ -170,7 +176,7 @@ const Prompt: (Props: Props) => JSX.Element = ({
>
<View style={{ paddingBottom }}>
<CloseButton onPress={onDismissAnimated} survey={survey} />
{markdown.Render(survey?.prompt?.message ?? '', {
{markdown.Render(promptText ?? '', {
body: [
promptTextStyle,
{
Expand All @@ -186,11 +192,9 @@ const Prompt: (Props: Props) => JSX.Element = ({
textDecorationLine: 'none',
color: survey?.color ?? '#7457be',
},
}) || (
<Text style={promptTextStyle}>{survey?.prompt?.message ?? ''}</Text>
)}
}) || <Text style={promptTextStyle}>{promptText ?? ''}</Text>}
<PromptButton
text={`${survey?.prompt?.button_text || ''}`}
text={`${promptButtonText || ''}`}
color={`${survey?.color || '#7457be'}`}
colorDark={survey?.color_dark}
onPress={showSurveyButtonClicked}
Expand Down
66 changes: 66 additions & 0 deletions src/language.tsx
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);
};
19 changes: 19 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ export type Survey = {
color_dark?: string;
company_id: string;
id: string;
primary_language?: Language;
prompt?: Prompt;
title: string;
translations?: Translation[];
};

export type Prompt = {
Expand Down Expand Up @@ -171,3 +173,20 @@ export type Question = {
id: string;
prompt: string;
};

export type Translation = {
language: Language;
items?: TranslationItems;
};

export type TranslationItems = { [K in TranslationItemKey]: TranslationItem };

export type TranslationItem = {
text?: string;
};

export type TranslationItemKey =
| 'survey.prompt.text'
| 'survey.prompt.buttonText';

export type Language = string;
Loading