I18nEnums Grails Plugin

2 votes
Dependency :
compile ":i18n-enums:1.0.1"

Documentation Source Issues

Summary

Adds an enumeration usable on Enums to easy add and implement the MessageSourceResolvable interface

Installation

Prefered way

Add a dependency to BuildConfig.groovy:
compile ':i18n-enums:1.0.1'

Deprecate way

Read: do not use
grails install-plugin i18n-enums

Description

This plugin adds an annotation on Enums to easy add and implement the MessageSourceResolvable interface. It was heavily inspired by this blog-entry by Rob Fletcher: ]http://adhockery.blogspot.dk/2009/03/internationalizing-domain-classes-and.html

If you use it, let me know

If you use this plugin in your application, let me know - it's always nice to know, when ones work is used in the wild. Send me a tweet to @sbglasius or send me an email to soeren@glasius.dk - thank you!

Installation

In your BuildConfig.groovy insert:

compile ':i18n-enums:1.0.1'

Basic usage

You just annotate your ennumeration with @I18nEnum

package test

@I18nEnum enum MyEnum { ONE, Two, three }

will generate the following codes for each enum:

test.MyEnum.ONE.codes   == ['test.MyEnum.ONE',   'test.MyEnum.ONE',   'test.MyEnum.one']
test.MyEnum.Two.codes   == ['test.MyEnum.TWO',   'test.MyEnum.Two',   'test.MyEnum.two']
test.MyEnum.three.codes == ['test.MyEnum.THREE', 'test.MyEnum.three', 'test.MyEnum.three']

And the following default values:

test.MyEnum.ONE.defaultMessage == 'ONE'
test.MyEnum.Two.defaultMessage == 'Two'
test.MyEnum.three.defaultMessage == 'three'

as it can be seen, the codes comes in three variations: UPPER_CASE, UNCHANGED and LOWER_CASE, and the default message is simply the Enum value

Advanced usage

To get even more control over, how the codes and the default value are generated, you can add parameters to the annotation:

@I18nEnum(prefix = '', postfix = '', shortName = false, defaultNameCase = DefaultNameCase.UNCHANGED)

Prefix and Postfix

Prefix controls what is added before the package name. If you do not add a dot after the prefix, one will be added for you.

Postfix controls what is added after the ENUM_VALUE. If you do not add a dot before the postfix, one will be added for you.

Example:

package test

@I18nEnum(prefix = 'enum', postfix = 'label') enum MyEnum { ONE, Two, three }

will generate the following codes for each enum:

test.MyEnum.ONE.codes ==   ['enum.test.MyEnum.ONE.label',   'enum.test.MyEnum.ONE.label',   'enum.test.MyEnum.one.label']
test.MyEnum.Two.codes ==   ['enum.test.MyEnum.TWO.label',   'enum.test.MyEnum.Two.label',   'enum.test.MyEnum.two.label']
test.MyEnum.three.codes == ['enum.test.MyEnum.THREE.label', 'enum.test.MyEnum.three.label', 'enum.test.MyEnum.three.label']

shortName

If you do not want the package name in the generated codes, just add shortName = true to your annotation parameters

Example:

package test

@I18nEnum(shortName = true) enum MyEnum { ONE, Two, three }

will generate the following codes for each enum:

test.MyEnum.ONE.codes ==   ['MyEnum.ONE',   'MyEnum.ONE',   'MyEnum.one']
test.MyEnum.Two.codes ==   ['MyEnum.TWO',   'MyEnum.Two',   'MyEnum.two']
test.MyEnum.three.codes == ['MyEnum.THREE', 'MyEnum.three', 'MyEnum.three']

defaultNameCase

To control the defaultMessage you can use defaultNameCase. It can be set to one of the following values:

UPPER_CASE, LOWER_CASE, CAPITALIZE, ALL_CAPS, UNCHANGED

where UNCHANGED is the default.

Example:

DefaultNameCase.CAPITALIZE
package test

@I18nEnum(defaultNameCase = DefaultNameCase.CAPITALIZE) enum MyEnum { ONE, Two, three }

And the following default values:

test.MyEnum.ONE.defaultMessage == 'One'
test.MyEnum.Two.defaultMessage == 'Two'
test.MyEnum.three.defaultMessage == 'Three'

DefaultNameCase.ALL_CAPS
@I18nEnum(defaultNameCase = DefaultNameCase.ALL_CAPS)
enum MyEnum {
    ONE, Two, three, Four_five
}

And the following default values:

test.MyEnum.ONE.defaultMessage == 'One'
test.MyEnum.Two.defaultMessage == 'Two'
test.MyEnum.three.defaultMessage == 'Three'
test.MyEnum.Four_five.defaultMessage == 'Four Five'

DefaultNameCase.LOWER_CASE
package test

@I18nEnum(defaultNameCase = DefaultNameCase.LOWER_CASE) enum MyEnum { ONE, Two, three }

And the following default values:

test.MyEnum.ONE.defaultMessage == 'one'
test.MyEnum.Two.defaultMessage == 'two'
test.MyEnum.three.defaultMessage == 'three'

Configuration

It is possible to configure default settings from configuration.

In Configuration.groovy create a configuration tree like this:

import grails.plugin.i18nEnum.transformation.DefaultNameCase

// Your other configuration

grails { plugin { i18nEnum { postfix = "label" shortName = true defaultNameCase = DefaultNameCase.ALL_CAPS } } }

The settings values are the same as the configuration directives for the annotation.

Changes

v1.0.1

Grails event hook to precompile AST transformations before the rest of the Grails compile phase. This is needed so that enums annotated will be compiled correctly.

v1.0.0

The plugin was formerly known as EnumMessageSourceResolvable, but was renamed to I18nEnums for easier reading and writing. Functionality wise it has not changed from v1.0.0 of EnumMessageSourceResolvable.

Now support configuration in Config.groovy - see documentation below

Previous versions

For previous versions see EnumMessageSourceResolvable

If you use it, let me know

If you use this plugin in your application, let me know - it's always nice to know, when ones work is used in the wild. Send me a tweet to @sbglasius or send me an email to soeren@glasius.dk - thank you!

License

Licensed under the Apache2.0 license. See http://www.apache.org/licenses/LICENSE-2.0 for more information.