Skip to content

Commit

Permalink
lua: initial gettext support
Browse files Browse the repository at this point in the history
This works completely for DT-provided lua scripts

translation of user scripts need a proper documentation on how to create
and handle po and mo file. Future work in the user manual
  • Loading branch information
Jérémy Rosen committed Jul 31, 2015
1 parent fb15ab6 commit 7d26924
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 1 deletion.
193 changes: 193 additions & 0 deletions doc/usermanual/lua/lua_api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2917,6 +2917,199 @@ NOTE2: If the parameter is a directory the call is non-blocking; the film object

</section>

<section status="final" id="darktable_gettext">
<title>darktable.gettext</title>
<indexterm>
<primary>Lua API</primary>
<secondary>gettext</secondary>
</indexterm>
<synopsis>table</synopsis>
<para>This table contains functions related to translating lua scripts</para>

<section status="final" id="darktable_gettext_gettext">
<title>darktable.gettext.gettext</title>
<indexterm>
<primary>Lua API</primary>
<secondary>gettext</secondary>
</indexterm>
<synopsis>function(
<emphasis><link linkend="darktable_gettext_gettext_msgid">msgid</link></emphasis> : string
) : string</synopsis>
<para>Translate a string using the darktable textdomain</para>

<variablelist>
<varlistentry id="darktable_gettext_gettext_msgid"><term>msgid</term><listitem>
<synopsis>string</synopsis>
<para>The string to translate</para>

</listitem></varlistentry>

<varlistentry><term><emphasis>return</emphasis></term><listitem>
<synopsis>string</synopsis>
<para>The translated string</para>

</listitem></varlistentry>


</variablelist>
</section>

<section status="final" id="darktable_gettext_dgettext">
<title>darktable.gettext.dgettext</title>
<indexterm>
<primary>Lua API</primary>
<secondary>dgettext</secondary>
</indexterm>
<synopsis>function(
<emphasis><link linkend="darktable_gettext_dgettext_domainname">domainname</link></emphasis> : string,
<emphasis><link linkend="darktable_gettext_dgettext_msgid">msgid</link></emphasis> : string
) : string</synopsis>
<para>Translate a string using the specified textdomain</para>

<variablelist>
<varlistentry id="darktable_gettext_dgettext_domainname"><term>domainname</term><listitem>
<synopsis>string</synopsis>
<para>The domain to use for that translation</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_dgettext_msgid"><term>msgid</term><listitem>
<synopsis>string</synopsis>
<para>The string to translate</para>

</listitem></varlistentry>

<varlistentry><term><emphasis>return</emphasis></term><listitem>
<synopsis>string</synopsis>
<para>The translated string</para>

</listitem></varlistentry>


</variablelist>
</section>

<section status="final" id="darktable_gettext_ngettext">
<title>darktable.gettext.ngettext</title>
<indexterm>
<primary>Lua API</primary>
<secondary>ngettext</secondary>
</indexterm>
<synopsis>function(
<emphasis><link linkend="darktable_gettext_ngettext_msgid">msgid</link></emphasis> : string,
<emphasis><link linkend="darktable_gettext_ngettext_msgid_plural">msgid_plural</link></emphasis> : string,
<emphasis><link linkend="darktable_gettext_ngettext_n">n</link></emphasis> : int
) : string</synopsis>
<para>Translate a string depending on the number of objects using the darktable textdomain</para>

<variablelist>
<varlistentry id="darktable_gettext_ngettext_msgid"><term>msgid</term><listitem>
<synopsis>string</synopsis>
<para>The string to translate</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_ngettext_msgid_plural"><term>msgid_plural</term><listitem>
<synopsis>string</synopsis>
<para>The string to translate in plural form</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_ngettext_n"><term>n</term><listitem>
<synopsis>int</synopsis>
<para>The number of objetc</para>

</listitem></varlistentry>

<varlistentry><term><emphasis>return</emphasis></term><listitem>
<synopsis>string</synopsis>
<para>The translated string</para>

</listitem></varlistentry>


</variablelist>
</section>

<section status="final" id="darktable_gettext_dngettext">
<title>darktable.gettext.dngettext</title>
<indexterm>
<primary>Lua API</primary>
<secondary>dngettext</secondary>
</indexterm>
<synopsis>function(
<emphasis><link linkend="darktable_gettext_dngettext_domainname">domainname</link></emphasis> : string,
<emphasis><link linkend="darktable_gettext_dngettext_msgid">msgid</link></emphasis> : string,
<emphasis><link linkend="darktable_gettext_dngettext_msgid_plural">msgid_plural</link></emphasis> : string,
<emphasis><link linkend="darktable_gettext_dngettext_n">n</link></emphasis> : int,
<emphasis><link linkend="darktable_gettext_dngettext_domainname">domainname</link></emphasis> : string,
<emphasis><link linkend="darktable_gettext_dngettext_dirname">dirname</link></emphasis> : string
) : string</synopsis>
<para>Translate a string depending on the number of objects using the specified textdomain</para>

<variablelist>
<varlistentry id="darktable_gettext_dngettext_domainname"><term>domainname</term><listitem>
<synopsis>string</synopsis>
<para>The domain to use for that translation</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_dngettext_msgid"><term>msgid</term><listitem>
<synopsis>string</synopsis>
<para>The string to translate</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_dngettext_msgid_plural"><term>msgid_plural</term><listitem>
<synopsis>string</synopsis>
<para>The string to translate in plural form</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_dngettext_n"><term>n</term><listitem>
<synopsis>int</synopsis>
<para>The number of objetc</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_dngettext_domainname"><term>domainname</term><listitem>
<synopsis>string</synopsis>
<para>The domain to use for that translation</para>

</listitem></varlistentry>

<varlistentry id="darktable_gettext_dngettext_dirname"><term>dirname</term><listitem>
<synopsis>string</synopsis>
<para>The base directory to look for the file. The file should be placed in <dirname>/<locale name>/LC_MESSAGES/<domain>.mo</para>

</listitem></varlistentry>

<varlistentry><term><emphasis>return</emphasis></term><listitem>
<synopsis>string</synopsis>
<para>The translated string</para>

</listitem></varlistentry>


</variablelist>
</section>

<section status="final" id="darktable_gettext_bindtextdomain">
<title>darktable.gettext.bindtextdomain</title>
<indexterm>
<primary>Lua API</primary>
<secondary>bindtextdomain</secondary>
</indexterm>
<synopsis>function(
)</synopsis>
<para>Tell gettext where to find the .mo file translating messages for a particular domain</para>

<variablelist>
</variablelist>
</section>

</section>

<section status="final" id="darktable_debug">
<title>darktable.debug</title>
<indexterm>
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ if(USE_LUA)
"lua/events.c"
"lua/film.c"
"lua/format.c"
"lua/gettext.c"
"lua/glist.c"
"lua/gui.c"
"lua/image.c"
Expand Down
91 changes: 91 additions & 0 deletions src/lua/gettext.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
This file is part of darktable,
copyright (c) 2015 Jeremy Rosen
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lua/lua.h"
#include "libintl.h"
// function used by the lua interpreter to load darktable

static int lua_gettext(lua_State*L)
{
const char* msgid = luaL_checkstring(L,1);
lua_pushstring(L,gettext(msgid));
return 1;
}

static int lua_dgettext(lua_State*L)
{
const char* domainname = luaL_checkstring(L,1);
const char* msgid = luaL_checkstring(L,2);
lua_pushstring(L,dgettext(domainname,msgid));
return 1;
}

static int lua_ngettext(lua_State*L)
{
const char* msgid = luaL_checkstring(L,1);
const char* msgid_plural = luaL_checkstring(L,2);
int n = luaL_checkint(L,3);
lua_pushstring(L,ngettext(msgid,msgid_plural,n));
return 1;
}

static int lua_dngettext(lua_State*L)
{
const char* domainname = luaL_checkstring(L,1);
const char* msgid = luaL_checkstring(L,2);
const char* msgid_plural = luaL_checkstring(L,3);
int n = luaL_checkint(L,4);
lua_pushstring(L,dngettext(domainname,msgid,msgid_plural,n));
return 1;
}

static int lua_bindtextdomain(lua_State*L)
{
const char* domainname = luaL_checkstring(L,1);
const char* dirname = luaL_checkstring(L,2);
bindtextdomain(domainname,dirname);
return 0;
}


int dt_lua_init_gettext(lua_State *L)
{

dt_lua_push_darktable_lib(L);
dt_lua_goto_subtable(L,"gettext");

lua_pushcfunction(L,lua_gettext);
lua_setfield(L,-2,"gettext");
lua_pushcfunction(L,lua_dgettext);
lua_setfield(L,-2,"dgettext");
//lua_pushcfunction(L,lua_dcgettext);
//lua_setfield(L,-2,"dcgettext");
lua_pushcfunction(L,lua_ngettext);
lua_setfield(L,-2,"ngettext");
lua_pushcfunction(L,lua_dngettext);
lua_setfield(L,-2,"dngettext");
//lua_pushcfunction(L,lua_dcngettext);
//lua_setfield(L,-2,"dcngettext");
lua_pushcfunction(L,lua_bindtextdomain);
lua_setfield(L,-2,"bindtextdomain");

lua_pop(L,1);
return 0;
}
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;
26 changes: 26 additions & 0 deletions src/lua/gettext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of darktable,
copyright (c) 2015 Jeremy Rosen
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DT_LUA_GETTEXT_H
#define DT_LUA_GETTEXT_H

int dt_lua_init_gettext(lua_State *L);

#endif
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;
3 changes: 2 additions & 1 deletion src/lua/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "lua/preferences.h"
#include "lua/print.h"
#include "lua/types.h"
#include "lua/gettext.h"
#include "lua/tags.h"
#include "lua/modules.h"
#include "lua/luastorage.h"
Expand Down Expand Up @@ -128,7 +129,7 @@ static lua_CFunction init_funcs[]
dt_lua_init_configuration, dt_lua_init_preferences, dt_lua_init_database, dt_lua_init_gui,
dt_lua_init_luastorages, dt_lua_init_tags, dt_lua_init_film, dt_lua_init_call,
dt_lua_init_view, dt_lua_init_events, dt_lua_init_init, dt_lua_init_widget,
dt_lua_init_lualib, NULL };
dt_lua_init_lualib, dt_lua_init_gettext, NULL };


void dt_lua_init(lua_State *L, const char *lua_command)
Expand Down
22 changes: 22 additions & 0 deletions tools/lua_doc/content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,28 @@ darktable.control.dispatch:set_text([[Runs a function in the background. This fu
darktable.control.dispatch:add_parameter("function","function",[[The call to dispatch]])
darktable.control.dispatch:add_parameter("...","anything",[[extra parameters to pass to the function]])

darktable.gettext:set_text([[This table contains functions related to translating lua scripts]])
darktable.gettext.gettext:set_text([[Translate a string using the darktable textdomain]])
darktable.gettext.gettext:add_parameter("msgid","string","The string to translate");
darktable.gettext.gettext:add_return("string","The translated string");
darktable.gettext.dgettext:set_text([[Translate a string using the specified textdomain]])
darktable.gettext.dgettext:add_parameter("domainname","string","The domain to use for that translation");
darktable.gettext.dgettext:add_parameter("msgid","string","The string to translate");
darktable.gettext.dgettext:add_return("string","The translated string");
darktable.gettext.ngettext:set_text([[Translate a string depending on the number of objects using the darktable textdomain]])
darktable.gettext.ngettext:add_parameter("msgid","string","The string to translate");
darktable.gettext.ngettext:add_parameter("msgid_plural","string","The string to translate in plural form");
darktable.gettext.ngettext:add_parameter("n","int","The number of objetc");
darktable.gettext.ngettext:add_return("string","The translated string");
darktable.gettext.dngettext:set_text([[Translate a string depending on the number of objects using the specified textdomain]])
darktable.gettext.dngettext:add_parameter("domainname","string","The domain to use for that translation");
darktable.gettext.dngettext:add_parameter("msgid","string","The string to translate");
darktable.gettext.dngettext:add_parameter("msgid_plural","string","The string to translate in plural form");
darktable.gettext.dngettext:add_parameter("n","int","The number of objetc");
darktable.gettext.dngettext:add_return("string","The translated string");
darktable.gettext.bindtextdomain:set_text([[Tell gettext where to find the .mo file translating messages for a particular domain]])
darktable.gettext.dngettext:add_parameter("domainname","string","The domain to use for that translation");
darktable.gettext.dngettext:add_parameter("dirname","string","The base directory to look for the file. The file should be placed in <dirname>/<locale name>/LC_MESSAGES/<domain>.mo");

----------------------
-- DARKTABLE.DEBUG --
Expand Down

0 comments on commit 7d26924

Please sign in to comment.