PostProcessBuild_IOS.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #if UNITY_IOS || UNITY_IPHONE
  2. using UnityEditor.Callbacks;
  3. using System.Collections;
  4. using UnityEditor.iOS_I2Loc.Xcode;
  5. using System.IO;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using System.Linq;
  9. namespace I2.Loc
  10. {
  11. public class PostProcessBuild_IOS
  12. {
  13. [PostProcessBuild(10000)]
  14. public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
  15. {
  16. if (buildTarget != BuildTarget.iOS)
  17. return;
  18. if (LocalizationManager.Sources.Count <= 0)
  19. LocalizationManager.UpdateSources();
  20. var langCodes = LocalizationManager.GetAllLanguagesCode(false).Concat(LocalizationManager.GetAllLanguagesCode(true)).Distinct().ToList();
  21. if (langCodes.Count <= 0)
  22. return;
  23. try
  24. {
  25. //----[ Export localized languages to the info.plist ]---------
  26. string plistPath = pathToBuiltProject + "/Info.plist";
  27. PlistDocument plist = new PlistDocument();
  28. plist.ReadFromString(File.ReadAllText(plistPath));
  29. PlistElementDict rootDict = plist.root;
  30. // Get Language root
  31. var langArray = rootDict.CreateArray("CFBundleLocalizations");
  32. // Set the Language Codes
  33. foreach (var code in langCodes)
  34. {
  35. if (code == null || code.Length < 2)
  36. continue;
  37. langArray.AddString(code);
  38. }
  39. rootDict.SetString("CFBundleDevelopmentRegion", langCodes[0]);
  40. // Write to file
  41. File.WriteAllText(plistPath, plist.WriteToString());
  42. //--[ Localize App Name ]----------
  43. string LocalizationRoot = pathToBuiltProject + "/I2Localization";
  44. if (!Directory.Exists(LocalizationRoot))
  45. Directory.CreateDirectory(LocalizationRoot);
  46. var project = new PBXProject();
  47. string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
  48. //if (!projPath.EndsWith("xcodeproj"))
  49. //projPath = projPath.Substring(0, projPath.LastIndexOfAny("/\\".ToCharArray()));
  50. project.ReadFromFile(projPath);
  51. //var targetName = PBXProject.GetUnityTargetName();
  52. //string projBuild = project.TargetGuidByName( targetName );
  53. project.RemoveLocalizationVariantGroup("I2 Localization");
  54. // Set the Language Overrides
  55. foreach (var code in langCodes)
  56. {
  57. if (code == null || code.Length < 2)
  58. continue;
  59. var LanguageDirRoot = LocalizationRoot + "/" + code + ".lproj";
  60. if (!Directory.Exists(LanguageDirRoot))
  61. Directory.CreateDirectory(LanguageDirRoot);
  62. var infoPlistPath = LanguageDirRoot + "/InfoPlist.strings";
  63. var InfoPlist = string.Format("CFBundleDisplayName = \"{0}\";", LocalizationManager.GetAppName(code));
  64. File.WriteAllText(infoPlistPath, InfoPlist);
  65. var langProjectRoot = "I2Localization/"+code+".lproj";
  66. var stringPaths = LanguageDirRoot + "/Localizable.strings";
  67. File.WriteAllText(stringPaths, string.Empty);
  68. project.AddLocalization(langProjectRoot + "/Localizable.strings", langProjectRoot + "/Localizable.strings", "I2 Localization");
  69. project.AddLocalization(langProjectRoot + "/InfoPlist.strings", langProjectRoot + "/InfoPlist.strings", "I2 Localization");
  70. }
  71. project.WriteToFile(projPath);
  72. }
  73. catch (System.Exception e)
  74. {
  75. Debug.Log (e);
  76. }
  77. }
  78. }
  79. }
  80. #endif