Android multilanguage support: string formatting issue due to different placeholder counts

android string resource %
android string format
android string placeholder
android multi language
android string resource format
android set string value programmatically
android get string-array from strings xml
android array resource

I'm making a french Android app and I'm trying to support English.

I use "placeholders" to format my strings, so I can adapt them to male and female users. For example, this string in my strings.xml file:

<string name="encouraging_comment">
    Les %1$s sont compliqué%2$ss...
</string>

will become "Les hommes sont compliqués" ("Men are complicated") or "Les femmes sont compliquées" ("Women are complicated").

And there lies my problem. The string translation, as follows...

<string name="encouraging_comment">
    %1$s are complicated...
</string>

...needs only one placeholder, when the french equivalent needs two.

How can I manage this issue ? Thanks in advance.

Just omit the second placeholder from the English template string and use an empty string for the second argument (or any other string, it doesn't matter, the value will be ignored) when you render the string:

XML:

<string name="encouraging_comment">
    %1$s are complicated...
</string>

Java:

getString(R.string.encouraging_comment, "women", "");
getString(R.string.encouraging_comment, "men", "");

This works because it's not an error if there are more arguments than placeholders, only if there are fewer arguments than placeholders.

I'm assuming that you'll have some kind of table or mapping where you look up the placeholder values based on language and gender. In pseudo-code:

(French,  Female) -> ("femmes", "e")
(French,  Male)   -> ("hommes", "" )
(English, Female) -> ("women",  "" )
(English, Male)   -> ("men",    "" )

String resources, XML resource that carries different strings for pluralization. All strings are capable of applying some styling markup and formatting arguments. For  How can i set my default language? The faces-config.xml default-locale does not seem to work. The <default-locale> is only used when the client didn't supply the locale by itself in flavor of the Accept-Language HTTP request header, or supplied one which isn't supported by the application as definied in <supported-locale>.

AFAIK, you can do this by adding check in your code like

String result;
if ( Locale.getDefault().getLanguage().equals("en")){  // examples "en", "fr", "sp"
      result= getString(R.string.encouraging_comment, myString);
    }
    else{
      result= getString(R.string.encouraging_comment, myString, myStringTwo);
    }
textView.setText(result);

Hope this helps you :)

Support different languages and cultures, You can provide support for different locales by using the resources There are also a few default files such as res/values/strings.xml For more information, see Localization. One of the most common tasks in an app is formatting text. The same problem may occur if you include RTL text within a  How to set font-weight of a column text to bold based on another column value in Telerik Grid. telerik,formatting,conditional,client-templates. I modified my code to the following with help from the post: Telerik MVC Grid making a Column Red Color based on Other Column Value .Columns(columns => {columns.Bound(d => d.IsTrue).Width(0); columns.Bound(d => d.Name).Title("Name).Width(200

One way to do this is create a string locale in your strings.xml

for English Strings.xml

<string name="locale">En</string>

for French Strings.xml

<string name="locale">Fr</string>  

Then while setting string

 switch (getString(R.string.locale)) {
            case "Fr":
                //set your string for French
                break;
            case "En":
                //set your string for English 
                break;
        }

Formatter, AccountManagerCallback · AccountManagerFuture · OnAccountsUpdateListener Although the format strings are similar to C, some customizations have the format string are fixed text including "Dukes Birthday: " and any other spaces supported data types, localization, and interactions between flags,  format string 1; Formating 6; formato 1; formats 1; formatting 7; formatting date 1; formatting dates 1; formatting issue 1; Formatting Rules 2; formula 12; formula filter 1; Formules 2; FQDN 1; fractile 1; framework 1; frecuency 1; Freelancer 2; freeze pane 1; from 1; From Folder 1; front end 2; front-end 4; FTP 1; FTP SFTP Connector 3; full 3

Localize your app, For example, if users input data that needs to be formatted or sorted Resources are text strings, layouts, sounds, graphics, and any other static data that your Android app needs. (This document focuses on localization and locale. the app might run without a problem, because res/values-en/strings.xml  [JDK-8133347] - Add makefiles support and basic session, persistence history navigation with jline [JDK-8133348] - Reference.reachabilityFence [JDK-8133451] - Add weak CAS, and acquire/release forms of CAS [JDK-8133568] - C1 support for Unsafe.{getAcquire|setRelease} [JDK-8133598] - 3rd party approval for GStreamer

10 Common Mistakes to Avoid In Software Localization – Phrase Blog, There are various file formats that make suitable resource files. can solve this problem by leaving extra space after each label for the string to grow. It supports alternate spellings, date formats and other differences Some developers love to create concatenated pieces of sentences using placeholders,​  #No Fix#. When type attribute of apex:inputField is changed as date type and datetime-local type dynamically within apex:repeat, validation of type always fails. - Actual Result validation of type always fails. - Expected Result The customer is expecting that validation of type should not fail in this case.

HTML tags in strings · Issue #212 · scelis/twine · GitHub, twine generate-localization-file test-twine.txt strings.xml --lang en Awesome, thanks for filling out the issue template +1 Do you know of a way to store the string with HTML tag formatting in Another tag we should support is <a> . /​how-to-use-formatted-strings-together-with-placeholders-in-android. 2014-October Archive by Thread. Messages sorted by: [ Thread ] [ Date] [ Author] Other months; Messages are ordered newest-to-oldest in this index. The newest threads will be at the top of this page, the oldest will be at the bottom.

Comments
  • Sadly this gives me a Wrong argument count error in the "english" block; I guess it counts the arguments for the default string.
  • @Eva can you share the error or your code snipet how are you using this code
  • Funny thing: with this code if(getString(R.string.locale).equals(Constantes.LOCALE_EN)) {s=res.getString(R.string.str, "...","");} The error/warning reads "Wrong argument count, format string encouraging_comment requires 2 but format call supplies 1". While with this one if(getString(R.string.locale).equals(Constantes.LOCALE_EN)) {s=res.getString(R.string.str, "...","");} it goes "Wrong argument count, format string encouraging_comment requires 1 but format call supplies 2". In both cases though the app works w/out crashing!