updateTranslations static method

Future<TranslationsUpdateResult> updateTranslations()

Updates existing translations with the ones from the Localizely platform.

This method should be called after localization delegates initialization.

Localizely.updateTranslations().then((response) => setState(() => print('Translations fetched')), onError: (error) => print('Error occurred'));

Throws SdkException in case sdk token, distribution id, metadata, or application version is not set or can't be detected. Throws ApiException in case of http request failure.

Implementation

static Future<TranslationsUpdateResult> updateTranslations() async {
  if (_sdkToken == null) {
    throw SdkException(
        'Localizely SDK has not been initialized or SDK token has not been provided during SDK initialization.');
  }

  if (_distributionId == null) {
    throw SdkException(
        'Localizely SDK has not been initialized or distribution ID has not been provided during SDK initialization.');
  }

  if (!Util.isMetadataSet()) {
    throw SdkException(
        "Localizely SDK missing metadata configuration. In case you are using the Flutter Intl IDE plugin for localization, please ensure 'ota_enabled' is set to 'true' within the 'flutter_intl/localizely' section of the 'pubspec.yaml' file. In case you are using the gen_l10n tool for localization, please ensure that the required localization code is generated via 'flutter pub run localizely_sdk:generate' command. If all the requirements are met and you still see this error, please ensure that the 'updateTranslations' method is called after localization delegates initialization.");
  }

  var appBuildNumber =
      SdkData.appBuildNumber ?? await Util.getAppBuildNumber();
  if (appBuildNumber == null) {
    throw SdkException(
        "The application version can't be detected. Please use the 'setAppVersion' method for setting up the required parameter.");
  }

  if (!SdkData.hasReleaseData) {
    SdkData.releaseData =
        await LabelsService.getPersistedReleaseData(_distributionId!);
  }

  var currentLocale = Util.getCurrentLocale();
  var appInstallationId = await Store.getAppInstallationId();
  var sdkBuildNumber = util.Util.getSdkBuildNumber();
  var deviceLocale = Util.getDeviceLocale();
  var currentReleaseVersion = SdkData.releaseVersion;

  var newReleaseVersion = await LabelsService.getLabels(
      _sdkToken!,
      _distributionId!,
      currentLocale,
      appInstallationId,
      sdkBuildNumber,
      appBuildNumber,
      deviceLocale: deviceLocale,
      preRelease: _preRelease,
      releaseVersion: currentReleaseVersion);

  return TranslationsUpdateResult(currentReleaseVersion, newReleaseVersion);
}