0

Since I've posted this question in several software forms and got no useful answer, I hope to get an answer here from you as my colleges.

If you program in WordPress, you might now translations files .po/.mo. I'm looking now for a plugin in IntelliJ / WebStorm that can generate .mo files and extend the .po files above each translation by the used places:

#: /includes/.......
msgid "Cheatin’ huh?"
msgstr "So geht das leider nicht.."

So does anyone knows if there is a plugin that can do this? It would be enough to not enter the usage each time manually because this annoys me so much. The generation would only be a nice to have.

Thanks a lot!

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Mr. Jo
  • 4,946
  • 6
  • 41
  • 100
  • 1
    https://plugins.jetbrains.com/plugin/7123-gnu-gettext-files-support--po- -- that's the only plugin that I know about. Do not know how well it works with WebStorm though. – LazyOne Nov 25 '20 at 21:21
  • I usually do that with poedit. Easy to use, low price for PRO version and it does what it has to do (posting as comment beause you asked for IntelliJ plugin and this would be an external tool) – Diego Nov 26 '20 at 08:35

1 Answers1

0

As of writing this answer there is still no plugin that does this seemlessly. However you can acheive automation using a GRUNT task.

Using Grunt requires you have a nodejs development environment configured

Create a package.json in your project root similar to

{
  "name": "yourProject",
  "majorVersion": "0",
  "minorVersion": "0",
  "incrementalVersion": "1",
  "qualifier": "-alpha",
  "version": "0.0.1-alpha",
  "description": "My great idea",
  "main": "GruntFile.js",
  "directories": {
    "test": "test"
  },
  "repository": {
    "type": "git",
    "url": "https://yourgitrepo.com/name/project.git"
  },
  "dependencies": {},
  "devDependencies": {
    "grunt": "^1.5.3",
    "grunt-cli": "^1.4.3",
    "grunt-contrib-clean": "^2.0.1",
    "grunt-contrib-copy": "^1.0.0",
    "grunt-exec": "^3.0.0",
    "grunt-pot": "^0.3.0",
    "grunt-replace": "^2.0.2",
    "load-grunt-tasks": "^5.1.0",
    "npm-cli": "^0.1.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Your Name",
  "license": "SEE LICENCE IN LICENCE.txt"
}

Once created navigate using a terminal and run

npm install

This creates a node environment.

Now create a GruntFile.js See below for example.

module.exports = function(grunt) {

    require('load-grunt-tasks')(grunt);

    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        prvPkg: grunt.file.readJSON('private.json'),
        clean: {
            standard: ['myproject/target',
                ],
        },
        makepot: {
            target: {
                options: {
                    cwd: 'myproject/target/generated/myproject',
                    include: ['.*'],
                    domainPath: 'languages/',             // Where to save the POT file.
                    mainFile: 'myproject.php',                 // Main project file.
                    potFilename: 'myproject.pot',              // Name of the POT file.
                    type: 'wp-plugin',
                    potHeaders: {
                        'report-msgid-bugs-to':  'https://www.myproject.com.au',
                        'plural-forms': 'nplurals=2; plural=n != 1;',
                        'last-translator': 'Author Name <author@myproject.com.au>',
                        'language-team': 'My Team <author@myproject.com.au>',
                        poedit:true,
                        'x-poedit-keywordslist': true,
                        'x-textdomain-support': 'yes'
                    }
                }
            }
        },
    });
    // Default task(s).
    grunt.registerTask('createMachineLanguage', '', function () {
        let exec = require('child_process').execSync;
        let options = {
            cwd: 'myproject/target/generated/myproject/languages/',
            encoding: 'utf8'
        }
        let result = exec("msginit --no-translator --input=myproject.pot --locale=en --output=myproject_en_AU.po",options);
        result += exec("msgfmt myproject_en_AU.po -o myproject_en_AU.mo",options);
        result += exec("rm myproject_en_AU.po",options);
        grunt.log.writeln(result);
    });
    grunt.registerTask('default',['clean:standard','makepot','createMachineLanguage'] );
}
The critical components are the grunt-pot grunt plugin. Your terminal environment must have the GNU gettext intalled which provides the function msginit and msgfmt. The grunt process executes these tasks automatically. Please note my environment was Ubuntu so you can see I use an rm to remove the po file once it is converted to an mo file.

So using the GruntTask plugin you can create your language files automatically. Executing them from GruntTask. Example of the GruntTask interface

Narrim
  • 567
  • 8
  • 22