Inkscape.org
Creating New Extensions Example Inkscape extension with localization / translation
  1. #1
    Nikki.Smith Nikki.Smith @Nikki.Smith
    *

    I've been adding localization to an extension, but struggled to find all the information in one place, especially as someone who'd never used gettext before. This is my attempt to pull together some helpful documentation, including where the translation files go and what gettext commands to use on both Windows and Linux.

    Please let me know if you find any mistakes, or can suggest improvements.

    How to install

    To manually install this extension, download and unpack the archive file. Copy the examples folder to the location listed by the Inkscape menu:

      Edit > Preferences > System: User extensions

    For Windows users this will be something like:

      %APPDATA%\inkscape\extensions

    Restart Inkscape and the new extension will be available.

    Image + Localize (translation)

    A minimal extension in which the Python script does nothing except close the window. The ImageLocalize().effect() method is called when the user clicks "Apply".

    All of the 'magic' is in the image_localize.inx file which describes the user interface, specifically what appears in the window when you open the extension. The image is loaded by <image>. See INX widgets and parameters

    Support for multiple languages is set in the .inx file using:

      <inkscape-extension translationdomain="image_localize"...

    which tells Inkscape to load a compiled translation .mo file. eg, for proper English (Great Britain) it would be locale\en_GB\LC_MESSAGES\image_localize.mo. The extension includes a set of (very short!) translation files, one for each language it supports. See Translation of extensions

    To try different languages in Inkscape, use the menu:

      Edit > Preferences > Interface: Language

    Creating and managing many translation files on a large project can be quite involved, and there are various GUI applications and web sites such as Poedit and POEditor that aim to make localization easier. However for a small extension it is easy enough just on the command line, and using a plain text editor:

    Windows Powershell

    # fetch tools
    cd $env:APPDATA\inkscape\extensions\examples
    wget https://github.com/vslavik/gettext-tools-windows/releases/download/v0.22.5/gettext-tools-windows-0.22.5.zip -OutFile gettext-tools-windows-0.22.5.zip
    wget https://gitlab.com/inkscape/inkscape/-/raw/master/po/its/inx.its -OutFile inx.its
    Expand-Archive -LiteralPath gettext-tools-windows-0.22.5.zip -DestinationPath gettext
    
    # make a fresh .pot from this extension's .inx
    gettext\bin\xgettext image_localize.inx --its=inx.its -o locale\en_GB\LC_MESSAGES\image_localize.pot
    
    # human translator edits .pot with English (Great Britain)
    notepad locale\en_GB\LC_MESSAGES\image_localize.pot
    
    # compile .pot into final .mo file, used by Inkscape
    gettext\bin\msgfmt --check -o locale\en_GB\LC_MESSAGES\image_localize.mo locale\en_GB\LC_MESSAGES\image_localize.pot

    Windows users will probably want to move the gettext folder of command line tools somewhere more central.

    Linux shell

    # fetch tools
    cd ~/.config/inkscape/extensions/examples
    sudo apt-get install gettext
    wget -O inx.its https://gitlab.com/inkscape/inkscape/-/raw/master/po/its/inx.its
    
    # make a fresh .pot from this extension's .inx
    xgettext image_localize.inx --its=inx.its -o locale/en_GB/LC_MESSAGES/image_localize.pot
    
    # human translator edits .pot with English (Great Britain)
    editor locale/en_GB/LC_MESSAGES/image_localize.pot
    
    # compile .pot into final .mo file, used by Inkscape
    msgfmt --check -o locale/en_GB/LC_MESSAGES/image_localize.mo locale/en_GB/LC_MESSAGES/image_localize.pot

    When editing an image_localize.pot file, the human translator completes msgstr "" with the local translation for each piece of text. eg,

      msgid "My favorite color for tomatoes is red."
      msgstr ""msgstr "My favourite colour for tomatoes is red."

    Note that you wouldn't normally include inx.its and the per-language image_localize.pot files when distributing an extension: they are only needed for compiling the final .mo files.

    Thank you to Theo Asimakopoulos for the Greek translation

  2. #2
    Nikki.Smith Nikki.Smith @Nikki.Smith
    *

    Just tested on the newly released Inkscape v1.4, and no problems

  3. #3
    Nikki.Smith Nikki.Smith @Nikki.Smith

    Now includes localized text in the Python script. Also many small changes that result in a much higher pylint code score.

    Download here: https://inkscape.org/~Nikki.Smith/%E2%98%85localization-translation-example-extension

  4. #4
    FrankI4466 FrankI4466 @FrankI4466
    *

    Thank's to you that help me.

    I'm using vscode to code my extension. I just ask to copilot to create pot file with a prompt like this (inx file open) :

    Can you create a .pot file for me in which you would put all the strings in this file to be translated preceded by msgid and all the strings translated into English (us) preceded by msgstr. Remember to avoid duplicate strings. Just give me the result.

    and add a command in json file : 

        "runOnSave.commands": [
            {
                "match": "\\.pot$",
                "nomatch": ".*none.*",
                "command": "msgfmt --check -o ${fileDirname}\\${fileBasenameNoExtension}.mo ${file}",
                "runIn": "terminal",
            }
        ]

    With run on save extension by Pucelle.

    Then the .mo file is automatically created.

    I copy a pot file with blank mgsstr in a folder named none to allow other than english to translate for their country. 

Inkscape Inkscape.org Inkscape Forum Creating New Extensions Example Inkscape extension with localization / translation