22using UnityEngine ;
33using UnityEditor ;
44using Tsgcpp . Localization . Extension . Editor . Google ;
5+ using System . IO ;
56
67namespace 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 = GetServiceAccountSheetsServiceProviderFromKeyJson ( bundle ) ;
52+ bundle . PullAllLocales ( provider ) ;
53+ }
54+
4155 #endregion
4256
4357 #region Push
@@ -66,15 +80,72 @@ 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 = GetServiceAccountSheetsServiceProviderFromKeyJson ( bundle ) ;
95+ bundle . PushAllLocales ( provider ) ;
96+ }
97+
6998 #endregion
7099
100+ #region Service Account Key from Environment Variable
101+
102+ internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServiceProvider (
103+ StringTableCollectionBundle bundle )
104+ {
105+ const string EnvironmentGoogleServiceAccountKey = "UNITY_LOCALIZATION_GOOGLE_SERVICE_ACCOUNT_KEY" ;
106+ return GetServiceAccountSheetsServiceProvider ( bundle , EnvironmentGoogleServiceAccountKey ) ;
107+ }
108+
71109 internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServiceProvider (
110+ StringTableCollectionBundle bundle ,
111+ string keyEnvironmentVariableName )
112+ {
113+ var serviceAccountKeyJson = Environment . GetEnvironmentVariable ( keyEnvironmentVariableName ) ;
114+ if ( string . IsNullOrEmpty ( serviceAccountKeyJson ) )
115+ {
116+ throw new InvalidOperationException ( $ "Environment variable \" { keyEnvironmentVariableName } \" is not set.") ;
117+ }
118+
119+ var provider = new ServiceAccountSheetsServiceProvider (
120+ serviceAccountKeyJson : serviceAccountKeyJson ,
121+ applicationName : bundle . SheetsServiceProvider . ApplicationName ) ;
122+ return provider ;
123+ }
124+
125+ #endregion
126+
127+ #region Service Account Key from Json File
128+
129+ internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServiceProviderFromKeyJson (
72130 StringTableCollectionBundle bundle )
73131 {
74- var serviceAccountKeyJson = Environment . GetEnvironmentVariable ( EnvironmentGoogleServiceAccountKey ) ;
132+ const string JsonKeyPath = "SecretCache/UnityLocalizationExtension/service-account-key.json" ;
133+ return GetServiceAccountSheetsServiceProviderFromKeyJson ( bundle , JsonKeyPath ) ;
134+ }
135+
136+ internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServiceProviderFromKeyJson (
137+ StringTableCollectionBundle bundle ,
138+ string keyJsonPath )
139+ {
140+ if ( ! File . Exists ( keyJsonPath ) )
141+ {
142+ throw new InvalidOperationException ( $ "File \" { keyJsonPath } \" is not found.") ;
143+ }
144+
145+ string serviceAccountKeyJson = File . ReadAllText ( keyJsonPath ) ;
75146 if ( string . IsNullOrEmpty ( serviceAccountKeyJson ) )
76147 {
77- throw new InvalidOperationException ( $ "Environment variable \" { EnvironmentGoogleServiceAccountKey } \" is not set .") ;
148+ throw new InvalidOperationException ( $ "File \" { keyJsonPath } \" is empty .") ;
78149 }
79150
80151 var provider = new ServiceAccountSheetsServiceProvider (
@@ -83,6 +154,8 @@ internal static ServiceAccountSheetsServiceProvider GetServiceAccountSheetsServi
83154 return provider ;
84155 }
85156
157+ #endregion
158+
86159 private const string BundlePath = "Assets/Example/StringTableCollectionBundle/StringTableCollectionBundle.asset" ;
87160
88161 public static StringTableCollectionBundle Bundle => GetStringTableCollectionBundle ( BundlePath ) ;
@@ -97,6 +170,5 @@ private static StringTableCollectionBundle GetStringTableCollectionBundle(string
97170
98171 return bundle ;
99172 }
100-
101173 }
102174}
0 commit comments