Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using .resx localized resources via CultureInfo.CurrentUICulture with IL2CPP

Discussion in 'Web' started by jilleJr, Apr 18, 2019.

  1. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Attn! This post explains a problem with solution, for future self reference. This is not a question nor a bug report.

    Using .resx resource files for localizing is a good choice of tool. We're using it on multiple products in the dependency chain. Works very well.

    But some problems in the "young IL2CPP" compilers world. Latest hit off the track: switching
    CultureInfo.CurrentCulture
    does not change the values returned from the resource managers.

    Why? To begin the bug hunting, first clue is that the resource manager can not locate the specified value among the different satellite resource assemblies, so it always returns the value from the base assembly, i.e. the "unspecified language" resource value.

    With lots of research I found many ugly (and very old) workarounds. Then when I found the solution, I realized it stared me in the eyes from the documentation. Was not obvious (to me ok, cut me s lack), but it was there. Was upon a step back, rubber duck session, that I saw it.

    Reason: The satellite assemblies did not even get loaded into the built product when building with IL2CPP, as opposed to when it did when building with Mono. The IL2CPP does a lot more code stripping than Mono.

    The resources are not referenced in the code, they're loaded on request via reflection! Huh! And what do we do with stuff loaded via reflection? Just add them to the linker!

    Boom bam. Big phew.

    ---

    Still experiencing trouble with using multiple extra languages via multiple satellite assemblies. They gotta have correct naming to work such as "baseAssemblyName.resources.dll", which collides when using multiple languages as the language code can not be added to the name, but they can't have the same name...

    Code (csharp):
    1. PrecompiledAssemblyException: Multiple precompiled assemblies with the same name Net_Standard_2_0_Project.resources.dll included for the current platform.
    OH WELL :rolleyes: Cant have everything. Getting one bonus language is enough for me to begin with!

    Maybe I should report the assembly name thing as a bug report... Nah maybe after easter
     
  2. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
    Hi Applejag,

    I'm currently trying to use resx and have the problem that was reported here:
    https://forum.unity.com/threads/resx-files.725558/

    Unity just throws the resx files out of the project, so when building they are never included and no resources.dll is created. How did you handle that part?
     
  3. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
    This is working for me now, by putting the resx files into their own class library.

    I'm still curious how exactly you have done it. What exactly have you added to the linker? Would be great to have an example.

    BTW: When using a class library you can workaround the multiple assembly problem by using Resource.Embedder: https://www.nuget.org/packages/Resource.Embedder/
     
  4. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Yea it's a requirement to embed the resources into a precompiled dll that's fed into Unity. Unity don't embed any resx files (except as TextAsset's, which could also be a workaround).

    Thank you so much for the NuGet package tip! I'll investigate