-
Notifications
You must be signed in to change notification settings - Fork 111
/
as-translation.c
279 lines (250 loc) · 7.16 KB
/
as-translation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2016-2024 Matthias Klumpp <matthias@tenstral.net>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the license, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "as-translation.h"
#include "as-translation-private.h"
#include <config.h>
#include <glib.h>
#include "as-utils-private.h"
/**
* SECTION:as-translation
* @short_description: Description of translation domains for an upstream component.
* @include: appstream.h
*
* Describes the translation domain and translation system used by the upstream component.
* See [the specification](https://www.freedesktop.org/software/appstream/docs/chap-Metadata.html#tag-translation) for
* more information on the corresponding XML tag.
*
* See also: #AsComponent
*/
typedef struct {
AsTranslationKind kind;
GRefString *id;
GRefString *source_locale;
} AsTranslationPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (AsTranslation, as_translation, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (as_translation_get_instance_private (o))
/**
* as_translation_kind_to_string:
* @kind: the #AsTranslationKind.
*
* Converts the enumerated value to a text representation.
*
* Returns: string version of @kind
**/
const gchar *
as_translation_kind_to_string (AsTranslationKind kind)
{
if (kind == AS_TRANSLATION_KIND_GETTEXT)
return "gettext";
if (kind == AS_TRANSLATION_KIND_QT)
return "qt";
return "unknown";
}
/**
* as_translation_kind_from_string:
* @kind_str: the string.
*
* Converts the text representation to an enumerated value.
*
* Returns: a #AsTranslationKind or %AS_TRANSLATION_KIND_UNKNOWN for unknown
**/
AsTranslationKind
as_translation_kind_from_string (const gchar *kind_str)
{
if (g_strcmp0 (kind_str, "gettext") == 0)
return AS_TRANSLATION_KIND_GETTEXT;
if (g_strcmp0 (kind_str, "qt") == 0)
return AS_TRANSLATION_KIND_QT;
return AS_TRANSLATION_KIND_UNKNOWN;
}
/**
* as_translation_init:
**/
static void
as_translation_init (AsTranslation *tr)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
priv->kind = AS_TRANSLATION_KIND_UNKNOWN;
}
/**
* as_translation_finalize:
**/
static void
as_translation_finalize (GObject *object)
{
AsTranslation *tr = AS_TRANSLATION (object);
AsTranslationPrivate *priv = GET_PRIVATE (tr);
as_ref_string_release (priv->id);
as_ref_string_release (priv->source_locale);
G_OBJECT_CLASS (as_translation_parent_class)->finalize (object);
}
/**
* as_translation_class_init:
**/
static void
as_translation_class_init (AsTranslationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = as_translation_finalize;
}
/**
* as_translation_get_kind:
* @tr: a #AsTranslation instance.
*
* The translation system type.
*
* Returns: an enum of type #AsTranslationKind
*/
AsTranslationKind
as_translation_get_kind (AsTranslation *tr)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
return priv->kind;
}
/**
* as_translation_set_kind:
* @tr: a #AsTranslation instance.
* @kind: the new #AsTranslationKind
*
* Set the translation system type.
*/
void
as_translation_set_kind (AsTranslation *tr, AsTranslationKind kind)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
priv->kind = kind;
}
/**
* as_translation_get_id:
* @tr: a #AsTranslation instance.
*
* The ID (e.g. Gettext translation domain) of this translation.
*/
const gchar *
as_translation_get_id (AsTranslation *tr)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
return priv->id;
}
/**
* as_translation_set_id:
* @tr: a #AsTranslation instance.
* @id: The ID of this translation.
*
* Set the ID (e.g. Gettext domain) of this translation.
*/
void
as_translation_set_id (AsTranslation *tr, const gchar *id)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
as_ref_string_assign_safe (&priv->id, id);
}
/**
* as_translation_get_source_locale:
* @tr: a #AsTranslation instance.
*
* The locale of the source strings for this component. If this has not been
* explicitly specified, `en_US` will be returned.
*
* Returns: (not nullable): The locale of the source strings for this component.
* Since: 0.14.6
*/
const gchar *
as_translation_get_source_locale (AsTranslation *tr)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
return (priv->source_locale != NULL) ? priv->source_locale : "en_US";
}
/**
* as_translation_set_source_locale:
* @tr: a #AsTranslation instance.
* @locale: (nullable): The POSIX locale that the source strings are in, or %NULL if unknown or default.
*
* Set the locale of the source strings for this component. In gettext, this is
* referred to as the `C` locale. It’s almost always `en_US`, but for some
* components it may not be.
*
* Since: 0.14.6
*/
void
as_translation_set_source_locale (AsTranslation *tr, const gchar *locale)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
as_ref_string_assign_safe (&priv->source_locale, locale);
}
/**
* as_translation_load_from_xml:
* @tr: a #AsTranslation instance.
* @ctx: the AppStream document context.
* @node: the XML node.
* @error: a #GError.
*
* Loads data from an XML node.
**/
gboolean
as_translation_load_from_xml (AsTranslation *tr, AsContext *ctx, xmlNode *node, GError **error)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
g_autofree gchar *prop = NULL;
g_autofree gchar *content = NULL;
prop = as_xml_get_prop_value (node, "type");
priv->kind = as_translation_kind_from_string (prop);
if (priv->kind == AS_TRANSLATION_KIND_UNKNOWN)
return FALSE;
as_ref_string_assign_transfer (&priv->source_locale,
as_xml_get_prop_value_refstr (node, "source_locale"));
content = as_xml_get_node_value (node);
as_translation_set_id (tr, content);
return TRUE;
}
/**
* as_translation_to_xml_node:
* @tr: a #AsTranslation instance.
* @ctx: the AppStream document context.
* @root: XML node to attach the new nodes to.
*
* Serializes the data to an XML node.
**/
void
as_translation_to_xml_node (AsTranslation *tr, AsContext *ctx, xmlNode *root)
{
AsTranslationPrivate *priv = GET_PRIVATE (tr);
xmlNode *n;
/* the translations tag is only valid in metainfo files */
if (as_context_get_style (ctx) != AS_FORMAT_STYLE_METAINFO)
return;
n = as_xml_add_text_node (root, "translation", priv->id);
as_xml_add_text_prop (n, "type", as_translation_kind_to_string (priv->kind));
as_xml_add_text_prop (n, "source_locale", priv->source_locale);
}
/**
* as_translation_new:
*
* Creates a new #AsTranslation.
*
* Returns: (transfer full): a #AsTranslation
**/
AsTranslation *
as_translation_new (void)
{
AsTranslation *tr;
tr = g_object_new (AS_TYPE_TRANSLATION, NULL);
return AS_TRANSLATION (tr);
}