Example #1
0
        //[PostProcessBuild]
        public static void PostProcessAndroid(BuildTarget buildTarget, string pathToBuiltProject)
        {
            if (buildTarget != BuildTarget.Android && buildTarget != BuildTarget.Tizen)
            {
                return;
            }

            if (LocalizationManager.Sources.Count <= 0)
            {
                LocalizationManager.UpdateSources();
            }
            var langCodes = LocalizationManager.GetAllLanguagesCode(false);

            if (langCodes.Count <= 0)
            {
                return;
            }
            string stringXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                               "<resources>\n" +
                               "    <string name=\"app_name\">{0}</string>\n" +
                               "</resources>";

            SetStringsFile(pathToBuiltProject + "/res/values", "strings.xml", stringXML, LocalizationManager.GetAppName(langCodes[0]));


            var list = new List <string>();

            list.Add(pathToBuiltProject + "/res/values");
            foreach (var code in langCodes)
            {
                string dir = pathToBuiltProject + "/res/values-" + code;

                SetStringsFile(dir, "strings.xml", stringXML, LocalizationManager.GetAppName(code));
            }
        }
        //[PostProcessBuild(10000)]
        public static void PostProcessAndroid(BuildTarget buildTarget, string pathToBuiltProject)
        {
            if (buildTarget != BuildTarget.Android && buildTarget != BuildTarget.Tizen)
            {
                return;
            }

            if (LocalizationManager.Sources.Count <= 0)
            {
                LocalizationManager.UpdateSources();
            }

            // Get language with variants, but also add it without the variant to allow fallbacks (e.g. en-CA also adds en)
            var langCodes = LocalizationManager.GetAllLanguagesCode(false).Concat(LocalizationManager.GetAllLanguagesCode(true)).Distinct().ToList();

            if (langCodes.Count <= 0)
            {
                return;
            }
            string stringXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                               "<resources>\n" +
                               "    <string name=\"app_name\">{0}</string>\n" +
                               "</resources>";

            SetStringsFile(pathToBuiltProject + "/res/values", "strings.xml", stringXML, LocalizationManager.GetAppName(langCodes[0]));


            var list = new List <string>();

            list.Add(pathToBuiltProject + "/res/values");
            foreach (var code in langCodes)
            {
                // Android doesn't use zh-CN or zh-TW, instead it uses: zh-rCN, zh-rTW, zh
                string fixedCode = code;
                if (fixedCode.StartsWith("zh", System.StringComparison.OrdinalIgnoreCase))
                {
                    string googleCode = GoogleLanguages.GetGoogleLanguageCode(fixedCode);
                    if (googleCode == null)
                    {
                        googleCode = fixedCode;
                    }
                    fixedCode = (googleCode == "zh-CN") ? "zh-CN" : "zh";
                }
                fixedCode = fixedCode.Replace("-", "-r");

                string dir = pathToBuiltProject + "/res/values-" + fixedCode;

                SetStringsFile(dir, "strings.xml", stringXML, LocalizationManager.GetAppName(code));
            }
        }
Example #3
0
        public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
        {
            if (buildTarget != BuildTarget.iOS)
            {
                return;
            }

            if (LocalizationManager.Sources.Count <= 0)
            {
                LocalizationManager.UpdateSources();
            }
            var langCodes = LocalizationManager.GetAllLanguagesCode(false).Concat(LocalizationManager.GetAllLanguagesCode(true)).Distinct().ToList();

            if (langCodes.Count <= 0)
            {
                return;
            }

            Debug.Log(String.Join(" ", langCodes));

            try
            {
                //----[ Export localized languages to the info.plist ]---------

                string        plistPath = pathToBuiltProject + "/Info.plist";
                PlistDocument plist     = new PlistDocument();
                plist.ReadFromString(File.ReadAllText(plistPath));

                PlistElementDict rootDict = plist.root;

                // Get Language root
                var langArray = rootDict.CreateArray("CFBundleLocalizations");

                // Set the Language Codes
                foreach (var code in langCodes)
                {
                    if (code == null || code.Length < 2)
                    {
                        continue;
                    }
                    langArray.AddString(code);
                }

                rootDict.SetString("CFBundleDevelopmentRegion", langCodes[0]);

                // Write to file
                File.WriteAllText(plistPath, plist.WriteToString());

                //--[ Localize App Name ]----------

                string LocalizationRoot = pathToBuiltProject + "/I2Localization";
                if (!Directory.Exists(LocalizationRoot))
                {
                    Directory.CreateDirectory(LocalizationRoot);
                }

                var    project  = new PBXProject();
                string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
                //if (!projPath.EndsWith("xcodeproj"))
                //projPath = projPath.Substring(0, projPath.LastIndexOfAny("/\\".ToCharArray()));

                project.ReadFromFile(projPath);
                //var targetName = PBXProject.GetUnityTargetName();
                //string projBuild = project.TargetGuidByName( targetName );

                project.RemoveLocalizationVariantGroup("I2 Localization");
                // Set the Language Overrides
                foreach (var code in langCodes)
                {
                    Debug.Log($"POST PROCESS BUILD IOS LANGCODE {code}");

                    if (code == null || code.Length < 2)
                    {
                        continue;
                    }

                    var LanguageDirRoot = LocalizationRoot + "/" + code + ".lproj";
                    if (!Directory.Exists(LanguageDirRoot))
                    {
                        Directory.CreateDirectory(LanguageDirRoot);
                    }

                    var           infoPlistPath     = LanguageDirRoot + "/InfoPlist.strings";
                    List <string> infoPlistContents = new List <string>
                    {
                        string.Format("CFBundleDisplayName = \"{0}\";", LocalizationManager.GetAppName(code))
                    };

                    foreach (KeyValuePair <string, string> entry in plistStringMap)
                    {
                        string translation = GetTranslationForLanguage(entry.Value, code);

                        if (!string.IsNullOrEmpty(translation))
                        {
                            infoPlistContents.Add($"{entry.Key} = \"{translation}\";");
                        }
                    }

                    string InfoPlist = String.Join(Environment.NewLine, infoPlistContents.ToArray()) + Environment.NewLine;

                    File.WriteAllText(infoPlistPath, InfoPlist);

                    var langProjectRoot = "I2Localization/" + code + ".lproj";

                    var stringPaths = LanguageDirRoot + "/Localizable.strings";
                    File.WriteAllText(stringPaths, string.Empty);

                    project.AddLocalization(langProjectRoot + "/Localizable.strings", langProjectRoot + "/Localizable.strings", "I2 Localization");
                    project.AddLocalization(langProjectRoot + "/InfoPlist.strings", langProjectRoot + "/InfoPlist.strings", "I2 Localization");
                }

                project.WriteToFile(projPath);
            }
            catch (System.Exception e)
            {
                Debug.LogError(e);
            }
        }
Example #4
0
        public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
        {
            if (buildTarget != BuildTarget.iOS)
            {
                return;
            }

            if (LocalizationManager.Sources.Count <= 0)
            {
                LocalizationManager.UpdateSources();
            }
            var langCodes = LocalizationManager.GetAllLanguagesCode(false);

            if (langCodes.Count <= 0)
            {
                return;
            }

            try
            {
                //----[ Export localized languages to the info.plist ]---------

                string        plistPath = pathToBuiltProject + "/Info.plist";
                PlistDocument plist     = new PlistDocument();
                plist.ReadFromString(File.ReadAllText(plistPath));

                PlistElementDict rootDict = plist.root;

                // Get Language root
                var langArray = rootDict.CreateArray("CFBundleLocalizations");

                // Set the Language Codes
                foreach (var code in langCodes)
                {
                    if (code == null || code.Length < 2)
                    {
                        continue;
                    }
                    langArray.AddString(code);
                }

                rootDict.SetString("CFBundleDevelopmentRegion", langCodes[0]);

                // Write to file
                File.WriteAllText(plistPath, plist.WriteToString());

                //--[ Localize App Name ]----------

                string LocalizationRoot = pathToBuiltProject + "/I2Localization";
                if (!Directory.Exists(LocalizationRoot))
                {
                    Directory.CreateDirectory(LocalizationRoot);
                }

                var    project  = new PBXProject();
                string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
                //if (!projPath.EndsWith("xcodeproj"))
                //projPath = projPath.Substring(0, projPath.LastIndexOfAny("/\\".ToCharArray()));

                project.ReadFromFile(projPath);
                //var targetName = PBXProject.GetUnityTargetName();
                //string projBuild = project.TargetGuidByName( targetName );

                // Set the Language Overrides
                foreach (var code in langCodes)
                {
                    if (code == null || code.Length < 2)
                    {
                        continue;
                    }

                    var LanguageDirRoot = LocalizationRoot + "/" + code + ".lproj";
                    if (!Directory.Exists(LanguageDirRoot))
                    {
                        Directory.CreateDirectory(LanguageDirRoot);
                    }

                    var infoPlistPath = LanguageDirRoot + "/InfoPlist.strings";
                    var InfoPlist     = string.Format("CFBundleDisplayName = \"{0}\";", LocalizationManager.GetAppName(code));
                    File.WriteAllText(infoPlistPath, InfoPlist);

                    var langProjectRoot = "I2Localization/" + code + ".lproj";

                    var stringPaths = LanguageDirRoot + "/Localizable.strings";
                    File.WriteAllText(stringPaths, string.Empty);

                    project.AddLocalization(langProjectRoot + "/Localizable.strings", langProjectRoot + "/Localizable.strings", "I2 Localization");
                    project.AddLocalization(langProjectRoot + "/InfoPlist.strings", langProjectRoot + "/InfoPlist.strings", "I2 Localization");
                }

                project.WriteToFile(projPath);
            }
            catch (System.Exception e)
            {
                Debug.Log(e);
            }
        }