using UnityEditor.Localization; using UnityEngine.Localization; using UnityEngine.Localization.Tables; namespace Crowdin.Plugin.Helpers.UnityLocalization { using Server_API.Helpers; public static class UnityLocalizationHelper { public static LocalizationTableCollection.Row TryGetStringTableRow(string key, string tableName) { var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections(); foreach (var collection in stringTableCollections) { if (collection.TableCollectionName != tableName) { continue; } foreach (var row in collection.GetRowEnumerator()) { if (row.KeyEntry.Key == key) { return row; } } } return null; } public static LocalizationTableCollection.Row TryGetAssetTableRow(string key, string tableName) { var stringTableCollections = LocalizationEditorSettings.GetAssetTableCollections(); foreach (var collection in stringTableCollections) { if (collection.TableCollectionName != tableName) { continue; } foreach (var row in collection.GetRowEnumerator()) { if (row.KeyEntry.Key == key) { return row; } } } return null; } public static string FindTableKeyNameByKeyId(long keyId, bool isAssetLocalize) { if (isAssetLocalize) { var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections(); foreach (var collection in assetTableCollections) { foreach (var row in collection.GetRowEnumerator()) { if (row.KeyEntry.Id == keyId) { return row.KeyEntry.Key; } } } } else { var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections(); foreach (var collection in stringTableCollections) { foreach (var row in collection.GetRowEnumerator()) { if (row.KeyEntry.Id == keyId) { return row.KeyEntry.Key; } } } } return null; } public static string GetLanguageCode(LocaleIdentifier localeCode) { var code = localeCode.Code; var languages = SettingsHelper.GetLanguageScheme(); if (localeCode.Code.Length == 2) { var twoLetterIndex = languages.FindIndex(item => item.twoLettersCode == localeCode.Code); if (twoLetterIndex > -1) { code = languages[twoLetterIndex].langType; } } else { var index = languages.FindIndex(item => item.locale == localeCode.Code); if (index > -1) { code = languages[index].langType; } } return code; } public static bool IsHavePluginAssetsTables() { var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections(); return assetTableCollections.Count != 0; } public static bool IsHavePluginStringsTables() { var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections(); return stringTableCollections.Count != 0; } } }