Unity iOS 语言本地化(unity本地导出iOS本地化.strings文件)

缘由:因为公司2 3D 共用一套iOS底层框架 一直以来都用拷贝的方法 取所需要的代码 导致一个问题 iOS底层拷贝过来的xxx.strings本地化文件 一直无法被编译 即使被编译 也无法达到效果 导致一系列问题
现在只需要把语言文件放在unity项目中就好了 如 Assets/babyframework/localizationForiOS

直接拷贝效果

image.png

采用插件导出后效果

image.png

粘贴关键代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.iOS.Xcode;

public class NativeLocale
{
    public static void AddLocalizedStringsIOS(string projectPath, string localizedDirectoryPath)
    {        
        DirectoryInfo dir = new DirectoryInfo(localizedDirectoryPath);
        if(!dir.Exists)
            return;

        List<string> locales = new List<string>();
        var localeDirs = dir.GetDirectories("*.lproj", SearchOption.TopDirectoryOnly);

        foreach(var sub in localeDirs)
            locales.Add(Path.GetFileNameWithoutExtension(sub.Name));

        AddLocalizedStringsIOS(projectPath, localizedDirectoryPath, locales);
    }

    public static void AddLocalizedStringsIOS(string projectPath, string localizedDirectoryPath, List<string> validLocales)
    {
        string projPath = projectPath + "/Unity-iPhone.xcodeproj/project.pbxproj";
        PBXProject proj = new PBXProject();
        proj.ReadFromFile(projPath);

        foreach(var locale in validLocales)
        {
            // copy contents in the localization directory to project directory
            string src = Path.Combine(localizedDirectoryPath, locale + ".lproj");
            DirectoryCopy(src, Path.Combine(projectPath, "Unity-iPhone/" + locale + ".lproj"));

            string fileRelatvePath = string.Format("Unity-iPhone/{0}.lproj/Localizable.strings", locale);
            proj.AddLocalization("Localizable.strings", locale, fileRelatvePath);
        }

        proj.WriteToFile(projPath);
    }


    private static void DirectoryCopy(string sourceDirName, string destDirName)
    {
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
            return;

        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        FileInfo[] files = dir.GetFiles();

        foreach (FileInfo file in files)
        {
            // skip unity meta files
            if(file.FullName.EndsWith(".meta"))
                continue;
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(destDirName, subdir.Name);
            DirectoryCopy(subdir.FullName, temppath);
        }
    }
}

调用

using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using UnityEditor.iOS.Xcode;

public class PostProcess: MonoBehaviour
{
    [PostProcessBuildAttribute(9999)]
    public static void OnPostProcessBuild(BuildTarget target, string path)
    {
        if (target == BuildTarget.iOS)
        {
            OnIOSBuild(target, path);
        }
    }

    private static void OnIOSBuild(BuildTarget target, string path)
    {
        NativeLocale.AddLocalizedStringsIOS(path, Path.Combine(Application.dataPath, "BabyframeWork/localizationForiOS"));
    }
}

文件路径如下


image.png

效果

image.png

image.png

项目需要用到unity PBXProject插件 不知道的自行Google


参考的同性交友网站地址
https://github.com/zeyangl/UnityAppNameLocalizationForIOS

https://bitbucket.org/Unity-Technologies/xcodeapi

推荐阅读更多精彩内容