I18n Templates Plugin

  • Authors: Marcel Overdijk
5 votes
Dependency:
compile ":i18n-templates:1.1.0.1"

 Documentation

Summary

Description

I18n Templates Plugin

This simple plugin provides i18n aware scaffolding templates to generate fully internationalizable applications.

(!) This plugin is updated to Grails 1.1

Installation

To install the i18n templates plugin type this command from your project's root folder:

grails install-plugin i18n-templates

(-) Installing the plugin overwrites any previously installed scaffolding templates.

Activation in a webapp

By default the user locale is detected from the incoming Accept-Language header. However, you can provide users the capability to switch locales by simply passing a parameter called lang to Grails as a request parameter:

/book/list?lang=es

Grails will automatically switch the user's locale and store it in a cookie so subsequent requests will have the new header.

Usage

The i18n resource bundles contained by your application will be used to resolve page titles, header labels, property labels, button labels and feedback messages. These texts need to be added manually; however if you don't add any messages, the Grails application will render as if the default templates were used. This is done by using default texts which are indentical to the texts which are used by the default templates.

If you want to customize or translate the texts then add messages for the following keys to your i18n resource bundle(s) for each domain:

<domainClass>.create
<domainClass>.edit
<domainClass>.list
<domainClass>.new
<domainClass>.show
<domainClass>.created
<domainClass>.updated
<domainClass>.deleted
<domainClass>.not.found
<domainClass>.<property> <-- for each property
<domainClass>.<property>.<value> <-- for each value in a list constraint
<domainClass>.<property>.<constraint>.error <-- for each applied constraint

You can use the generate-i18n-messages script to ouput all needed messages for a domain class to your command line interface.
grails generate-i18n-messages <domainClass>

You can also use the script to output the messages for all domain classes.

grails generate-i18n-messages *

If you have a domain class in a package or are generating from a Hibernate mapped class remember to include the fully qualified package name.

grails generate-i18n-messages <fully qualified package name><domainClass>

Example

Imagine you application consists of these 2 domain classes:

class Author {

static hasMany = [books:Book] String name

static constraints = { name(blank: false, maxSize: 50) } }

class Book {

Author author String isbn String title String category Float price String description

static constraints = { author() isbn(blank: false, maxSize: 10) title(blank: false, maxSize: 50) category(inList: ["Fantasy", "Mystery", "Romance", "Thriller"]) price(min: 0F, max: 100F, nullable: true) description(blank: true, maxSize: 500) } }

Then these messages could be used to translate your application:

# Author messages
author.create=Create Author
author.edit=Edit Author
author.list=Author List
author.new=New Author
author.show=Show Author
author.created=Author {0} created
author.updated=Author {0} updated
author.deleted=Author {0} deleted
author.not.found=Author not found with ID {0}
author.id=ID
author.name=Name
author.name.blank.error=Property [name] of class [Author] cannot be blank
author.name.maxSize.error=Property [name] of class [Author] with value [{2}] exceeds the maximum size of [{3}]
author.books=Books

# Book messages book.create=Create Book book.edit=Edit Book book.list=Book List book.new=New Book book.show=Show Book book.created=Book {0} created book.updated=Book {0} updated book.deleted=Book {0} deleted book.not.found=Book not found with ID {0} book.id=ID book.author=Author book.isbn=ISBN book.isbn.blank.error=Property [isbn] of class [Book] cannot be blank book.isbn.maxSize.error=Property [isbn] of class [Book] with value [{2}] exceeds the maximum size of [{3}] book.title=Title book.title.blank.error=Property [title] of class [Book] cannot be blank book.title.maxSize.error=Property [title] of class [Book] with value [{2}] exceeds the maximum size of [{3}] book.category=Category book.category.Fantasy=Fantasy book.category.Mystery=Mystery book.category.Romance=Romance book.category.Thriller=Thriller book.category.inList.error=Property [category] of class [Book] with value [{2}] is not contained within the list [{3}] book.price=Price book.price.min.error=Property [price] of class [Book] with value [{2}] is less than minimum value [{3}] book.price.max.error=Property [price] of class [Book] with value [{2}] exceeds maximum value [{3}] book.description=Description book.description.maxSize.error=Property [description] of class [Book] with value [{2}] exceeds the maximum size of [{3}]

After translated, those messages could be appended to the "messages.properties" in your project's grails-appi18n folder.

General messages

The plugin already contains English and Dutch texts for general messages as displayed below:

home=Home
create=Create
edit=Edit
update=Update
delete=Delete
delete.confirm=Are you sure?

When installing the i18n templates plugin these texts are installed as 'i18n-templates.properties' resource bundles in your project's grails-appi18n folder. If you are creating a translation for a different language then you need to add a translation for above messages as well.

Plugin version history

1.1.0.1 (March 12, 2009)

  • Removed search action from Controller.groovy which was added by accident

1.1 (March 10, 2009)

  • Templates now use <g:textField />, <g:hiddenField /> etc. where possible instead of normal <input /> fields. This is a difference with the default Grails 1.1 templates
  • Added valueMessagePrefix when rendering inList constraint (GRAILSPLUGINS-473)
  • Synchronized scaffolding templates with latest templates from Grails 1.1

1.0.4 (November 14, 2008)

  • GenerateI18nMessages script can now also be called using "*" to generate i18n messages for all domain classes
  • GenerateI18nMessages script now also generated error messages for properties (GRAILSPLUGINS-530)
  • Synchronized scaffolding templates with latest templates from Grails 1.0.4 (GRAILS-3394)

1.0.3 (June 6, 2008)

  • Synchronized scaffolding templates with latest templates from Grails 1.0.3 (which included bug fixes for GRAILS-2444, GRAILS-2945 and GRAILS-3043)

1.0.1 (February 19, 2008)

  • Synchronized scaffolding templates with latest templates from Grails 1.0.1 (which included bug fix for GRAILS-2347)

1.0 (February 5, 2008)

  • Synchronized scaffolding templates with latest templates from Grails 1.0

1.0-RC1 (January 12, 2008)

  • Synchronized scaffolding templates with latest templates in Grails core svn
  • Fixed GRAILSPLUGINS-195 - I18n does not work with java domain classes