Skip to content

Commit

Permalink
use 'i18n.resolvedLanguage', not 'language'
Browse files Browse the repository at this point in the history
This was added in a relatively recent version of i18next; some online resources haven't updated to reflect it
https://www.i18next.com/overview/api#resolvedlanguage

`language` is what i18next detected or we have set it to. It could be either a locale code like 'en-US' or a plain language like 'en'.

`resolvedLanguage` is what language is actually being used for translation, after considering what is detected, what langauges we provided, and what fallbacks we specified.

In our case this should always be either 'en', 'es', or 'lo' - the 3 languages we support at this time.

So `resolvedLanguage` is the safe property to use and we should avoid `language`.
  • Loading branch information
JGreenlee committed Feb 12, 2024
1 parent 069a72e commit 633db15
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion www/js/config/useImperialConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function formatForDisplay(value: number): string {
if (value >= 100) opts.maximumFractionDigits = 0;
else if (value >= 1) opts.maximumSignificantDigits = 3;
else opts.maximumFractionDigits = 2;
return Intl.NumberFormat(i18next.language, opts).format(value);
return Intl.NumberFormat(i18next.resolvedLanguage, opts).format(value);
}

export function convertDistance(distMeters: number, imperial: boolean): number {
Expand Down
2 changes: 1 addition & 1 deletion www/js/control/DataDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const DataDatePicker = ({ date, setDate, open, setOpen, minDate }) => {
return (
<>
<DatePickerModal
locale={i18n.language}
locale={i18n.resolvedLanguage || 'en'}
mode="single"
visible={open}
onDismiss={onDismiss}
Expand Down
2 changes: 1 addition & 1 deletion www/js/diary/diaryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function getFormattedTimeRange(beginFmtTime: string, endFmtTime: string)
const endTime = DateTime.fromISO(endFmtTime, { setZone: true });
const range = endTime.diff(beginTime, ['hours', 'minutes']);
return humanizeDuration(range.as('milliseconds'), {
language: i18next.language,
language: i18next.resolvedLanguage,
largest: 1,
round: true,
});
Expand Down
5 changes: 4 additions & 1 deletion www/js/onboarding/PrivacyPolicy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const PrivacyPolicy = () => {
);
}

const templateText = useMemo(() => getTemplateText(appConfig, i18n.language), [appConfig]);
const templateText = useMemo(
() => getTemplateText(appConfig, i18n.resolvedLanguage),
[appConfig],
);

return (
<>
Expand Down
5 changes: 4 additions & 1 deletion www/js/onboarding/StudySummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const StudySummary = () => {
const { i18n } = useTranslation();
const appConfig = useAppConfig();

const templateText = useMemo(() => getTemplateText(appConfig, i18n.language), [appConfig]);
const templateText = useMemo(
() => getTemplateText(appConfig, i18n.resolvedLanguage),
[appConfig],
);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion www/js/splash/notifScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function getNotifs() {
function scheduleNotifs(scheme, notifTimes: DateTime[], setIsScheduling: Function) {
return new Promise<void>((rs) => {
setIsScheduling(true);
const localeCode = i18next.language;
const localeCode = i18next.resolvedLanguage || 'en';
const nots = notifTimes.map((n) => {
const nDate = n.toJSDate();
const seconds = nDate.getTime() / 1000; // the id must be in seconds, otherwise the sorting won't work
Expand Down
2 changes: 1 addition & 1 deletion www/js/splash/storeDeviceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function storeDeviceSettings() {
.getVersionNumber()
.then((appver) => {
const updateJSON = {
phone_lang: i18next.language,
phone_lang: i18next.resolvedLanguage,
curr_platform: window['cordova'].platformId,
manufacturer: window['device'].manufacturer,
client_os_version: window['device'].version,
Expand Down
2 changes: 1 addition & 1 deletion www/js/survey/enketo/AddNoteButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const AddNoteButton = ({ timelineEntry, notesConfig, storeKey }: Props) => {

useEffect(() => {
let newLabel: string;
const localeCode = i18n.language;
const localeCode = i18n.resolvedLanguage || 'en';
if (notesConfig?.['filled-in-label'] && notesFor(timelineEntry)?.length) {
newLabel = notesConfig?.['filled-in-label']?.[localeCode];
setDisplayLabel(newLabel);
Expand Down
2 changes: 1 addition & 1 deletion www/js/survey/enketo/enketoHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const LABEL_FUNCTIONS = {
const configSurveys = appConfig.survey_info.surveys;

const config = configSurveys[name]; // config for this survey
const lang = i18next.language;
const lang = i18next.resolvedLanguage || 'en';
const labelTemplate = config.labelTemplate?.[lang];

if (!labelTemplate) return 'Answered'; // no template given in config
Expand Down
2 changes: 1 addition & 1 deletion www/js/survey/multilabel/confirmHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getLabelOptions(appConfigParam?) {
}
/* fill in the translations to the 'text' fields of the labelOptions,
according to the current language */
const lang = i18next.language;
const lang = i18next.resolvedLanguage || 'en';
for (const opt in labelOptions) {
labelOptions[opt]?.forEach?.((o, i) => {
const translationKey = o.value;
Expand Down

0 comments on commit 633db15

Please sign in to comment.