4

I have a string with two placeholders, with XML:

<string name="message">%1$s informs that %2$s is not feeling well today.</string>

Because %2$s is either "he" or "she", when translated to portuguese, we should only have the first placeholder, that is the name of a person:

<string name="message">%1$s informa que não se sente muito bem hoje.</string>

The code compiles and works fine, but Lint is complaining that String.format string doesn't match the XML format string

What is the best way to handle this situations?

Ricardo Belchior
  • 536
  • 3
  • 11
  • 2
    as a quick hack, add the second placeholder at the end in the Portuguese string resource, then just set it to emptiness, like so: %1$s informa que não se sente muito bem hoje.%2$s ;-) – leRobot Sep 24 '15 at 14:44
  • 1
    you can maintain same format with two placeholder and inside code you can handle with which language being used, and add empty string for second placeholder for portuguese. – Rajen Raiyarela Sep 24 '15 at 14:48
  • 1
    @leRobot that solves the warning but the %2$s gets appended to the translated string. I would need to create another one, and not translate it in portugues, just for that specific case... ugly. – Ricardo Belchior Sep 24 '15 at 14:53
  • 1
    @RajenRaiyarela not the most elegant solution, but will do that if nothing better arrives. – Ricardo Belchior Sep 24 '15 at 14:55
  • Do you really think that checking for the current language in runtime is better than create an empty String resource? I do not know if there is a built-in way to do this, but is interesting to see people's creativity. – tato.rodrigo Sep 24 '15 at 15:02
  • 1
    @tato.rodrigo it's not just an "empty string resource", it's a string that's only empty in portuguese. Our app is translated in 11 languages, by different people in a 3rd party platform. so yes. i prefer to keep it simple in the xml and, if i need to, create one condition in the code. – Ricardo Belchior Sep 24 '15 at 15:10
  • you can attach contextual comments for the translators to see or add exceptions in that 3rd party platform though :P – leRobot Sep 24 '15 at 15:17

2 Answers2

2

Lint will complain as it is looking for the 2nd parameter. you can put your placeholder at the place of the word which represent he and she both and keep it same for both the meanings. alternatively if there no word for he and she in this language, just keep the placeholder there and replace it with a blank string.

Rahul Tiwari
  • 5,931
  • 2
  • 41
  • 66
2
<string name="message">%1$s informa que não %2$s sente muito bem hoje.</string>

and let %2$s be "se" and... "se" :)

Psytho
  • 2,802
  • 1
  • 14
  • 23
  • 1
    %2$s comes from "he"/"she". doesn't make sense to have it translated differently. We get our translations from a 3rd party platform, so I prefer not to do that. thanks for your input ! – Ricardo Belchior Sep 24 '15 at 15:04
  • @RicardoBelchior a placeholder in one language should be there in all the languages, this is how it works. so sometimes one have to make exception. – Rahul Tiwari Sep 25 '15 at 06:34