-
Notifications
You must be signed in to change notification settings - Fork 67
Open
Description
I wrote a small implementation of the gettext concept recently and wanted to provide two new methods to the community. I was writing these in React Native, as shown below, and would be happy to incorporate the pure JS equivalents into this project for Hacktoberfest! πππ
In short, the first method pboolgettext returns the plural form if a boolean is true or the singular form if the boolean is false. This is a case when no variables need to be inserted.
The second, pvboolgettext, does the same, but formats the plural message with variables.
/*
* Returns the singular form of the message if a given boolean condition is true, otherwise the plural form.
*
* usage : pgettext(singularMessageId: string, pluralMessageId: string, b: boolean)
*
* @param singularMessageId <string> : the message ID to be used if the number of elements specified is zero
* @param pluralMessageId <string> : the message ID to be sued if the number of elements specified is greater than zero
* @param b <boolean> : a condition indicating if the singular message form should be displayed or not
*/
pboolgettext(singularMessageId: string, pluralMessageId: string, b: boolean) {
if (typeof singularMessageId === "undefined" || typeof singularMessageId === "undefined" || typeof b === "undefined") {
throw "pboolgettext: Invalid arguments";
}
return b ? this.translations[singularMessageId] : this.translations[pluralMessageId];
}
/*
* Returns the singular form of the message if a given boolean condition is true, otherwise the plural form formatted with variables.
*
* usage : pgettext(singularMessageId: string, pluralMessageId: string, b: boolean, value: any)
* usage : pgettext(singularMessageId: string, pluralMessageId: string, b: boolean, values: [any, ... ,any])
*
* Note : variables can be mixed string and number.
*
* @param singularMessageId <string> : the message ID to be used if the number of elements specified is zero
* @param pluralMessageId <string> : the message ID to be sued if the number of elements specified is greater than zero
* @param b <boolean> : a condition indicating if the singular message form should be displayed or not
* @param v <any> : the variable or variables to insert into the plural string
*/
pvboolgettext(singularMessageId: string, pluralMessageId: string, b: boolean, ...values) {
if (typeof singularMessageId === "undefined" || typeof singularMessageId === "undefined" || typeof b === "undefined" || typeof values === "undefined") {
throw "pvboolgettext: Invalid arguments";
}
if (b) {
return this.translations[singularMessageId];
}
return this.vngettext(pluralMessageId, ...values);
}Metadata
Metadata
Assignees
Labels
No labels