translate function

Future<void> translate(
  1. FileSystem fileSystem,
  2. TranslateOptions options
)

Translates the ARB files in the specified directory using the given options.

The fileSystem parameter represents the file system to use for accessing the ARB files. The options parameter contains the translation options, including the model provider, API key, context, safety settings, and more.

Implementation

Future<void> translate(
  FileSystem fileSystem,
  TranslateOptions options,
) async {
  final translationDelegate = switch (options.modelProvider) {
    ModelProvider.gemini => GeminiTranslationDelegate(
        model: options.model,
        apiKey: options.apiKey,
        batchSize: options.batchSize,
        context: options.context,
        disableSafety: options.disableSafety,
        useEscaping: options.useEscaping,
        relaxSyntax: options.relaxSyntax,
      ),
    ModelProvider.vertexAi => GeminiTranslationDelegate.vertexAi(
        model: options.model,
        apiKey: options.apiKey,
        projectUrl: options.vertexAiProjectUrl!,
        batchSize: options.batchSize,
        context: options.context,
        disableSafety: options.disableSafety,
        useEscaping: options.useEscaping,
        relaxSyntax: options.relaxSyntax,
      ),
    ModelProvider.openAi => ChatGptTranslationDelegate(
        model: options.model,
        apiKey: options.apiKey,
        batchSize: options.batchSize,
        context: options.context,
        useEscaping: options.useEscaping,
        relaxSyntax: options.relaxSyntax,
      ),
    ModelProvider.customOpenAiCompatible => ChatGptTranslationDelegate.custom(
        model: options.customModel!,
        apiKey: options.apiKey,
        baseUrl: options.customModelProviderBaseUrl!,
        batchSize: options.batchSize,
        context: options.context,
        useEscaping: options.useEscaping,
        relaxSyntax: options.relaxSyntax,
      ),
  };

  final bundles =
      AppResourceBundleCollection(fileSystem.directory(options.arbDir)).bundles;
  final templateBundle = bundles.firstWhere(
      (bundle) => bundle.file.path.endsWith(options.templateArbFile));

  for (final bundle in bundles.where((bundle) => bundle != templateBundle)) {
    if (options.excludeLocales?.contains(bundle.locale.toString()) ?? false) {
      print('Skiping excluded locale ${bundle.locale}');

      continue;
    }

    await _translateBundle(
      translationDelegate: translationDelegate,
      templateBundle: templateBundle,
      bundle: bundle,
    );
  }
}