Search Unity

Question Can't set Value of Localized String variable in PersistentVariablesSource

Discussion in 'Localization Tools' started by Estema_TheWoz, Nov 11, 2021.

  1. Estema_TheWoz

    Estema_TheWoz

    Joined:
    Aug 5, 2021
    Posts:
    7
    Hello everyone,

    Might be something very obvious... But I tried to access a Localized String variable set in a Variable Groupe Asset assigned to my Project Settings > Localization > String Database > Smart Format > Sources under "Persistent Variables Source" with "global" for key.

    From now, i've tried to access it by this way :

    Code (CSharp):
    1. PersistentVariablesSource source = LocalizationSettings
    2.             .StringDatabase
    3.             .SmartFormatter
    4.             .GetSourceExtension<PersistentVariablesSource>();
    5. IVariable val;
    6.         var o = source["global"].TryGetValue("locStringVarName", out val);
    7. Variable<LocalizedString> locStringVar;
    8.         if(o) locStringVar = val as Variable<LocalizedString>;
    9. if(locStringVar != null){
    10. var op = LocalizationSettings.InitializationOperation;
    11.         if (op.IsDone)
    12.         {
    13.             locStringVar.Value = localizedString;
    14.         }
    15.         else
    16.             op.Completed += (op) => locStringVar.Value = localizedString;
    17. }
    I have no errors but or the localizedString is never assigned to the locStringVar.Value or I miss something to update it.

    I access, at the same time, a string variable in the same persistent Variables and it work fine.
    Also, I'm working with com.unity.localization package v1.0.4 under Unity v2020.3.19f1


    Any suggestions would be very helpful :)

    Regards
     
    Last edited: Nov 12, 2021
  2. whebertML

    whebertML

    Joined:
    Mar 1, 2022
    Posts:
    1
    Did you ever get this working? I was just recently trying to do the same thing and it wasn't super clear how to modify a LocalizedString in the PersistentVariablesSource. Anyway, got it working like so:

    Code (CSharp):
    1.  
    2. using UnityEngine.Localization;
    3. using UnityEngine.Localization.Settings;
    4. using UnityEngine.Localization.SmartFormat.Extensions;
    5. using UnityEngine.Localization.SmartFormat.PersistentVariables;
    6.  
    7. var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    8. if(source.TryGetValue("global", out VariablesGroupAsset globalVariables))
    9. {
    10.   if(globalVariables.TryGetValue("locStringVarName", out IVariable variable))
    11.   {
    12.     var localizedString = variable as LocalizedString;
    13.     if(localizedString != null)
    14.     {
    15.       localizedString.TableEntryReference = "table_entry_key";
    16.     }
    17.   }
    18. }
    19.  
     
    Last edited: Aug 18, 2022
    akareactor likes this.
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    That's the correct way to do it.