Skip to content

Commit b4fd5f9

Browse files
committed
feat: Add "Pull/Push-AllLocalizationTablesWithGoogleServiceAccount"
1 parent ee275d5 commit b4fd5f9

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

Assets/Example/Editor/ExampleLocalizationSynchronizationMenu.cs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using UnityEngine;
33
using UnityEditor;
44
using Tsgcpp.Localization.Extension.Editor.Google;
5+
using System.IO;
56

67
namespace Tsgcpp.Localization.Extension.Example.Editor
78
{
@@ -12,8 +13,6 @@ public static class ExampleLocalizationSynchronizationMenu
1213
/// <summary>
1314
/// Environment variable name for Google Sheets API key (Json format).
1415
/// </summary>
15-
private const string EnvironmentGoogleServiceAccountKey = "UNITY_LOCALIZATION_GOOGLE_SERVICE_ACCOUNT_KEY";
16-
1716
[MenuItem("Localization Extension Example/Pull All Localization Tables", false, priority = 1)]
1817
internal static void PullAllLocalizationTablesMenu()
1918
{
@@ -38,6 +37,21 @@ internal static void PullAllLocalizationTablesWithGoogleServiceAccount()
3837
bundle.PullAllLocales(provider);
3938
}
4039

40+
/// <summary>
41+
/// Pull all StringTables in StringTableCollectionBundle from Google Spreadsheet.
42+
/// </summary>
43+
/// <remarks>
44+
/// This method can also be used in CI.
45+
/// This is for environments that cannot use environment variables.
46+
/// FYI: GameCI cannot use additional environemnt variables.
47+
/// </remarks>
48+
internal static void PullAllLocalizationTablesFromTempKeyJson()
49+
{
50+
var bundle = Bundle;
51+
var provider = GetServiceAccountSheetsServiceProviderFromTempKeyJson(bundle);
52+
bundle.PullAllLocales(provider);
53+
}
54+
4155
#endregion
4256

4357
#region Push
@@ -66,8 +80,27 @@ internal static void PushAllLocalizationTablesWithGoogleServiceAccount()
6680
bundle.PushAllLocales(provider);
6781
}
6882

83+
/// <summary>
84+
/// Push all StringTables in StringTableCollectionBundle to Google Spreadsheet.
85+
/// </summary>
86+
/// <remarks>
87+
/// This method can also be used in CI.
88+
/// This is for environments that cannot use environment variables.
89+
/// FYI: GameCI cannot use additional environemnt variables.
90+
/// </remarks>
91+
internal static void PushAllLocalizationTablesWithGoogleServiceAccountFromTempKeyJson()
92+
{
93+
var bundle = Bundle;
94+
var provider = GetServiceAccountSheetsServiceProviderFromTempKeyJson(bundle);
95+
bundle.PushAllLocales(provider);
96+
}
97+
6998
#endregion
7099

100+
#region Service Account Key from Environment Variable
101+
102+
private const string EnvironmentGoogleServiceAccountKey = "UNITY_LOCALIZATION_GOOGLE_SERVICE_ACCOUNT_KEY";
103+
71104
internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServiceProvider(
72105
StringTableCollectionBundle bundle)
73106
{
@@ -83,6 +116,34 @@ internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServi
83116
return provider;
84117
}
85118

119+
#endregion
120+
121+
#region Service Account Key from Temp Json
122+
123+
private const string TempJsonKeyPath = "Temp/UnityLocalizationExtension/service-account-key.json";
124+
125+
internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServiceProviderFromTempKeyJson(
126+
StringTableCollectionBundle bundle)
127+
{
128+
if (!File.Exists(TempJsonKeyPath))
129+
{
130+
throw new InvalidOperationException($"File \"{TempJsonKeyPath}\" is not found.");
131+
}
132+
133+
string serviceAccountKeyJson = File.ReadAllText(TempJsonKeyPath);
134+
if (string.IsNullOrEmpty(serviceAccountKeyJson))
135+
{
136+
throw new InvalidOperationException($"File \"{TempJsonKeyPath}\" is empty.");
137+
}
138+
139+
var provider = new ServiceAccountSheetsServiceProvider(
140+
serviceAccountKeyJson: serviceAccountKeyJson,
141+
applicationName: bundle.SheetsServiceProvider.ApplicationName);
142+
return provider;
143+
}
144+
145+
#endregion
146+
86147
private const string BundlePath = "Assets/Example/StringTableCollectionBundle/StringTableCollectionBundle.asset";
87148

88149
public static StringTableCollectionBundle Bundle => GetStringTableCollectionBundle(BundlePath);
@@ -97,6 +158,5 @@ private static StringTableCollectionBundle GetStringTableCollectionBundle(string
97158

98159
return bundle;
99160
}
100-
101161
}
102162
}

0 commit comments

Comments
 (0)