Search Unity

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

Discussion in 'Localization Tools' started by marekzd, Nov 20, 2020.

  1. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    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?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    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.
     
  3. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    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.

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEditor.Localization;
    4. using UnityEngine;
    5.  
    6. ...
    7.  
    8. var tableCollectionName = "Table_Collection";
    9. var tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableCollectionName);
    10. foreach (var table in tableCollection.StringTables)
    11. {
    12.     foreach (var element in table)
    13.     {
    14.         var entry = element.Value;
    15.         entry.IsSmart = true;
    16.     }
    17.  
    18.     EditorUtility.SetDirty(table);
    19. }
    20. EditorUtility.SetDirty(tableCollection);
    21.  

    Ok.
    Thanks for the info.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    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.
     
  5. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    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 ?
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    If you close the window and re open it are they correct? I think we just don't update the window.
     
  7. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    No, after reopening window they are only partially correct - some colums are still unchanged.
    After restarting Unity the problem remains.
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    Could you file a bug report?
     
  9. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    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 ?
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    Can you share the script you are using? Maybe something will stand out.
     
  11. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    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.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Localization;
    3. using UnityEngine;
    4.  
    5. public class LocalizationUtils : MonoBehaviour
    6. {
    7.     [MenuItem("Dev/Fix Localizations")]
    8.     static void FixLocalizations()
    9.     {
    10.         // Mark all localizations as smart
    11.         var tableCollectionName = "Strings";
    12.         var tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableCollectionName);
    13.         foreach (var table in tableCollection.StringTables)
    14.         {
    15.             foreach (var element in table)
    16.             {
    17.                 var entry = element.Value;
    18.                 entry.IsSmart = true;
    19.             }
    20.  
    21.             EditorUtility.SetDirty(table);
    22.         }
    23.         EditorUtility.SetDirty(tableCollection);
    24.     }
    25.  
    26.     [MenuItem("Dev/Fix Localizations 2")]
    27.     static void FixLocalizations2()
    28.     {
    29.         // Mark all localizations as not smart
    30.         var tableCollectionName = "Strings";
    31.         var tableCollection = LocalizationEditorSettings.GetStringTableCollection(tableCollectionName);
    32.         foreach (var table in tableCollection.StringTables)
    33.         {
    34.             foreach (var element in table)
    35.             {
    36.                 var entry = element.Value;
    37.                 entry.IsSmart = false;
    38.             }
    39.  
    40.             EditorUtility.SetDirty(table);
    41.         }
    42.         EditorUtility.SetDirty(tableCollection);
    43.     }
    44. }
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    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
    Code (csharp):
    1. EditorUtility.SetDirty(table.SharedTableData);
     
  13. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    Wow, nice :)

    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
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    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?
     
  15. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    No, it didn't change.
     
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    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.
     
  17. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    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 ?
     
  18. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    Yes please file a bug report. You can then share the bug number so I can keep an eye on it :)
     
  19. marekzd

    marekzd

    Joined:
    Jul 1, 2016
    Posts:
    33
    Ok, bug filed :D
    Issue number: 1294892
     
    karl_jones likes this.
  20. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    245
    karl_jones likes this.
  21. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
  22. DucaDiMonteSberna

    DucaDiMonteSberna

    Joined:
    Jan 18, 2018
    Posts:
    79
    so are we still bound to click on every single entry in a table, repeating on each language even now? it's 100+ click per table...
     
  23. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    We have an option to enable it for all rows in 1.5.0
     
    DucaDiMonteSberna likes this.