yasirkula / UnityNativeShare

A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS
MIT License
891 stars 131 forks source link

Does not show the export dialog in the device language on iOS #53

Closed vertis closed 4 years ago

vertis commented 4 years ago

Regardless of what the system language is (for example german/de). The native export window shows in the language of the development region (en in my case).

In playing with the Info.plist this seems to be driven by CFBundleDevelopmentRegion having a value of en. If I change it to de then the dialog shows in german. This is only partially a solution since I'm trying to localize for about 4-5 different languages.

I'm not sure if this is something that can be solved in this library or in Unity itself, but I figured I would mention it here.

vertis commented 4 years ago

On further investigation adding languages to CFBundleLocalizations fixes this problem. Which you can do with a post processing script

// Credit: https://github.com/ibrahimpenekli/GameToolkit-Localization/blob/master/Assets/GameToolkit-Localization/Editor/LocalizationBuildPostprocessor.cs

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using System.Collections.Generic;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif

public class AddLocalizations : MonoBehaviour
{
    [PostProcessBuild(9999)]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string pathToBuiltProject)
    {
#if UNITY_IOS
        if (buildTarget == BuildTarget.iOS)
        {
            // Continue if any localization info exists.
            var localizations = GetLocalizations();
            if (localizations.Count == 0)
            {
                return;
            }

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

            // Get root of plist/dict.
            var rootDict = plist.root;
            var plistLocalizations = rootDict.CreateArray("CFBundleLocalizations");

            // Add localizations.
            foreach (string locale in localizations)
            {
                plistLocalizations.AddString(locale);
                Debug.Log("[LocalizationBuildPostprocessor] Localization added: " + locale);
            }

            // Save all changes.
            File.WriteAllText(plistPath, plist.WriteToString());
        }
#endif
    }

    private static List<string> GetLocalizations()
    {
        var localizations = new List<string> { "en", "de" };
        return localizations;
    }
}

Left here for someones future reference

yasirkula commented 4 years ago

Thanks for sharing your findings. I wasn't aware of this issue but apparently, it is not convenient to solve this via NativeShare's own post processing script since each game's localization requirements might differ. Developers interested in localized share dialogs can write a custom post processor script like yours, so it will be a nice reference for those developers.