Placeholders in grd resource strings consumed by Java/Android - $1 won't work

101 views
Skip to first unread message

Lukasz

unread,
Mar 13, 2015, 7:20:08 PM3/13/15
to chromi...@chromium.org, Lambros Lambrou
Hi,

I have a resource string in src/remoting/resources/remoting_strings.grd:

      <message desc="Description of why host is offline, when client gets a host-offline-reason it doesn't recognize."
               name="IDS_OFFLINE_REASON_UNKNOWN" formatter_data="android_java">
        Unrecognized host error: <ph name="HOST_OFFLINE_REASON">$1<ex>EXIT_CODE_FROM_HOST_NEWER_THAN_CLIENT</ex></ph>.
      </message>

The string contains "$1" as a placeholder.  The placeholder works fine in javascript.  The problem is that this placeholder won't be recognized as-is by Java/Android.  

It seems to me that I can get away with "%s" as a placeholder (i.e. it seems to me that such placeholder would work both in Android/Java as well as in Javascript/chrome.i18n [which seems to accept any unique string as a placeholder?]).  Before I try this out to see if works in practice - QUESTION: Does this seem like a reasonable approach?  Any comments on going down this route?

FWIW, I tried finding already existing strings with placeholders, but most of them are used from C++, not from Java (there are some resources targeting Android in chrome/app/generated_resources.grd, but when searching for some of them [i.e. IDS_PROMPT_DANGEROUS_DOWNLOAD] I am finding them dereferenced only from C++, not from Java).

Thanks,

Lukasz

Anton Vayvod

unread,
Mar 15, 2015, 7:43:20 AM3/15/15
to luk...@chromium.org, chromium-dev, Lambros Lambrou
Hi Lukasz,

some other Android dev might know more but it seems that we have special .grd files for strings like this that are only going to be used from Java. One example of such string would be at https://code.google.com/p/chromium/codesearch#chromium/src/chrome/android/java/strings/android_chrome_strings.grd&l=156 and its usage isat https://code.google.com/p/chromium/codesearch#chromium/src/chrome/android/java/src/org/chromium/chrome/browser/preferences/SignInPreference.java&l=84

I haven't found examples of having the same message defined for both Android and other platforms with %1. My guess is that if you want to avoid duplication, you'll have to extract and expand the string in C++ and pass the end result to Java via JNI.

Hope this helps,
Anton.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

Newton Allen

unread,
Mar 16, 2015, 1:30:59 PM3/16/15
to Anton Vayvod, luk...@chromium.org, chromium-dev, Lambros Lambrou
What Anton said applies to strings used in the src/chrome folder.

In your case, though, the string is used in src/remoting, so the string shouldn't be in android_chrome_strings.grd. If the string is used only in Java, then just use %s (or %d) as the placeholder. If you're using the string from both C++ and Java, then you'll need to do placeholder substitution manually in Java (e.g. using String.replace()).


Lukasz

unread,
Mar 16, 2015, 2:26:46 PM3/16/15
to chromi...@chromium.org, ava...@google.com, luk...@chromium.org, lambros...@chromium.org
Yes - my problem is caused by the desire to use the same resource string both from 1) Android/Java and 2) from Javascript.

I was wrong that chrome.i18n would work with any unique string as a placeholder - it didn't work when I tried it out with "%1$s" (didn't work = placeholder was not replaced by chrome.i18n.getMessage(/*i18n-content*/'OFFLINE_REASON_UNKNOWN', hostOfflineReason)).

I saw <if> element used in src/chrome/app/generated_resources.grd and hoped that I could use it as follows, but grit says that this is invalid input:
      <message desc="Description of why host is offline, when client gets a host-offline-reason it doesn't recognize."
               name="IDS_OFFLINE_REASON_UNKNOWN" formatter_data="android_java">
        Unrecognized host error: <ph name="HOST_OFFLINE_REASON"><if expr="is_android">%1$s</if><if expr="not is_android">$1</if><ex>EXIT_CODE_FROM_HOST_NEWER_THAN_CLIENT</ex></ph>.
      </message>

So, I guess I have the following options left:
  1. Have separate resource string definition for Javascript and for Android (as suggested by Anton).
    • Suboption A: separate grd files for Android and Javascript.
    • Suboption B: <if expr="is_android"> element at a resource-string level (as in generated_resources.grd).
  2. Write code for placeholder substitution that will work for $1, $2, etc. (as suggested by newt@)
I think I'll just go forward with option #1b for now.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

Reply all
Reply to author
Forward
0 new messages