MessageFormat 未按预期工作
MessageFormat doesn't work as expected
如果有错误我也找不到。
我给 2
和 1
作为 MessageFormat
的参数,但 returns 1
和 1
。
我无法克服这个问题。我也找不到任何关于它的已知问题。
有没有机会解决这个问题?
我的代码喜欢下面的代码。 TextHelper
class.
出现错误
Intl
class:
public class Intl {
private static final String FILENAME = "MessageBundle";
public static ResourceBundle MessageBundle;
static {
try {
MessageBundle = ResourceBundle.getBundle(FILENAME); // Uses the default Locale of the phone
} catch (NullPointerException | MissingResourceException e) {
Locale defaultLocale = new Locale("en", "US");
MessageBundle = ResourceBundle.getBundle(FILENAME, defaultLocale); // Uses the default Locale if an exception occurred
}
}
}
MessageBundle_en_US.properties
文件:
########################################################################
# HOW TO GENERATE A singleNotifSummary
########################################################################
# String singlePen Intl.MessageBundle.getString("singlePen");
# MessageFormat form = new MessageFormat("singleNotifSummary");
# double[] limits = {1,2};
# String multiPens Intl.MessageBundle.getString("multipen");
# String[] part = {"{0,number}" + singlePen, "{0,number}" + multiPen};
# ChoiceFormat format = new ChoiceFormat(limits, part);
# form.setFormatByArgumentIndex(0, format);
# Object[] args = {1};
# String result = form.format(args);
# Ex 1: Total 1 pen // when args = {1}
# Ex 2: Total 12 pens // when args = {12}
########################################################################
singlePen=pen
singleBook=book
multiPen=pens
multiBook=books
# singleNotifSummary specifies only one type of materials
singleNotifSummary=Total {0}
# doubleNotifSummary specifies both of materials book and pen
# Ex 1: Total 27 books and 1 pen
doubleNotifSummary=Total {0} and {1}
TextHelper
class
public class TextHelper {
...
private int mTotalBooks = 0;
private int mTotalPens = 0;
...
...
/****** Variables that are used when generating Summary Text ******/
final double[] limits = {1,2}; // 1 means single and 2 means multi
final String singleNotifSummary = Intl.MessageBundle.getString("singleNotifSummary"); // "New {0}"
final String doubleNotifSummary = Intl.MessageBundle.getString("doubleNotifSummary"); // "New {0} and {1}"
final MessageFormat singleForm = new MessageFormat(singleNotifSummary);
final MessageFormat doubleForm = new MessageFormat(doubleNotifSummary);
final String singleBook = Intl.MessageBundle.getString("singleBook"); // "Book"
final String multiBook = Intl.MessageBundle.getString("multiBook"); // "Books"
final String singlePen = Intl.MessageBundle.getString("singlePen"); // "Pen"
final String multiPen = Intl.MessageBundle.getString("multiPen"); // "Pens"
final String[] bookPart = {"{0,number,integer} " + singleBook, "{0,number,integer} " + multiBook};
final String[] penPart = {"{0,number,integer} " + singlePen, "{0,number,integer} " + multiPen};
final ChoiceFormat bookFormat = new ChoiceFormat(limits, bookPart);
final ChoiceFormat penFormat = new ChoiceFormat(limits, penPart);
/**********************************************************************/
...
public String generateSummaryText () {
String summaryText = "";
if (mTotalBooks == 0 && mTotalPens > 0) {
// In this case there is only Book(s) (single-type)
singleForm.setFormatByArgumentIndex(0, penFormat); // applying the format to argument "{0}"
Object[] args = { mTotalPens }; // total Pens count
summaryText = singleForm.format(args);
} else if (mTotalBooks > 0 && mTotalPens == 0) {
// In this case there is only Pen(s) (single-type)
singleForm.setFormatByArgumentIndex(0, offerFormat); // applying the format to argument "{0}"
Object[] args = { mTotalBooks }; // total Books count
summaryText = singleForm.format(args);
} else {
// In this case there are Book(s) and Pen(s) (double-type)
doubleForm.setFormatByArgumentIndex(0, bookFormat); // applying the format to argument "{0}"
doubleForm.setFormatByArgumentIndex(1, penFormat); // applying the format to argument "{1}"
Log.v(TAG, TAG2 + " --> mTotalBooks: " + mTotalBooks); // writes 2
Log.v(TAG, TAG2 + " --> mTotalPens: " + mTotalPens); // writes 1
// Object[] args = { new Integer(mTotalPens), new Integer(mTotalPens) };
Object[] args = { mTotalPens, mTotalPens };
summaryText = doubleForm.format(args);
Log.v(TAG, TAG2 + " --> summaryText: " + summaryText); // writes "Total 1 book and 1 pen"
Log.v(TAG, TAG2 + " --> mTotalBooks: " + mTotalBooks); // writes 2
Log.v(TAG, TAG2 + " --> mTotalPens: " + mTotalPens); // writes 1
}
return summaryText;
}
}
问题是您在 bookPart
和 penPart
中都指的是 0
。因此,当您将 {1, 2}
传递给格式化程序时,它会根据您设置的限制正确计算 single/plural,但仅打印第 0 个参数,即 1
。在整个格式化模式中,您必须将 pens 参数称为 {1}
。
正确的代码是:
final String[] bookPart = {"{0,number,integer} " + singleBook, "{0,number,integer} " + multiBook};
final String[] penPart = {"{1,number,integer} " + singlePen, "{1,number,integer} " + multiPen};
查看第二个示例,第 2 行 https://docs.oracle.com/javase/7/docs/api/java/text/ChoiceFormat.html。
如果有错误我也找不到。
我给 2
和 1
作为 MessageFormat
的参数,但 returns 1
和 1
。
我无法克服这个问题。我也找不到任何关于它的已知问题。
有没有机会解决这个问题?
我的代码喜欢下面的代码。 TextHelper
class.
Intl
class:
public class Intl {
private static final String FILENAME = "MessageBundle";
public static ResourceBundle MessageBundle;
static {
try {
MessageBundle = ResourceBundle.getBundle(FILENAME); // Uses the default Locale of the phone
} catch (NullPointerException | MissingResourceException e) {
Locale defaultLocale = new Locale("en", "US");
MessageBundle = ResourceBundle.getBundle(FILENAME, defaultLocale); // Uses the default Locale if an exception occurred
}
}
}
MessageBundle_en_US.properties
文件:
########################################################################
# HOW TO GENERATE A singleNotifSummary
########################################################################
# String singlePen Intl.MessageBundle.getString("singlePen");
# MessageFormat form = new MessageFormat("singleNotifSummary");
# double[] limits = {1,2};
# String multiPens Intl.MessageBundle.getString("multipen");
# String[] part = {"{0,number}" + singlePen, "{0,number}" + multiPen};
# ChoiceFormat format = new ChoiceFormat(limits, part);
# form.setFormatByArgumentIndex(0, format);
# Object[] args = {1};
# String result = form.format(args);
# Ex 1: Total 1 pen // when args = {1}
# Ex 2: Total 12 pens // when args = {12}
########################################################################
singlePen=pen
singleBook=book
multiPen=pens
multiBook=books
# singleNotifSummary specifies only one type of materials
singleNotifSummary=Total {0}
# doubleNotifSummary specifies both of materials book and pen
# Ex 1: Total 27 books and 1 pen
doubleNotifSummary=Total {0} and {1}
TextHelper
class
public class TextHelper {
...
private int mTotalBooks = 0;
private int mTotalPens = 0;
...
...
/****** Variables that are used when generating Summary Text ******/
final double[] limits = {1,2}; // 1 means single and 2 means multi
final String singleNotifSummary = Intl.MessageBundle.getString("singleNotifSummary"); // "New {0}"
final String doubleNotifSummary = Intl.MessageBundle.getString("doubleNotifSummary"); // "New {0} and {1}"
final MessageFormat singleForm = new MessageFormat(singleNotifSummary);
final MessageFormat doubleForm = new MessageFormat(doubleNotifSummary);
final String singleBook = Intl.MessageBundle.getString("singleBook"); // "Book"
final String multiBook = Intl.MessageBundle.getString("multiBook"); // "Books"
final String singlePen = Intl.MessageBundle.getString("singlePen"); // "Pen"
final String multiPen = Intl.MessageBundle.getString("multiPen"); // "Pens"
final String[] bookPart = {"{0,number,integer} " + singleBook, "{0,number,integer} " + multiBook};
final String[] penPart = {"{0,number,integer} " + singlePen, "{0,number,integer} " + multiPen};
final ChoiceFormat bookFormat = new ChoiceFormat(limits, bookPart);
final ChoiceFormat penFormat = new ChoiceFormat(limits, penPart);
/**********************************************************************/
...
public String generateSummaryText () {
String summaryText = "";
if (mTotalBooks == 0 && mTotalPens > 0) {
// In this case there is only Book(s) (single-type)
singleForm.setFormatByArgumentIndex(0, penFormat); // applying the format to argument "{0}"
Object[] args = { mTotalPens }; // total Pens count
summaryText = singleForm.format(args);
} else if (mTotalBooks > 0 && mTotalPens == 0) {
// In this case there is only Pen(s) (single-type)
singleForm.setFormatByArgumentIndex(0, offerFormat); // applying the format to argument "{0}"
Object[] args = { mTotalBooks }; // total Books count
summaryText = singleForm.format(args);
} else {
// In this case there are Book(s) and Pen(s) (double-type)
doubleForm.setFormatByArgumentIndex(0, bookFormat); // applying the format to argument "{0}"
doubleForm.setFormatByArgumentIndex(1, penFormat); // applying the format to argument "{1}"
Log.v(TAG, TAG2 + " --> mTotalBooks: " + mTotalBooks); // writes 2
Log.v(TAG, TAG2 + " --> mTotalPens: " + mTotalPens); // writes 1
// Object[] args = { new Integer(mTotalPens), new Integer(mTotalPens) };
Object[] args = { mTotalPens, mTotalPens };
summaryText = doubleForm.format(args);
Log.v(TAG, TAG2 + " --> summaryText: " + summaryText); // writes "Total 1 book and 1 pen"
Log.v(TAG, TAG2 + " --> mTotalBooks: " + mTotalBooks); // writes 2
Log.v(TAG, TAG2 + " --> mTotalPens: " + mTotalPens); // writes 1
}
return summaryText;
}
}
问题是您在 bookPart
和 penPart
中都指的是 0
。因此,当您将 {1, 2}
传递给格式化程序时,它会根据您设置的限制正确计算 single/plural,但仅打印第 0 个参数,即 1
。在整个格式化模式中,您必须将 pens 参数称为 {1}
。
正确的代码是:
final String[] bookPart = {"{0,number,integer} " + singleBook, "{0,number,integer} " + multiBook};
final String[] penPart = {"{1,number,integer} " + singlePen, "{1,number,integer} " + multiPen};
查看第二个示例,第 2 行 https://docs.oracle.com/javase/7/docs/api/java/text/ChoiceFormat.html。