run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  final tId = argResults!['translation-id'] as String;
  final lId = argResults!['locale-id'] as String;
  final text = argResults!['text'] as String;
  var id = argResults?['id'] as String? ?? '';
  final rawVariant = argResults?['variant'] as String?;
  final variant =
      (rawVariant != null && rawVariant.isNotEmpty) ? rawVariant : null;
  final autoTranslate = argResults?['auto-translate'] == true;

  final client = container.read(translationLocalizationServiceClientProvider);

  if (id.isEmpty) {
    try {
      final findReq = TranslationLocaleAndVariant(
        translationId: tId,
        localeId: lId,
      );
      if (variant != null) {
        findReq.variant = variant;
      }
      final existing =
          await client.getTranslationLocalizationByTranslationLocaleAndVariant(
        findReq,
        options: getCallOptions(requireUser: false),
      );
      if (existing.id.isNotEmpty) {
        id = existing.id;
      }
    } catch (_) {
      // Record does not exist yet; proceed with empty ID to create new entry
    }
  }

  final loc = TranslationLocalization(
    translationId: tId,
    localeId: lId,
    text: text,
    autoTranslate: autoTranslate,
  );
  if (variant != null) {
    loc.variant = variant;
  }
  if (id.isNotEmpty) {
    loc.id = id;
  }

  final result = await client.upsertTranslationLocalization(
    loc,
    options: getCallOptions(requireUser: false),
  );
  printOutput(result);
  return 0;
}