Repository: mesonbuild/meson

Issue:
i18n module doesn't help when merging back translations
At the moment we have to do:

    custom_target('desktop-file',
                  input: 'org.gnome.PowerStats.desktop.in',
                  output: 'org.gnome.PowerStats.desktop',
                  install: true,
                  install_dir: join_paths(get_option('datadir'), 'applications'),
                  command: [
                    msgfmt, '--desktop', '--template', '@INPUT@', '-d', podir, '-o', '@OUTPUT@'
                  ])

Which is pretty horrific for something that's going to be super common for desktop applications. It's also the same issue for AppData files, GSettings schema and lots of other file types.


Discussion:
So just an idea for the API if anybody has some thoughts:

```
i18n.merge_file('org.gnome.PowerStats.desktop.in', # Should we care about output, or just drop .in?
    type: 'desktop', # or 'xml',
    po_dir: '../po', # bit ugly
    install: true,
    install_dir: get_option('datadir'),
)
I think that is nearly perfect, but I think we do need input and output. Some people are going to be shipping meson and autotools build systems alongside each other for some time, and so the input file might actually be org.gnome.PowerStats.desktop.in.in. Or, it might be that the input file is called app.desktop (which is itself built from something else) and converted to com.mycompany.myapp.desktop when merging translations.
Seems reasonable.

Resolving patch:
diff --git a/mesonbuild/modules/i18n.py b/mesonbuild/modules/i18n.py
--- a/mesonbuild/modules/i18n.py
+++ b/mesonbuild/modules/i18n.py
@@ -14,10 +14,26 @@
 
 from os import path
 from .. import coredata, mesonlib, build
+from ..mesonlib import MesonException
 import sys
 
 class I18nModule:
 
+    def merge_file(self, state, args, kwargs):
+        podir = kwargs.pop('po_dir', None)
+        if not podir:
+            raise MesonException('i18n: po_dir is a required kwarg')
+        podir = path.join(state.build_to_src, state.subdir, podir)
+
+        file_type = kwargs.pop('type', 'xml')
+        VALID_TYPES = ('xml', 'desktop')
+        if not file_type in VALID_TYPES:
+            raise MesonException('i18n: "{}" is not a valid type {}'.format(file_type, VALID_TYPES))
+
+        kwargs['command'] = ['msgfmt', '--' + file_type,
+                             '--template', '@INPUT@', '-d', podir, '-o', '@OUTPUT@']
+        return build.CustomTarget(kwargs['output'] + '_merge', state.subdir, kwargs)
+
     @staticmethod
     def _read_linguas(state):
         linguas = path.join(state.environment.get_source_dir(), state.subdir, 'LINGUAS')


Test patch:
diff --git a/test cases/frameworks/6 gettext/data/meson.build b/test cases/frameworks/6 gettext/data/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/frameworks/6 gettext/data/meson.build	
@@ -0,0 +1,8 @@
+i18n.merge_file(
+  input: 'test.desktop.in',
+  output: 'test.desktop',
+  type: 'desktop',
+  po_dir: '../po',
+  install: true,
+  install_dir: join_paths(get_option('datadir'), 'applications')
+)
diff --git a/test cases/frameworks/6 gettext/data/test.desktop.in b/test cases/frameworks/6 gettext/data/test.desktop.in
new file mode 100644
--- /dev/null
+++ b/test cases/frameworks/6 gettext/data/test.desktop.in	
@@ -0,0 +1,6 @@
+[Desktop Entry]
+Name=Test
+GenericName=Application
+Comment=Test Application
+Type=Application
+
diff --git a/test cases/frameworks/6 gettext/installed_files.txt b/test cases/frameworks/6 gettext/installed_files.txt
--- a/test cases/frameworks/6 gettext/installed_files.txt	
+++ b/test cases/frameworks/6 gettext/installed_files.txt	
@@ -1,3 +1,4 @@
 usr/bin/intlprog
 usr/share/locale/de/LC_MESSAGES/intltest.mo
 usr/share/locale/fi/LC_MESSAGES/intltest.mo
+usr/share/applications/test.desktop
diff --git a/test cases/frameworks/6 gettext/meson.build b/test cases/frameworks/6 gettext/meson.build
--- a/test cases/frameworks/6 gettext/meson.build	
+++ b/test cases/frameworks/6 gettext/meson.build	
@@ -4,3 +4,4 @@ i18n = import('i18n')
 
 subdir('po')
 subdir('src')
+subdir('data')
diff --git a/test cases/frameworks/6 gettext/po/LINGUAS b/test cases/frameworks/6 gettext/po/LINGUAS
new file mode 100644
--- /dev/null
+++ b/test cases/frameworks/6 gettext/po/LINGUAS	
@@ -0,0 +1,2 @@
+de
+fi
diff --git a/test cases/frameworks/6 gettext/po/POTFILES b/test cases/frameworks/6 gettext/po/POTFILES
--- a/test cases/frameworks/6 gettext/po/POTFILES	
+++ b/test cases/frameworks/6 gettext/po/POTFILES	
@@ -1 +1,2 @@
 src/intlmain.c
+data/test.desktop