(Quick Reference)

3 Usage - Reference Documentation

Authors: Søren Berg Glasius

Version: 1.0.8

3 Usage

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.