paraphrase icon indicating copy to clipboard operation
paraphrase copied to clipboard

What about strings without parameters?

Open Raenar4k opened this issue 2 years ago • 2 comments

As far as i understand Paraphrase aims to generate these methods for strings that use ICU message format. But what about simple strings without any arguments?

My use-case is that we used a string without arguments, that later was changed to have 1 argument. But if we use R.strings for these cases, then we cannot detect such change. Reverse is possible to detect, thanks to Paraphrase.

  <string name="test_string">Bagels are tasty</string>
  <string name="test_argument">bagel count: {0}</string>
  <string name="test_plural">{count, plural, one {# tasty bagel} other {# tasty bagels}}
image

Raenar4k avatar Oct 31 '23 14:10 Raenar4k

Seems a like pretty compelling argument

JakeWharton avatar Oct 31 '23 19:10 JakeWharton

Great question!

We could start generating accessors for string resources without arguments. Something like:

object FormattedResources {
  @StringRes val test_string: Int = R.string.test_string
  fun test_argument(arg0: Any): StringResource = ...
  fun test_plural(count: Number): StringResource = ...
}

Then if we added an argument, the val would turn into a fun and we would get a compile error.

I suspect that in most Android projects, a majority of the string resources will have no arguments. So it might be a little weird to access everything through a class called FormattedResources. But maybe not? Or maybe we could rename it StringResources?

Another option would be something like a lint rule that warns us if we make a getString call with missing arguments.

theisenp avatar Nov 02 '23 05:11 theisenp