Skip to content

Allow registering dynamic translations #471

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

LiamMorrow
Copy link
Contributor

@LiamMorrow LiamMorrow commented May 10, 2025

In some use cases, other i18n solutions manage translations. In these cases, it is desirable to resolve the translations dynamically.

For example, I use tolgee as a way to create and manage translations. It already deals with resolving the locale, and potentially pulling new translations from a remote server. As such, it is not feasible to know the translations at build or app start time, or even resolve them immediately without using a fallback.

This PR just adds a mechanism to provide a function to the registerTranslation function, which will be called when getting the translation.

In my application, this looks like this:

import { registerTranslation } from 'react-native-paper-dates';
import { tolgee } from '@/services/tolgee';

registerTranslation('default', () => {
  const t = tolgee.t;
  return {
  // Note that if I created this object at app start, these dynamic values would not update or use tolgees fallback while it resolved them
    save: t('Save'),
    selectSingle: t('SelectDate'),
    selectMultiple: t('SelectDates'),
    selectRange: t('SelectPeriod'),
    notAccordingToDateFormat: (inputFormat) =>
      t(`DateFormatMustBe{inputFormat}`, { inputFormat: inputFormat }),
    mustBeHigherThan: (date) => t(`MustBeLaterThan{date}`, { date }),
    mustBeLowerThan: (date) => t(`MustBeEarlierThan{date}`, { date }),
    mustBeBetween: (startDate, endDate) =>
      t(`MustBeBetween{startDate}-{endDate}`, { startDate, endDate }),
    dateIsDisabled: t('DayIsNotAllowed'),
    previous: t('Previous'),
    next: t('Next'),
    typeInDate: t('TypeInDate'),
    pickDateFromCalendar: t('PickDateFromCalendar'),
    close: t('Close'),
    minute: t('Minute'),
    hour: t('Hour'),
  };
});

In some use cases, other i18n solutions manage translations (e.g. Tolgee). In these cases, it is desirable to resolve the translations dynamically
@iM-GeeKy iM-GeeKy requested a review from RichardLindhout May 10, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant