createTranslationFile function

String createTranslationFile(
  1. List<String> locales, {
  2. required List<String> imports,
  3. required List<String> translationMaps,
  4. bool save = true,
})

Generates the TData.dart and the specified locales translations as dart files.

Implementation

String createTranslationFile(
  List<String> locales, {
  required List<String> imports,
  required List<String> translationMaps,
  bool save = true,
}) {
  if (locales.isEmpty) {
    locales.add('en');
  }
  var classAppLocales = _buildAppLocalesFileContent(locales);
  var hasTranslationMaps = imports.isNotEmpty;
  var imports0 = imports.join('\n');
  var translateClassString = '';

  /// TData file
  final tClassName = config.dartTranslationClassname;
  var tLocalesCode = '';

  /// only add locale codes if user asks for useMaps:true
  if (hasTranslationMaps) {
    /// Translation File.
    var transKeysString = '{\n';
    transKeysString += translationMaps.join('\n');
    transKeysString += '  };\n';

    tLocalesCode = '''
  static Map<String, Map<String, String>> byKeys = getByKeys();
  static Map<String, Map<String, String>> getByKeys() => $transKeysString

  static Map<String, Map<String, String>>? _byText;
  static Map<String, Map<String, String>> get byText {
    _byText ??= getByText();
    return _byText!;
  }

  static Map<String, Map<String, String>> getByText() {
    final source = getByKeys();
    final output = <String, Map<String, String>>{};
    final master = source[AppLocales.available.first.key]!;
    for (final localeKey in source.keys) {
      output[localeKey] = mapLocaleKeysToMasterText(
        source[localeKey]!,
        masterMap: master,
      );
    }
    return output;
  }
''';
  } else {
    /// keep the fields for easy map access, if the user wants to cache
    /// the json somewhere.
    tLocalesCode = '''
  static Map<String, Map<String, String>> byKeys = {};
  static Map<String, Map<String, String>> byText = {};
''';
  }

  // only applies when `use_maps: true`
  // if (config.useDartMaps) {
  if (config.validTKeyFile) {
    translateClassString = '''
//ignore: avoid_classes_with_only_static_members
abstract class $tClassName {

  $tLocalesCode

  ${getCodeMapLocaleKeysToMasterText(tClassName)}
}''';
  }

  // final _transImportsString = transImports.join('\n');
// $_transImportsString
  var fileContent = '''

// GENERATED CODE - DO NOT MODIFY BY HAND

/// You can learn more about `flutter_translation_sheet` in the Wiki:
/// https://github.com/roipeker/flutter_translation_sheet/

// ignore_for_file: lines_longer_than_80_chars
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
$imports0

$translateClassString

$classAppLocales

$_kLangVoTemplate

/// demo widgets

$kLangPickerMaterial

$kLangPickerCupertino

''';
  if (save) {
    var filepath = config.dartTranslationPath;
    // trace('Building translations ($className) file at ', filepath);
    saveString(filepath, fileContent);
  }
  return fileContent;
}