Commit 7e29ea17 authored by Hugo Lefeuvre's avatar Hugo Lefeuvre Committed by Sébastien Blin

i18n: make chatview translatable

This patch implements i18n support in the chatview using Jed.

+ gettext style API, transparent for translators.
+ extract string using xgettext, translate po as usual.
+ convert po files to JSON using po2json.
  This has to be done once, by the Jenkins automatic i18n bump for
  instance. The build-chatview-locales script can be used to
  automate the .po -> .json convertion process.
+ po2json generated files are shipped as gresource and loaded into the
  chatview by webkitchatcontainer.

Gitlab: #900
Change-Id: Iaf925e2fd37174fff0b6139fc0019bda91938ace
Reviewed-by: Sébastien Blin's avatarSébastien Blin <sebastien.blin@savoirfairelinux.com>
parent 016e4b4e
......@@ -6,3 +6,9 @@ file_filter = po/<lang>.po
source_file = po/ring-client-gnome.pot
source_lang = en
type = PO
[jami.ring-client-gnome-chatviewpot]
file_filter = po/chatview/<lang>.po
source_file = po/chatview/ring-client-gnome-chatview.pot
source_lang = en
type = PO
web/chatview.html
# chatview translation files
Generate .pot as following:
xgettext -o ring-client-gnome-chatview.pot -L Javascript --from-code=utf-8 -D ../../ -f POTFILES.in
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-04-25 14:38+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: web/chatview.html:181
msgid "Hide chat view"
msgstr ""
#: web/chatview.html:182
msgid "Place video call"
msgstr ""
#: web/chatview.html:183
msgid "Place audio call"
msgstr ""
#: web/chatview.html:184
msgid "Add to conversations"
msgstr ""
#: web/chatview.html:185
msgid "Unban contact"
msgstr ""
#: web/chatview.html:186
msgid "Send"
msgstr ""
#: web/chatview.html:187
msgid "SendFile"
msgstr ""
#: web/chatview.html:188 web/chatview.html:933
msgid "Accept"
msgstr ""
#: web/chatview.html:189 web/chatview.html:943
msgid "Refuse"
msgstr ""
#: web/chatview.html:190
msgid "Block"
msgstr ""
#: web/chatview.html:194
msgid "Type a message"
msgstr ""
#: web/chatview.html:301
msgid "Note: an interaction will create a new contact."
msgstr ""
#: web/chatview.html:336
#, javascript-format
msgid "%s is not in your contacts"
msgstr ""
#: web/chatview.html:338
msgid ""
"Note: you can automatically accept this invitation by sending a message."
msgstr ""
#: web/chatview.html:515
#, javascript-format
msgid "%d days ago"
msgstr ""
#: web/chatview.html:518
msgid "one day ago"
msgstr ""
#: web/chatview.html:523
#, javascript-format
msgid "%d hours ago"
msgstr ""
#: web/chatview.html:526
msgid "one hour ago"
msgstr ""
#: web/chatview.html:531
#, javascript-format
msgid "%d minutes ago"
msgstr ""
#: web/chatview.html:534
msgid "just now"
msgstr ""
#: web/chatview.html:634
msgid "Sending"
msgstr ""
#: web/chatview.html:637
msgid "Failure"
msgstr ""
#: web/chatview.html:1286
msgid "Delete"
msgstr ""
#: web/chatview.html:1473
msgid "Retry"
msgstr ""
#!/bin/bash
# This script generates json data from chatview .po files and updates
# web.gresource.xml if needed.
PO2JSON="node-po2json"
FORMAT="jed1.x"
POPATH="po/chatview/"
GRESOURCE_PREFIX="i18n"
GRESOURCE_FILE="web/web.gresource.xml"
JSON_OUT="web/${GRESOURCE_PREFIX}"
function update_gresource {
# check gresource file: if there is no entry for passed .json file, create one
if [ -z "$(cat ${GRESOURCE_FILE} | grep ${1})" ]; then
echo "[W] no gresource entry for ${1}, adding it"
sed -i "s:<\!-- Locale -->:<\!-- Locale -->\n <file>${GRESOURCE_PREFIX}/${1}</file>:g" ${GRESOURCE_FILE}
fi
}
if ! [ -x "$(command -v ${PO2JSON})" ]; then
echo "[E] can't find ${PO2JSON}!"
exit 1
fi
if [ ! -d "${JSON_OUT}" ]; then
echo "[W] directory ${JSON_OUT} does not exist, creating it"
mkdir "${JSON_OUT}"
fi
for F in ${POPATH}/*.po
do
WITHOUT_PATH=$(basename -- "$F")
WITHOUT_EXT="${WITHOUT_PATH%.*}"
FILEPATH="${JSON_OUT}/${WITHOUT_EXT}.json"
echo "[I] building ${FILEPATH} from ${WITHOUT_PATH}"
${PO2JSON} $F ${FILEPATH} -f ${FORMAT}
update_gresource ${WITHOUT_EXT}.json
done
......@@ -349,6 +349,42 @@ webview_script_dialog(WebKitWebView *self,
return true;
}
static void
init_js_i18n(WebKitChatContainer *view)
{
auto locales = g_get_language_names();
gchar *function_call;
GBytes *locale_data;
int i = 0;
while (locales[i] != NULL) {
auto res = g_strdup_printf("/net/jami/JamiGnome/i18n/%s.json", locales[i]);
locale_data = g_resources_lookup_data(res, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
if (locale_data)
break;
i++;
}
if (!locale_data) {
/* no translation available for current locale, use default */
function_call = g_strdup("init_i18n()");
} else {
gsize size;
auto data = g_bytes_unref_to_data(locale_data, &size);
auto nul_terminated = g_strndup((char*) data, size);
function_call = g_strdup_printf("init_i18n(%s)", nul_terminated);
g_free(nul_terminated);
g_free(data);
}
webkit_chat_container_execute_js(view, function_call);
g_free(function_call);
}
static void
javascript_library_loaded(WebKitWebView *webview_chat,
GAsyncResult *result,
......@@ -385,6 +421,9 @@ javascript_library_loaded(WebKitWebView *webview_chat,
}
else
{
/* load translations before anything else */
init_js_i18n(self);
priv->js_libs_loaded = TRUE;
g_signal_emit(G_OBJECT(self), webkit_chat_container_signals[READY], 0);
......@@ -400,6 +439,7 @@ load_javascript_libs(WebKitWebView *webview_chat,
WebKitChatContainerPrivate *priv = WEBKIT_CHAT_CONTAINER_GET_PRIVATE(self);
/* Create the list of libraries to load */
priv->js_libs_to_load = g_list_append(priv->js_libs_to_load, (gchar*) "/net/jami/JamiGnome/jed.js");
priv->js_libs_to_load = g_list_append(priv->js_libs_to_load, (gchar*) "/net/jami/JamiGnome/linkify.js");
priv->js_libs_to_load = g_list_append(priv->js_libs_to_load, (gchar*) "/net/jami/JamiGnome/linkify-string.js");
priv->js_libs_to_load = g_list_append(priv->js_libs_to_load, (gchar*) "/net/jami/JamiGnome/linkify-html.js");
......
......@@ -229,7 +229,7 @@ body {
display: flex;
}
#navbar #addBannedContactButton, #navbar #addToConversationsButton {
#navbar #unbanButton, #navbar #addToConversationsButton {
display: none;
}
......@@ -237,7 +237,7 @@ body {
display: none;
}
#navbar.onBannedState #addBannedContactButton {
#navbar.onBannedState #unbanButton {
display: flex;
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -8,8 +8,11 @@
<file>linkify.js</file>
<file>linkify-string.js</file>
<file>linkify-html.js</file>
<file>jed.js</file>
<!-- CSS -->
<file>chatview.css</file>
<!-- Locale -->
</gresource>
</gresources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment