script: Support translatable strings for properties

ClutterScript should be able to automatically call gettext() and friends
on strings loaded from a UI definition, prior to passing the string to
the object it is constructing.

The basic implementation is trivial:

  - set a translation domain on the ClutterScript instance
  - mark the translatable strings inside the JSON data, like:

      "property" : {
        "translatable" : true,
        "string" : "a translatable string"
      }

The hard part is now getting the tools we use to extract the
translatable strings to understand the JSON format we use inside
ClutterScript.
This commit is contained in:
Emmanuele Bassi
2012-03-06 14:23:33 +00:00
parent 4a9414ff87
commit 7646404196
5 changed files with 162 additions and 2 deletions

View File

@ -1088,6 +1088,54 @@ clutter_script_parser_parse_end (JsonParser *parser)
clutter_script_ensure_objects (CLUTTER_SCRIPT_PARSER (parser)->script);
}
gboolean
_clutter_script_parse_translatable_string (ClutterScript *script,
JsonNode *node,
char **str)
{
JsonObject *obj;
const char *string, *domain;
const char *res;
gboolean translatable;
if (!JSON_NODE_HOLDS_OBJECT (node))
return FALSE;
obj = json_node_get_object (node);
if (!(json_object_has_member (obj, "translatable") &&
json_object_has_member (obj, "string")))
return FALSE;
translatable = json_object_get_boolean_member (obj, "translatable");
string = json_object_get_string_member (obj, "string");
if (string == NULL || *string == '\0')
return FALSE;
if (json_object_has_member (obj, "domain"))
domain = json_object_get_string_member (obj, "domain");
else
domain = NULL;
if (domain == NULL || *domain == '\0')
domain = clutter_script_get_translation_domain (script);
if (translatable)
{
if (domain != NULL && *domain != '\0')
res = g_dgettext (domain, string);
else
res = gettext (string);
}
else
res = string;
if (str != NULL)
*str = g_strdup (res);
return TRUE;
}
gboolean
_clutter_script_parse_node (ClutterScript *script,
GValue *value,
@ -1203,6 +1251,16 @@ _clutter_script_parse_node (ClutterScript *script,
return TRUE;
}
}
else if (p_type == G_TYPE_STRING)
{
char *str = NULL;
if (_clutter_script_parse_translatable_string (script, node, &str))
{
g_value_take_string (value, str);
return TRUE;
}
}
}
return FALSE;