Improving the ndmf.localization adoption experience #179
enitimeago
started this conversation in
General
Replies: 1 comment 1 reply
-
Your issues with loading assets from static constructors may be caused by static constructors being run off the main thread, where loading assets isn't allowed, or running before the asset database is initialized. For this reason it can be better to use
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I went through a fair amount of trial-and-error to figure out how to adopt
ndmf.localization
and resolve various issues I've run into with it so far, but I'm having a hard time coming up with a concrete "issue" to describe what happened.I'll try and summarize what happened and write out my experience, in case there might be some useful takeaways at all?
Wasn't sure if this should be filed under Issues or Discussions here, feel free to convert to an issue if that makes more sense?
Happy to try and expand on anything that's unclear here.
Long story short
In summary, I think I ran into:
AssetDatabase.LoadAssetAtPath<LocalizationAsset>
can unexpectedly return null when called (too early?) due to[InitializeOnLoad]
[InitializeOnLoad]
and works fine.ndmf.localization.Localizer
's assetLoader callback will cause a null pointer exception inside NDMF atndmf/Editor/UI/Localization/Localizer.cs
Lines 63 to 71 in 1465ba0
The long story
Looking at Modular Avatar's Editor/Localization/Localization.cs, its Localization class is annotated with
[InitializeOnLoad]
and the comment "ensure that we register languages with NDMF on domain load".I tried adding the same attribute when I created my Localizer class, which constructs an NDMF Localizer inside its static constructor:
This turned out to cause my package to fail to load localizations when imported into an open Unity project, until I right-clicked the project and clicked Reimport.
Not understanding the root cause, but observing Modular Avatar's, NDMF's, Avatar Optimizer's localization setups were using GUIDs instead of hardcoded package paths, I thought I'd try doing the same:
Having done so I observed "Translation files failed to be loaded!" in Unity's log right after importing the package.
Further fruitless rewrites landed me with this:
And I'd see "Failed to load LocalizationAsset for en-us" "Failed to load LocalizationAsset for ja-jp" in my logs.
Accidentally leaving out
.Where(asset => asset != null)
caused a null pointer exception inside NDMF.I tried some other things that didn't seem to make any effect:
autoReferenced: true
and I changed that to falseFinally, I tried removing
[InitializeOnLoad]
, and that allowed localizations to be successfully loaded when the package was imported.👉 Question: How is Modular Avatar's use of
[InitializeOnLoad]
different? A cursory look at Modular Avatar shows that it also seems to reference localization files in its static initializer, albeit json files not po files? https://github.com/bdunderscore/modular-avatar/blob/8e6d8302ef5d6899f152d56649c81a8db3e38046/Editor/Localization/Localization.cs#L55👉 Question: What's the "correct" time to be loading localization files? I feel like my final rewrite here, despite the rewrite probably being unnecessary for localizations to "work", is the cleanest approach i.e. only try to load localization files when NDMF asks for them.
👉 Question: Should docs recommend searching localization files using GUIDs or hardcoded paths? I would have guessed GUIDs, but changing to GUIDs bit me when I tried moving my localizations from the dedicated localization assembly to editor assembly subfolder. Crucially I let Unity move the .meta file (and therefore GUID) of the assembly to inside the editor assembly. (In other words packageRoot/Localizations.meta got moved to packageRoot/Editor/Localizations.meta.) In-place upgrading my package caused localizations to break with "Translation files not found!", and I had to resolve this by creating a new GUID for the Localizations folder.
👉 Question: Should docs recommend a language code capitalization? i.e. "en-US" vs "en-us"
👉 Question: Should NDMF be catching null assets being returned by the asset loader? Anything else that can be implemented in NDMF that can help stop people making similar mistakes to me? :)
Beta Was this translation helpful? Give feedback.
All reactions