We can add a resourcebundle property file to our application to support internationalization (i18n). The file contains key-value pairs where the value is a localized value per supported language or locale. In IntelliJ IDEA we can easily see which keys are not yet translated. We open a resourcebundle property file and click on the
ResourceBundle tab at the bottom of the editor. We get a list of all available keys on the left and on the right a text area per supported language with the translated values. If a key is not translated for all supported languages it will be colored in red. We only have to select the keys in red and fill in the values on the right for the given languages.
Sample with IntelliJ IDEA 13.1.1
We can use the
run-script command to run Groovy scripts within the context of a Grails application. We can pass one or more Groovy scripts as argument to the
run-script command. The Grails environment will be configured and we can access the Spring application context, domain classes, Grails services and more. Basically everything we can do in the Grails console or shell can be saved as a Groovy script and run with the
In a previous blog post we have seen how we can use a
BaseScript AST transformation to set a base script class for running scripts. Since Groovy 2.3 we can apply the
@BaseScript annotation on
import statements. Also we can implement a
run method in our
Script class in which we call an abstract method. The abstract method will actually run the script, so we can execute code before and after the script code runs by implementing logic in the
Since Groovy 2.3 we can use the
@Sortable annotation to make a class implement the
Comparable interface. Also new
comparator methods are added. All properties of a class are used to implement the
compareTo method. The order of the properties determines the priority used when sorting. With the annotation parameters
excludes we can define which properties of the class need to be used to implement the
We have seen some features of the
@Builder AST transformation in previous and other blog post. We can use another strategy to let the AST transformation generate a class where the class only has a constructor that accepts a builder class. And with
@CompileStatic we can even make sure that all required properties must have a value set by the builder before the constructor accepts the argument. We use the
builderStrategy annotation parameter and set it to
In a previous post we learned about the new
@Builder AST transformation introduced in Groovy 2.3. We applied to the annotation to our class files and we got a nice fluent API to set property values. But what if we cannot change the class itself, for example if we want to create a fluent API for classes in an external library. Then we can still use the
@Builder AST transformation but we use a different strategy. We can define the builder strategy via a annotation parameter.
Since Groovy 2.3 we can easily create a fluent API for our classes with the
@Builder AST transformation. We can apply the annotation to our classes and the resulting class file will have all the necessary methods to support a fluent API. We can customize how the fluent API is generated with different annotation parameters. In Groovy code we already can use the
with method to have a clean way to set property values or use the named constructor arguments. But if our classes need to be used from Java it is nice to give the Java developers a fluent API for our Groovy classes.
Groovy adds a lot of extra methods to the
File object to work with the contents or find and filter files in a directory. These methods are now also added to the
java.nio.file.Path class since Groovy 2.3.
Since Groovy 1.8 we can use the
trampoline method for a closure to get better recursive behavior for tail recursion. All closure invocations are then invoked sequentially instead of stacked, so there is no
StackOverFlowError. As from Groovy 2.3 we can use this for recursive methods as well with the
@TailRecursive AST transformation. If we apply the annotation to our method with tail recursion the method invocations will be sequential and not stacked, like with the closure’s
Writing documentation with Asciidoc is such a treat. We can add markers to our code where we want to explain something in our code. The markers have numbers and are enclosed in
> brackets. The explanation for the markers follows a code listing in a callout list. Here we use the same marker and add extra text to explain the code. We can put the markers in comments in our code so we can use the markers in existing code.