Search Unity

Having an empty Localized as a valid entry?

Discussion in 'Localization Tools' started by PlaycorpStudios, Jul 21, 2021.

  1. PlaycorpStudios

    PlaycorpStudios

    Joined:
    Aug 2, 2016
    Posts:
    48
    By using the localizaiton package you can configure it to define what message returns when the translation is missing for specific Locale which is very helpful to find a key or table that has a missing entry.

    but let's say I have a usecase that localization value should be empty for some language, is there a possiblity to achieve this with smart string? so rather than return key\table is missing, gives me an empty text. for example putting {} for the translation value.

    upload_2021-7-21_17-44-41.png
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,295
    If you leave the entry empty then it should fallback to the US entry ("HI").
    We dont support having a specific entry be empty and ignored at the moment.

    Doing {} in a smart string will use the current default value, so item 0 in that example.
    You could try putting a single space in the string, we check if null or empty so just whitespace should work.
    What is your use case here? Do you want it to fallback to the US one or just show nothing?
     
  3. PlaycorpStudios

    PlaycorpStudios

    Joined:
    Aug 2, 2016
    Posts:
    48
    Hi Karl,

    Thanks for your quick response,

    No I don't want it to fallback to US.
    My use case in here is I have list of first name key and last name key for NPCs that randomly get pick.
    Not all NPCs have last name, so one entry in that list is referencing an empty Localization Entry. A localization that doesn't have a translation for any language. so I want to get empty text when I call GetLocalizedString on it, but instead i get the key of missing entry. Because that's how i configured it in in LocalizationSettings for missing translation.
    upload_2021-7-21_18-21-31.png

    I don't want to set this to empty, because then I loose a useful feature which tells me what translation is missing.

    Thanks.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,295
    You could try changing the Missing Translation State to be PrintWarning only. Then you will still get a warning in the console but should get an empty string returned.
    Alternatively you could add some kind of marker text to indicate that it should be empty and then check that in your code.

    E.G
    Code (csharp):
    1.  
    2. if (text == "EMPTY")
    3.     text = "";
    4.  
    In theory you could also do it with a smart string like you say but you would need to make sure that the smart string error reporting was set to ignore or make sure you pass in valid arguments.
    E.G pass in string.Empty as an argument into GetLocalizedString, then you should be safe to do {0} in your Smart String.
    We check if the entry text is empty, we dont check if the result of a smart string is. In fact you could even do it without using a Smart String for this. As long as you pass in String.Empty as the argument and then have the string as {0} it will work as default(string.format) or smart.
     
    PlaycorpStudios likes this.
  5. zhaozony

    zhaozony

    Joined:
    Jan 7, 2020
    Posts:
    29
    Does this trick still work? upload_2022-9-29_13-31-29.png upload_2022-9-29_13-31-42.png
     
  6. zhaozony

    zhaozony

    Joined:
    Jan 7, 2020
    Posts:
    29
    upload_2022-9-29_13-31-58.png Use case is simple: it's a basic card, description of card is empty.
     

    Attached Files:

  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,295
    Did you try adding a space in the string?
    Could you file a bug report for this? If its something we have a bug and use case for then we can prioritize coming up with a solution.
     
  8. zhaozony

    zhaozony

    Joined:
    Jan 7, 2020
    Posts:
    29
    A white space still works. I'll use it.
    Nevermind, I think since empty string is not allowed, it's common that calculated string from variables isn't allowed too. I feel it's a bit expected behaviour.
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,295
    We do a check if the string is null or empty, your calculated string would have created an empty string.
     
  10. Gapa

    Gapa

    Joined:
    Dec 11, 2012
    Posts:
    27
    This will do it.
    Code (CSharp):
    1. private LocalizedString _localizedName
    2. public string LocalizedName { get => _localizedName.IsEmpty ? "" : _localizedName.GetLocalizedString(); }