using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEditor.Localization; using UnityEngine; using UnityEngine.Localization.Metadata; using UnityEngine.Localization.Tables; namespace Crowdin.Plugin.Helpers.UnityLocalization { using Models.Translations; using Models.Translations.Items; using Server_API.Helpers; public static class ExportDataHelper { public static bool ParseStringRowToCrowdinTranslation( LocalizationTableCollection.Row row, string tableName, string context) { var textKey = row.KeyEntry.Key; string sourceText = null; for (var i = 0; i < row.TableEntriesReference.Length; i++) { if (row.TableEntries[i] is null) continue; var locale = row.TableEntriesReference[i]; var code = UnityLocalizationHelper.GetLanguageCode(locale); if (code == SettingsHelper.GetSourceLanguage()) { sourceText = row.TableEntries[i].LocalizedValue; } } if (sourceText is null) { return false; } var translation = new TranslationTextObj( row.KeyEntry.Id, textKey, sourceText, new List(), ContextEntryIdHelper.AppendOrUpdateEntryIdInContextString(context, row.KeyEntry.Id), tableName ); return TranslationsHelper.TryAddOrUpdateTranslation(translation); } public static bool UpdateTranslationFromStringsTableRow( LocalizationTableCollection.Row row, string tableName, string context, out IReadOnlyList localesWithTranslation) { var translations = new List(); var localesOut = new List(); var textKey = row.KeyEntry.Key; var sourceSting = ""; for (var i = 0; i < row.TableEntriesReference.Length; i++) { if (row.TableEntries[i] is null) continue; var locale = row.TableEntriesReference[i]; var code = UnityLocalizationHelper.GetLanguageCode(locale); if (code != SettingsHelper.GetSourceLanguage()) { translations.Add(new TranslationTextItem { language = code, text = row.TableEntries[i].LocalizedValue }); if (!string.IsNullOrWhiteSpace(row.TableEntries[i].LocalizedValue)) { localesOut.Add(code); } } else { sourceSting = row.TableEntries[i].LocalizedValue; } } localesWithTranslation = localesOut; return TranslationsHelper.TryAddOrUpdateTranslation(new TranslationTextObj( row.KeyEntry.Id, textKey, sourceSting, translations, ContextEntryIdHelper.AppendOrUpdateEntryIdInContextString(context, row.KeyEntry.Id), tableName) ); } public static bool ParseAssetRowToCrowdinTranslation(LocalizationTableCollection.Row row, string tableName) { var translations = new List(); for (var i = 0; i < row.TableEntriesReference.Length; i++) { var locale = row.TableEntriesReference[i]; if (row.TableEntries[i] is null) { continue; } var path = AssetDatabase.GUIDToAssetPath(row.TableEntries[i].Guid); var loadedAsset = AssetDatabase.LoadAssetAtPath(path); if (loadedAsset is null) { continue; } translations.Add(new TranslationAssetItem { language = UnityLocalizationHelper.GetLanguageCode(locale), asset = loadedAsset }); } if (translations.Count is 0) return false; var translation = new TranslationAssetObj(row.KeyEntry.Key, translations, tableName); return TranslationsHelper.TryAddOrUpdateTranslation(translation); } } }