How to put a variable value inside a resource file value

I have been working with resource files for a while, and I was wondering if there is any great way that I still don't know about to put variables in the resource value.

A simple example:

You bought #amountOfBooksBought books.

My current way of working was to declare two resource values ​​(for 2 labels):

BoughtBooksAmountPreTextLabel.Text: "You Bought"
BoughtBooksAmountPostTextLabel.Text: "books."

Inside the two labels with this text will be the inscription BoughtBooksAmountValueLabel, which contains the number of books purchased.

Is there a more elegant solution for this or is it simple?

+4
source share
1 answer

You can put the formatted line in your resource file:

"You have bought {0} books." 

Then use a formatted string with your value like this:

 BoughtBooksAmountTextLabel.Text = String.Format(yourResourceString, BoughtBooksAmountValueLabel) 
+11
source

Source: https://habr.com/ru/post/1303123/


All Articles