How can I quickly enable 'smart' for multiple translations in a table?

I’ve managed to import my translations into localization table and adjust them for SmartFormatter.

How can I quickly check all translations as ‘smart’ ?

What’s the reason for having ‘smart’ setting on each translation instead on the whole row or even whole table ? Are there situations where it is helpful?

Hmm. We don’t really have an alternative than just setting the IsSmart flag for each language.
This can also be done through a script to speed things up.
You bring up an interesting point though, maybe we don’t need to set it for every language, setting it per entry is probably enough. I can’t actually think of an example of where you may only want smart on some now.
Ill make a note look into it with the UX team.

Thanks, for the clue. I managed to write one.
Although I had problems with saving… so there might be some bugs in it.
It eventually worked after I clicked on File -> Save Project and reopened Localization Tables window.

using UnityEditor;
using UnityEditor.Localization;
using UnityEngine;

...

var tableCollectionName = "Table_Collection";
var tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableCollectionName);
foreach (var table in tableCollection.StringTables)
{
    foreach (var element in table)
    {
        var entry = element.Value;
        entry.IsSmart = true;
    }

    EditorUtility.SetDirty(table);
}
EditorUtility.SetDirty(tableCollection);

Ok.
Thanks for the info.

Do you mean the changes were not saved or that the file was not being written?
When you mark an asset dirty it does not save the file, just tells Unity that its changed so the next time it flushes to disk for a save it will write the file out.

I don’t know the cause, but saving behaves strange.

I’ve done some more testing and:

  • after running a script and clicking on File -> Save Project resulted in table files being modified
  • but loclalization window didn’t update
  • and only after reopening it, it seemed to update
  • but not completly - right now I see that some tables changed while the others don’t

Do you see any flaws in my script ? I don’t feel super confident about it.
May I run File -> Save Project through the script ?

If you close the window and re open it are they correct? I think we just don’t update the window.

No, after reopening window they are only partially correct - some colums are still unchanged.
After restarting Unity the problem remains.

Could you file a bug report?

The problem is that I’m not able to give a solid repro.

I’ve created a smaller project and uploaded there a lot of translations.
My observations:

  • files got modified everytime - that’s seems nice
  • once ‘localization tables’ window updated ok after running a script and saving the project
  • once I needed to reimport a folder with localization tables to force ‘localization tables’ window to update
  • another time the table collection lost connection to tables (they become loose)
  • and reimport didn’t help
  • but restarting Unity did help
  • I’ve not yet observed a situation from my main project, where after running a script ‘localization tables’ window was updated, but only partially (only some columns were updated)

Shall I report a bug with my above observations and this small project ?

Can you share the script you are using? Maybe something will stand out.

Here you have my editor script from this smaller project:

FixLocalizations() function sets smart to true for all translations.
FixLocalizations2() function is the same but sets smart to false.
I was playing with both of them alternately.

using UnityEditor;
using UnityEditor.Localization;
using UnityEngine;

public class LocalizationUtils : MonoBehaviour
{
    [MenuItem("Dev/Fix Localizations")]
    static void FixLocalizations()
    {
        // Mark all localizations as smart
        var tableCollectionName = "Strings";
        var tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableCollectionName);
        foreach (var table in tableCollection.StringTables)
        {
            foreach (var element in table)
            {
                var entry = element.Value;
                entry.IsSmart = true;
            }

            EditorUtility.SetDirty(table);
        }
        EditorUtility.SetDirty(tableCollection);
    }

    [MenuItem("Dev/Fix Localizations 2")]
    static void FixLocalizations2()
    {
        // Mark all localizations as not smart
        var tableCollectionName = "Strings";
        var tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableCollectionName);
        foreach (var table in tableCollection.StringTables)
        {
            foreach (var element in table)
            {
                var entry = element.Value;
                entry.IsSmart = false;
            }

            EditorUtility.SetDirty(table);
        }
        EditorUtility.SetDirty(tableCollection);
    }
}

Ok I think I know the issue.
Setting IsSmart makes changes to metadata inside of SharedTableData. So you also need to set that dirty.
You don’t need to set the collection dirty.
Do this where you set the collection dirty instead

EditorUtility.SetDirty(table.SharedTableData);

Wow, nice :slight_smile:

Now it behaves much more predictable.

On my small project:

  • everything works ok
  • only I need to close and reopen 'localization tables` window each time I run the script
  • good thing is that I don’t even need to save project - setting data dirty seems enough

On my main project:

  • it works better, but partial updates remained
  • when I clear smart flag on all translations, some columns remain not changed
  • translations in those columns still have smart flag set
  • saving project, reimporting tables files or restarting unity doesn’t help
  • changes (git diff) on the files confirms the problem - these unchanged tables doesn’t seem to have metadata removed, while others have

The smart flag is stored in metadata that is part of the shared table data. Does the shared table data asset change in a git diff?

No, it didn’t change.

I would need to take a look at an example project or if you cant get it to reproduce I can look at the full project if you are happy to share it.

I would like to avoid sharing the whole project,
but I was able to extract only the localization part.
It is enough to repro this ‘partial update’ problem at least on my side.

Shall I file this as a bug ? Will you be able to find it ?

Yes please file a bug report. You can then share the bug number so I can keep an eye on it :slight_smile:

Ok, bug filed :smile:
Issue number: 1294892

1 Like

Maybe it’s related to this issue I sent some days ago?: Unity Issue Tracker - Smart Format Tag in Metadata stays after disabling the Smart option when Smart option is enabled on another same column Entry

Check the metadata on the entries on which you can’t clear the smart flag, if they have more than one “Smart Format Tag” it’s probably the same bug.

1 Like