using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using UnityEditor.Localization; using UnityEngine; using UnityEngine.Localization.Tables; namespace Crowdin.Plugin.Operations { using Helpers; using Helpers.FileHelpers; using Helpers.PluginLocalization; using Helpers.UnityLocalization; using Models; using Server_API; using Server_API.Helpers; using Server_API.Serializable; /// Pushes source strings (no translations) to Crowdin. Behaves differently depending on the /// project type: string-based projects only ever exchange CSV files directly; file-based /// projects keep one source file per table under the plugin's root directory. public static class PushStrings { public static Task RunAsync( PushStringsOptions options, IProgress progress, CancellationToken cancellationToken) { progress.Report(new OperationProgress(LocalizationHelper.GetLocalStringByKey(LocalKeys.start_parsing_str))); return SettingsHelper.GetProjectType() is ProjectType.StringBased ? PushStringBasedAsync(options, progress, cancellationToken) : PushFileBasedAsync(options, progress, cancellationToken); } private static async Task PushStringBasedAsync( PushStringsOptions options, IProgress progress, CancellationToken cancellationToken) { var perTable = new Dictionary(); int totalSent = 0; int totalRows = 0; foreach (string tableName in options.Tables) { TranslationsHelper.RefreshAllStringsTranslation(); StringTableCollection tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableName); progress.Report(new OperationProgress(string.Empty)); progress.Report(new OperationProgress( LocalizationHelper.GetLocalStringByKey(LocalKeys.upload_str_table, tableName))); int sentForTable = 0; foreach (LocalizationTableCollection.Row row in tableCollection.GetRowEnumerator()) { totalRows++; string context = tableCollection.GetSharedEntryComments(row.KeyEntry.Id); if (ExportDataHelper.ParseStringRowToCrowdinTranslation(row, tableName, context)) { sentForTable++; } } bool uploaded = await StringBasedProjectsHelper.UploadStringBasedProject( new StringBasedProjectsHelper.UploadProjectCommand { TableName = tableName, SelectedBranchId = options.BranchId, UpdateStrings = options.UpdateStrings, CleanupMode = options.CleanupMode, UpdateOption = options.UpdateOptionApiKey, IncludeTranslations = false }, progress, cancellationToken); if (!uploaded) { continue; } progress.Report(new OperationProgress(LocalizationHelper.GetLocalStringByKey( LocalKeys.table_exported_strings, tableName, sentForTable.ToString()))); perTable[tableName] = sentForTable; totalSent += sentForTable; } return new PushStringsResult(totalSent, totalRows, perTable); } private static async Task PushFileBasedAsync( PushStringsOptions options, IProgress progress, CancellationToken cancellationToken) { int rootDirId = await DirectoriesHelper.EnsureRootDirectoryAsync(cancellationToken); var perTable = new Dictionary(); int totalSent = 0; int totalRows = 0; foreach (string tableName in options.Tables) { TranslationsHelper.RefreshAllStringsTranslation(); StringTableCollection tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableName); int sentForTable = 0; foreach (LocalizationTableCollection.Row row in tableCollection.GetRowEnumerator()) { totalRows++; string context = tableCollection.GetSharedEntryComments(row.KeyEntry.Id); if (ExportDataHelper.ParseStringRowToCrowdinTranslation(row, tableName, context)) { sentForTable++; } } string fileName = $"{tableName}.csv"; string filepath = $"{FolderHelper.resourcesDirectory}/{fileName}"; CreateFileHelper.SaveTranslationsToFile(filepath, tableName, withTranslations: false); progress.Report(new OperationProgress(string.Empty)); progress.Report(new OperationProgress( LocalizationHelper.GetLocalStringByKey(LocalKeys.upload_str_table, tableName))); FileData fileData; try { fileData = await StorageHelper.UploadSourceFileAsync( fileName, fileName, filepath, rootDirId, includeTranslationsInFile: false, cancellationToken, options.UpdateOptionApiKey); } catch (CrowdinApiException ex) { Debug.LogException(ex); progress.Report(new OperationProgress(LocalizationHelper.GetLocalStringByException(ex))); continue; } finally { RemoveFileHelper.RemoveFile(filepath); } string message = fileData.createdAt != fileData.updatedAt ? LocalizationHelper.GetLocalStringByKey(LocalKeys.file_updated, tableName) : LocalizationHelper.GetLocalStringByKey(LocalKeys.file_created, tableName); progress.Report(new OperationProgress( LocalizationHelper.GetLocalStringByKey(LocalKeys.send_successful, message))); progress.Report(new OperationProgress(LocalizationHelper.GetLocalStringByKey( LocalKeys.table_exported_strings, tableName, sentForTable.ToString()))); perTable[tableName] = sentForTable; totalSent += sentForTable; } return new PushStringsResult(totalSent, totalRows, perTable); } } }