Skip to content

Add "trigger plural on boolean" and "trigger plural with variables on boolean" methods #62

Open
@dm20

Description

@dm20

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions