Search code examples
scalagradlegettextpoxgettext

Is there any way go get GNU xgettext (or something similar) to work with scala files?


We are using the GNU gettext utilities for creating PO files directly from our code base. The problem is - however - that some of our code base is written in Scala (most of it is Java). We are using gradle for building and deployment.

As far as I can see, Scala is not a supported language according to the xgettext documentation:

5.1.3 Choice of input file language

‘-L name’

‘--language=name’ Specifies the language of the input files.

The supported languages are C, C++, ObjectiveC, PO, Shell, Python, Lisp, EmacsLisp, librep, Scheme, Smalltalk, Java, JavaProperties, C#, awk, YCP, Tcl, Perl, PHP, GCC-source, NXStringTable, RST, RSJ, Glade, Lua, JavaScript, Vala, GSettings, Desktop.

Is there any way to use xgettext for Scala files? I have found this sbt compiler plugin that does what we need, but unfortunately we are using gradle instead of sbt.


Solution

  • The Perl module Locale::XGettext allows you to write your own xgettext variant for pretty much every text format. You only have to supply a parser for the language/format that is able to extract the strings.

    It is possible to write the parser in Java with minimal Perl boilerplate code around it. I have described the process in a blog post Extending Xgettext With Locale::XGettext. A sample extractor in Java is available at https://github.com/gflohr/Locale-XGettext/tree/master/samples/Java

    Disclaimer: I am the author of all the above-mentioned software and documents!

    If interfacing with Perl code sounds too complicated, you can also just write a parser for Scala in a language of your choice, run that parser before xgettext, and dump all translatable strings into a format that your version of xgettext understands, for example C:

    gettext("Hello, world!");
    gettext("Another string.");
    

    And then you simply use the generated dummy source file as input to xgettext along with the other source files inside your build setup.

    It may be worth noting that xgettext will always recognize .po and .pot files as input, even if you had invoked it with --language=java. That means, that .po or .pot files are suited best for dumping the translatable strings from your Scala file, provided that you find a way to generate .po/.pot files.

    Finally, the sources of the gettext tools are designed to be extensible. You can add the Scala language if you are able to write a string extractor and format checker for it in C. You cannot, however, add support for another language at runtime, but you have to compile your own version of xgettext or contribute your extractor to the GNU gettext project.