1 /* Simple Plugin API
2  *
3  * Copyright © 2021 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_I18N_H
26 #define SPA_I18N_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <spa/utils/hook.h>
33 #include <spa/utils/defs.h>
34 
35 /** \defgroup spa_i18n I18N
36  * Gettext interface
37  */
38 
39 /**
40  * \addtogroup spa_i18n
41  * \{
42  */
43 
44 #define SPA_TYPE_INTERFACE_I18N		SPA_TYPE_INFO_INTERFACE_BASE "I18N"
45 
46 #define SPA_VERSION_I18N		0
47 struct spa_i18n { struct spa_interface iface; };
48 
49 struct spa_i18n_methods {
50 #define SPA_VERSION_I18N_METHODS	0
51         uint32_t version;
52 
53 	/**
54 	 * Translate a message
55          *
56          * \param object the i18n interface
57          * \param msgid the message
58          * \return a translated message
59 	 */
60 	const char *(*text) (void *object, const char *msgid);
61 
62 	/**
63 	 * Translate a message for a number
64          *
65          * \param object the i18n interface
66          * \param msgid the message to translate
67          * \param msgid_plural the plural form of \a msgid
68          * \param n a number
69          * \return a translated message for the number \a n
70 	 */
71 	const char *(*ntext) (void *object, const char *msgid,
72 			const char *msgid_plural, unsigned long int n);
73 };
74 
75 SPA_FORMAT_ARG_FUNC(2)
76 static inline const char *
spa_i18n_text(struct spa_i18n * i18n,const char * msgid)77 spa_i18n_text(struct spa_i18n *i18n, const char *msgid)
78 {
79 	const char *res = msgid;
80 	if (SPA_LIKELY(i18n != NULL))
81 		spa_interface_call_res(&i18n->iface,
82 	                        struct spa_i18n_methods, res,
83 				text, 0, msgid);
84 	return res;
85 }
86 
87 static inline const char *
spa_i18n_ntext(struct spa_i18n * i18n,const char * msgid,const char * msgid_plural,unsigned long int n)88 spa_i18n_ntext(struct spa_i18n *i18n, const char *msgid,
89 		const char *msgid_plural, unsigned long int n)
90 {
91 	const char *res = n == 1 ? msgid : msgid_plural;
92 	if (SPA_LIKELY(i18n != NULL))
93 		spa_interface_call_res(&i18n->iface,
94 	                        struct spa_i18n_methods, res,
95 				ntext, 0, msgid, msgid_plural, n);
96 	return res;
97 }
98 
99 /**
100  * \}
101  */
102 
103 #ifdef __cplusplus
104 }  /* extern "C" */
105 #endif
106 
107 #endif /* SPA_I18N_H */
108