Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dm20 opened this issue Oct 18, 2019 · 0 comments

Comments

@dm20
Copy link

dm20 commented Oct 18, 2019

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);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant