NSLocalizedString with variables formatter

Here is an issue of NSLocalizedString, this line:

NSLocalizedString(“Page (currentPage ?? 1)/(totalPages ?? 1)”, comment: “”)

The string ‘Page’ should be localized but currentPage and totalPages are number variables, they are unable to pre-translated into localized string table.
So the correct way is:

NSLocalizedString(“Page”, comment: “”) + ” (currentPage ?? 1)/(totalPages ?? 1)”
or
String.formatted( NSLocalizedString(“Page %d/%d”, comment: “”),
currentPage ?? 1,
totalPages ?? 1)

I think the second style is good because in some languages we need to custom digital format. For example, in Chinese it may be:
第 %d/%d 页

Leave a Reply

Your email address will not be published. Required fields are marked *