2023-08-15 16:29:53 +08:00

76 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
public class IOSLocalizationTool
{
public static readonly Dictionary<SystemLanguage, string> validLanguageMap = new Dictionary<SystemLanguage, string>()
{
[SystemLanguage.English] = "en",
[SystemLanguage.ChineseSimplified] = "zh-Hans",
[SystemLanguage.ChineseTraditional] = "zh-Hant",
[SystemLanguage.Japanese] = "ja",
[SystemLanguage.Korean] = "ko",
[SystemLanguage.Spanish] = "es",
[SystemLanguage.Vietnamese] = "vi",
[SystemLanguage.Thai] = "th",
[SystemLanguage.Indonesian] = "id",
[SystemLanguage.Portuguese] = "pt",
};
public static void SetLocalization(string pathToBuiltProject)
{
#if UNITY_IOS
string buildPath = pathToBuiltProject;
const string infoPlistName = "Info.plist";
var plistPath = Path.Combine(buildPath, infoPlistName);
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
// url schemes
const string bundleLocalizationKey = "CFBundleLocalizations";
if (!plist.root.values.TryGetValue(bundleLocalizationKey, out var localizations))
{
localizations = plist.root.CreateArray(bundleLocalizationKey);
}
foreach (string value in validLanguageMap.Values)
{
localizations.AsArray().AddString(value);
}
plist.WriteToFile(plistPath);
var projectPath = PBXProject.GetPBXProjectPath(buildPath);
var project = new PBXProject();
project.ReadFromFile(projectPath);
var target = project.GetUnityMainTargetGuid();
var resourceTarget = project.GetResourcesBuildPhaseByTarget(target);
foreach (string value in validLanguageMap.Values)
{
var path = Path.Combine(Path.Combine(Application.dataPath, "../", "BFVersions/ios/ios_common"), $"{value}.lproj");
var inProjectPath = Path.GetFileName(path);
project.AddFolderReference(path, inProjectPath);
var resGUID = project.FindFileGuidByProjectPath(inProjectPath);
project.AddFileToBuildSection(
target,
resourceTarget,
resGUID);
}
project.WriteToFile(projectPath);
#endif
}
}