Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plurals don't work in xml

I am trying to load plurals in the error text in my TextInputEditText:

<android.support.design.widget.TextInputEditText
    android:id="@+id/et_subject_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    android:lines="1"
    android:maxLines="1"
    android:singleLine="true"
    android:text="@{ model.name }"
    android:hint="@string/hint_name"
    app:validateRegex='@{"^*\\S{2}.*$"}'
    app:validateRegexMessage="@{@plurals/error_too_short(model.minNameLength)}"/>

But in my app it shows my string but with %d instead of model.minNameLength value. The app:validateRegex is an attribute is defined by Data Binding Validator library.
My plurals:

<plurals name="error_too_short">
    <item quantity="one" >Too short, enter at least %d visible character.</item>
    <item quantity="other" >Too short, enter at least %d visible characters.</item>
</plurals>

What's wrong? Or has it a some another way to show plurals in xml?
P.S. I use a data binding engine V2 if it's important.

like image 981
Шах Avatar asked Jul 17 '18 23:07

Шах


2 Answers

In the official Android docs:

When using the getQuantityString() method, you need to pass the count twice if your string includes string formatting with a number. For example, for the string %d songs found, the first count parameter selects the appropriate plural string and the second count parameter is inserted into the %d placeholder. If your plural strings do not include string formatting, you don't need to pass the third parameter to getQuantityString.

So, as you want model.minNameLength to define which plural version to select as well as to insert its value in the string, you should provide it twice. So, the databinding expression should instead be like:

app:validateRegexMessage="@{@plurals/error_too_short(model.minNameLength, model.minNameLength)}"
like image 95
Sarquella Avatar answered Sep 28 '22 00:09

Sarquella


It should look like this:

<plurals name="recent_messages">
    <item quantity="zero">No Recent Messages</item>
    <item quantity="one">1 Recent Message</item>
    <item quantity="other">%1$d Recent Messages</item>
</plurals>

mContext.getResources().getQuantityString(R.plurals.recent_messages, count, count);
like image 30
Elletlar Avatar answered Sep 28 '22 01:09

Elletlar