Vala + i18n + gtk, How to make a working app?

Viewed 293

I am trying to create a small demo program to understand how it works, gtk3 + i18n + meson, i would like to translate the window title _ ("Settings") from the normal string from english to italian thanks to it.po and window.vala, but running the sources the program is not translated where am I wrong?

this is the github repository : https://github.com/Fluid-DE/test

window.vala

public class Jarvis.Window : Gtk.ApplicationWindow {
    public GLib.Settings settings;

    public Window (Application app) {
        Object (
            application: app
        );
    }

    construct {
        title = _("Settings");
        //set_title( _("TEST") );
        window_position = Gtk.WindowPosition.CENTER;
        set_default_size (350, 80);

        settings = new GLib.Settings ("com.github.alecaddd.jarvis");

        move (settings.get_int ("pos-x"), settings.get_int ("pos-y"));
        resize (settings.get_int ("window-width"), settings.get_int ("window-height"));

        delete_event.connect (e => {
            return before_destroy ();
        });

        show_all ();
    }

    public bool before_destroy () {
        int width, height, x, y;

        get_size (out width, out height);
        get_position (out x, out y);

        settings.set_int ("pos-x", x);
        settings.set_int ("pos-y", y);
        settings.set_int ("window-width", width);
        settings.set_int ("window-height", height);

        return false;
    }
}

it.po

#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: com.github.alecaddd.jarvis\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-11-10 09:31+0000\n"
"PO-Revision-Date: 2020-10-23 16:15+0000\n"
"Last-Translator: Fabio Zaramella <fabiozaramella@hotmail.it>\n"
"Language-Team: Italian\n"
"Language: it\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Poedit 2.0.6\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-Basepath: ../src\n"
"X-Poedit-SearchPath-0: .\n"

#: src/MainWindow.vala:308
msgid "New note"
msgstr "Nuova nota"

#: src/MainWindow.vala:313
msgid "Delete note"
msgstr "Cancella nota"

#: src/MainWindow.vala:327
msgid "White"
msgstr "Bianco"

#: src/MainWindow.vala:338
msgid "Red"
msgstr "Rosso"

#: src/MainWindow.vala:349
msgid "Orange"
msgstr "Oracione"

#: src/MainWindow.vala:360
msgid "Yellow"
msgstr "Giallo"

#: src/MainWindow.vala:371
msgid "Green"
msgstr "Verde"

#: src/MainWindow.vala:382
msgid "Blue"
msgstr "Blu"

#: src/MainWindow.vala:432
msgid "Note Color"
msgstr "Colore Nota"

#: src/MainWindow.vala:448
msgid "Settings"
msgstr "Impostazioni"

meson.build

###########
# Project #
###########

project('com.github.alecaddd.jarvis', ['c', 'vala'],
           version: '0.alpha',
     meson_version: '>= 0.53.0'
)


###########
# Version #
###########

project_version = meson.project_version()
version_array = project_version.split('.')
project_major_version = version_array[0].to_int()
project_minor_version = version_array[1]


#################
# Default paths #
#################

project_prefix = get_option('prefix')
project_bindir = join_paths(project_prefix, get_option('bindir'))
project_localedir = join_paths(project_prefix, get_option('localedir'))
project_datadir = join_paths(project_prefix, get_option('datadir'))
project_pkgdatadir = join_paths(project_datadir, meson.project_name())
project_schemadir = join_paths(project_datadir, 'glib-2.0', 'schemas')


###########
# Options #
###########

#Compiler

vala_version_required = '0.48.2'
vala = meson.get_compiler('vala')
cc = meson.get_compiler('c')

if not vala.version().version_compare('>= @0@'.format(vala_version_required))
    error('Valac >= @0@ required!'.format(vala_version_required))
endif

#Argument

add_project_arguments([
    '-DGETTEXT_PACKAGE="' + meson.project_name() + '"',
    ],
    language:'c'
)

add_project_arguments([
        '--vapidir', join_paths(meson.current_source_dir(), 'vapi'),
        '--pkg', 'config',
    ],
    language: 'vala',
)

#Configuration file

config_h = configuration_data()

package_bugreport = 'http://bugzilla.gnome.org/enter_bug.cgi?product=' + meson.project_name()

# package
set_defines = [
  ['PACKAGE', meson.project_name()],
  ['PACKAGE_BUGREPORT', package_bugreport],
  ['PACKAGE_NAME', meson.project_name()],
  ['PACKAGE_STRING', '@0@ @1@'.format(meson.project_name(), project_version)],
  ['PACKAGE_TARNAME', meson.project_name()],
  ['PACKAGE_URL', 'https://wiki.gnome.org/Apps/Calendar'],
  ['PACKAGE_VERSION', project_version],
  ['VERSION', project_version],
  ['GETTEXT_PACKAGE', meson.project_name()],
  ['APPLICATION_ID', meson.project_name()]
]

foreach define: set_defines
  config_h.set_quoted(define[0], define[1])
endforeach

# Compiler flags

config_data = configuration_data()
config_data.set('version', meson.project_version())
config_data.set('testdata_dir', join_paths(meson.source_root(), 'data', 'tests'))
config_data.set('app_name', meson.project_name())

config_file = configure_file(
         output: 'config.h',
  configuration: config_h
)

project_config_dep = declare_dependency(
    sources: config_file,
    include_directories: include_directories('.')
)

################
# Dependencies #
################

glib_dep = dependency('glib-2.0', version: '>= 2.58.0')
gtk_dep = dependency('gtk+-3.0', version: '>= 3.22.20')
gio_dep = dependency('gio-2.0', version: '>= 2.58.0')

gnome = import('gnome')
i18n = import('i18n')
pkg = import('pkgconfig')

top_inc = include_directories('.')

data_dir = join_paths(meson.source_root(), 'data')
po_dir = join_paths(meson.source_root(), 'po')
src_dir = join_paths(meson.source_root(), 'src')


###########
# Subdirs #
###########

subdir('src')
subdir('po')
subdir('data')

meson.add_install_script('meson/post_install.py')
1 Answers

In your class where the main method is, you should tell the gettext three things:

  1. the translation domain, usually the same as the application name

  2. the location where the compiled translations will be installed

  3. the character encoding of translations, usually UTF-8.

In my case I do this:

     Intl.setlocale (LocaleCategory.ALL, "");
     string langpack_dir = Path.build_filename (Constants.APP_INSTALL_PREFIX, "share", "locale");
     Intl.bindtextdomain (Constants.APP_ID, langpack_dir);
     Intl.bind_textdomain_codeset (Constants.APP_ID, "UTF-8");
     Intl.textdomain (Constants.APP_ID);

Replace the Constants values with your Config.h file

After doing this you need to regenerate the translation files.

In Gnome Developer have more information about this

Related