From f341a269c92720b20ecdda68971a33c124fba414 Mon Sep 17 00:00:00 2001 From: Ken Gaillot Date: Jun 10 2020 17:56:11 +0000 Subject: Refactor: libcrmcommon: add convenience macros for plurals I've avoided making s_if_plural() an official API due to its hackiness, but it really is the best solution for now. Promote it to pcmk__plural_s(), along with a companion macro pcmk__plural_alt() for more complicated plurals. --- diff --git a/include/crm/common/internal.h b/include/crm/common/internal.h index 484c836..ee560c9 100644 --- a/include/crm/common/internal.h +++ b/include/crm/common/internal.h @@ -107,6 +107,29 @@ bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len); gint crm_alpha_sort(gconstpointer a, gconstpointer b); +/* Correctly displaying singular or plural is complicated; consider "1 node has" + * vs. "2 nodes have". A flexible solution is to pluralize entire strings, e.g. + * + * if (a == 1) { + * crm_info("singular message"): + * } else { + * crm_info("plural message"); + * } + * + * though even that's not sufficient for all languages besides English (if we + * ever desire to do translations of output and log messages). But the following + * convenience macros are "good enough" and more concise for many cases. + */ + +/* Example: + * crm_info("Found %d %s", nentries, + * pcmk__plural_alt(nentries, "entry", "entries")); + */ +#define pcmk__plural_alt(i, s1, s2) (((i) == 1)? (s1) : (s2)) + +// Example: crm_info("Found %d node%s", nnodes, pcmk__plural_s(nnodes)); +#define pcmk__plural_s(i) pcmk__plural_alt(i, "", "s") + static inline char * crm_concat(const char *prefix, const char *suffix, char join) {