Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What is the recommended way to localize rich text?

Discussion in 'Localization Tools' started by mrCharli3, Nov 27, 2022.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Is there a "good" way to localize rich text? I highlight a lot of text in my game using tags
    <cspace=-.05em><b><color=#FF0000>Highlight</color></b></cspace>
    . Which make the localization a bit hard to read, is there a better way?
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    What component are you using for rich text? TextMeshPro has stylesheet feature. Unity UI also has stylesheet feature.

    That way you can at least replace with
    Code (JavaScript):
    1. <cspace=-.05em><b><color=#FF0000>Highlight</color></b></cspace>
    2.  
    3. replace with something like this (read the docs for exact syntax)
    4.  
    5. <style='myhilight1'>Hilight</style>
    Not perfect but slightly more readable than your example.
    Using stylesheets has additional benefit of being able to reuse styles and change style without modifying localized strings.
     
    Last edited: Nov 27, 2022
    mrCharli3 likes this.
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Oh this looks great, didnt know about that feature.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
  5. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Yes, you can use them to add formatting without any arguments in the template string. Is that what you mean?
     
  7. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
    Let's assume I have the following setup:
    upload_2023-2-26_19-58-49.png

    upload_2023-2-26_20-5-32.png

    Now I have a situation in which I can pass in an argument to have it colored red.
    However, what I want to achieve is that I can apply the formatting to hardcoded strings to e.g. highlight certain words in longer strings that are not affected by any arguments.

    Would that be possible with the template formatting? I know that I could use the <style></style> tags, but a solution with the Template Formatter would greatly improve the clarity of my text formatting.
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Can you give me an example? The template needs to be Invoked in a placeholder, however you don't need to do anything with the values in the template, so you can do {0:t(template name)} and do nothing with the 0 value in the template. You just need a way to call the formatter. So I think it will wor how you want. If you don't want to pass a value in then you could add a global variable and just invoke that each time but not use the value. E.g {global.nothing:t(...)}
    I have not tested this but it may also be possible to do it using an empty placeholder, this means use the current value which would be null by default. E.g {{}:t(name)}
     
    Last edited: Feb 26, 2023
  9. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    I think the question is how to do something like this (at least that's what how I interpreted it and would like to know):

    'Next word is {"red":template(c_r)} and this one is {"green":template(c_g)}', without having to pass strings "red" and "green" as variables. Except the {"red":something} syntax doesn't exist.

    Doing 'Next word is {0:template(c_r)} and this one is {1:template(c_g)}' with ["red", "green"] passed as arguments would technically work but that's a lot messier to maintain especially taking into acount that words "red" and "green" also need to be translated and in correct form. Would be much nicer to do it with single smart string.
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Ah I see. I'm afraid that's not possible at the moment. The values do need to be arguments. It's an interesting use case though. I'll have a think about how we could support such a thing, maybe placeholders with speech marks inside {"red"} etc.
     
  11. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
    Thank you for both your replies.

    This is exactly what I meant.

    I think the solution with speech marked placeholders would be great.
     
    karl_jones likes this.
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Hi,

    Good news. With a few small tweaks, this is possible.

    First, you want to create a custom source. This will treat any value contained inside of speech marks as literal text.
    Code (csharp):
    1. using System;
    2. using UnityEngine.Localization.SmartFormat.Core.Extensions;
    3.  
    4. [Serializable]
    5. public class LiteralTextSource : ISource
    6. {
    7.     public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
    8.     {
    9.         if (selectorInfo.SelectorText.Length < 3)
    10.             return false;
    11.  
    12.         int len = selectorInfo.SelectorText.Length;
    13.         if (selectorInfo.SelectorText[0] == '\"' && selectorInfo.SelectorText[len - 1] == '\"')
    14.         {
    15.             selectorInfo.Result = selectorInfo.SelectorText.Substring(1, len - 2);
    16.             return true;
    17.         }
    18.         return false;
    19.     }
    20. }
    Now you need to tell smart format that speech marks are an allowed selector
    Go to the localization settings and add " into here
    upload_2023-2-26_21-32-38.png

    You can now do
    {"hello":t(highlight)} 

    If you want to add spaces into your words, such as "hello world", you will also need to add the space character to the allowed selector chars
    :)
     
    Last edited: Feb 26, 2023
    Dennooo likes this.
  13. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
    Many thanks for the quick solution!
    It works like a charm :)
     
    karl_jones likes this.
  14. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
    Found a small exception that throws an error: For example, when I enter a string with special characters (e.g. ü from the German language), I get the following error message.

    upload_2023-2-27_0-35-50.png
     
  15. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Hmm that's strange. You should be able to get around this by adding them to the allowed selector characters although I would consider this a bug. Can you please submit a bug report so we can look Into this?
     
    Dennooo likes this.
  16. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
    Thank you. I created a bug report.
     
    karl_jones likes this.