Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added features / redesign plugin #16

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
/build
/plugin.xml
/web-app/WEB-INF
.idea
*.iml
105 changes: 51 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@ i18n-asset-pipeline version | required for
1.x | `asset-pipeline` version 2.0.0 or higher
2.x | Grails 3.x


## Breaking update information 2.1.0
* Removed @import option, replaced with default i18n messages inheritance
(i.e. all keys from messages.i18n are merged with the localized version messages_nl.i18n)
* Added support for loading messages.properties from plugins,
*Note:* Changes to the messages properties files are not hot reloaded. You need
to update the i18n file for this to happen.
* Added support for custom named i18n properties files (i.e. <asset:i18n name='shared'/>)
will actually search for a messages bundle shared.properties
* Added support for regular expressions for selecting key values from the message bundle.
simply add `regexp: <regular expression>` in the .i18n file.
For example: `regexp: portal\..*` adds all messages starting with `portal.`.
* Added support for correctly loading the encoding of the messages bundle.

## Installation

To use this plugin you have to add the following code to your `build.gradle`:

```groovy
buildscript {
dependencies {
classpath 'org.amcworld.plugins:i18n-asset-pipeline:2.0.0'
classpath 'org.amcworld.plugins:i18n-asset-pipeline:2.1.0-SNAPSHOT'
}
}

dependencies {
runtime 'org.grails.plugins:i18n-asset-pipeline:2.0.0'
runtime 'org.grails.plugins:i18n-asset-pipeline:2.1.0-SNAPSHOT'
}
```

Expand Down Expand Up @@ -62,19 +76,14 @@ Each i18n file must be defined according to the following rules:
* Files are line based.
* Lines are trimmed (i. e. leading and terminating whitespaces are removed).
* Empty lines and lines starting with a hash `#` (comment lines) are ignored.
* Lines starting with `@import` *`url`* are resolved by importing file
*`url`*, processing it according to these rules, and replacing the
`@import` statement by its content. The import file may contain further
import statements, even circular ones. You may omit file extension `.i18n`
in *`url`*.
* Lines starting with `regexp:` are matched to all keys from the message file.
For example: `regexp:.*` adds all keys to the message file.
* All other lines are treated as messsage codes which are translated to the
required language.
* Comments after import statements and message codes are not allowed.

Each i18n file may contain asset-pipeline `require` statements to load other
assets such as JavaScript files. **ATTENTION!** Don't use `require` to load
other i18n files because they will not be processed correctly. Use the
`@import` declaration instead.
assets such as JavaScript files.

## Typical file structure

Expand All @@ -95,54 +104,27 @@ Then, you should have the same set of files in e. g. `grails-app/assets/i18n`:
* `messages_es.i18n`
* `messages_fr.i18n`

Normally, you would have to declare the same set of message codes in each file.
To DRY, add a file `_messages.i18n` to `grails-app/assets/i18n` (the
leading underscore prevents the i18n file to be compiled itself):

```
#
# _messages.i18n
# List of message codes that should be available on client-side.
#

# Add your messages codes here:
default.btn.cancel
default.btn.ok
contact.foo.bar

```

Then, you can import this file in all other files, e. g.:

```
#
# messages.i18n
# Client-side i18n, English messages.
#

@import _messages

```

```
#
# messages_de.i18n
# Client-side i18n, German messages.
#
The codes are can be added manually to each file, but the default inheritance of i18n properties
files are enforced. So the entries of `messages_en_UK.i18n` contains all entries from:

@import _messages
* `messages.i18n`
* `messages_en.i18n`
* `messages_en_UK.i18n`

```
To add all properties defined in the `messages.properties` bundle simply add `regexp:.*`
to `messages.i18n` and add empty files for each supported client side locale.

* `messages.i18n` (contains `regexp:.*`)
* `messages_en.i18n` (empty)
* `messages_en_UK.i18n` (empty)
* `messages_nl.i18n` (empty)

```
#
# messages_es.i18n
# Client-side i18n, Spanish messages.
#
This results in:

@import _messages

```
* `messages.js` (all entries from messages.properties)
* `messages_en.js` (all entries from messages.properties merged with messages_en.properties)
* `messages_en_UK.js` (all entries from messages.properties merged with messages_en.properties merged with messages_en_UK.properties)
* `messages_nl.js` (all entries from messages.properties merged with messages_nl.properties)

## Including localized assets

Expand All @@ -165,11 +147,26 @@ Examples:
<asset:i18n name="texts" locale="${locale}" />
```

## i18n.js

A more advanced i18n.js is included to add more advanced formatting on the client.
It can be included with `<asset:javascript src="i18n-asset/i18n.js/>`.
You can now use `$i18n.m(<code>,<args>,<defaultMessage>)` to retrieve the messages.
Internally $i18n uses $L to get the message.

* code: the code of the message in the messages resource bundle, required
* args: an array of values to format the message with, optional
* defaultMessage: a defaultMessage to display if code is not found, defaults to `[<code>]`

For convencience `$i18n.md(<code>,<defaultMessage>)` is added as well.

## Author

This plugin was written by [Daniel Ellermann](mailto:[email protected])
([AMC World Technologies GmbH][amc-world]).

Updated by [Dennie de Lange](mailto:[email protected]).

## License

This plugin was published under the
Expand Down
15 changes: 12 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ buildscript {
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.13.2"
}
}

version '2.0.0'
version '2.1.0-SNAPSHOT'
group 'org.grails.plugins'

apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'asset-pipeline'
apply plugin: 'org.grails.grails-plugin'
apply plugin: 'org.grails.grails-plugin-publish'
apply plugin: 'org.grails.grails-gsp'
Expand All @@ -45,6 +47,8 @@ ext {
gradleWrapperVersion = project.gradleWrapperVersion
}



repositories {
mavenLocal()
maven { url 'https://repo.grails.org/grails/core' }
Expand All @@ -65,15 +69,16 @@ dependencies {
compile 'org.grails:grails-core'
compile 'org.grails:grails-dependencies'
compile 'org.grails:grails-web-boot'
compile 'org.grails.plugins:asset-pipeline:3.1.0'
compile "com.bertramlabs.plugins:asset-pipeline-gradle:2.13.2"
compile 'com.bertramlabs.plugins:asset-pipeline-grails:2.13.2'
compile 'com.bertramlabs.plugins:asset-pipeline-core:2.13.2'

console 'org.grails:grails-console'

profile 'org.grails.profiles:web-plugin:3.1.3'

testCompile 'org.grails:grails-plugin-testing'
}

task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
Expand All @@ -99,3 +104,7 @@ grailsPublish {
desc = 'asset-pipeline plugin to use localized messages in JavaScript.'
developers = [dellermann: 'Daniel Ellermann']
}

assets {
packagePlugin = true
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
grailsVersion=3.1.3
gradleWrapperVersion=2.9
grailsVersion=3.2.9
gradleWrapperVersion=3.3
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Nov 27 23:09:32 CET 2015
#Tue May 23 12:01:00 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip
41 changes: 41 additions & 0 deletions grails-app/assets/javascripts/i18n-asset/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
;(function(factory){
factory(window.$L);
}(function(messages){
if(!messages){
console.log('No messagebundle loaded, always returning message code as fallback.');
}

var emptyArray = [];
var i18n = function(){}
i18n.prototype.md = function(code,defaultMessage){
return this.m(code,emptyArray,defaultMessage);
},
i18n.prototype.m = function(code,args,defaultMessage){
var message = this.getMessage(code);
if(!message){
message = defaultMessage || this.getDefaultMessage(code);
}
if(args && args.length>0){
message = this.format(message,args);
}
return message;
},
i18n.prototype.format = function(source,params){
if ( params === undefined ) {
return source;
}
for(var i=0;i<params.length;i++){
source = source.replace( new RegExp( "\\{" + i + "\\}", "g" ), function() {
return params[i];
});
}
return source;
},
i18n.prototype.getDefaultMessage = function(code){
return '[' + code + ']';
},
i18n.prototype.getMessage = function(code){
return messages? messages(code): null;
}
window.$i18n = new i18n();
}));
12 changes: 6 additions & 6 deletions grails-app/taglib/asset/pipeline/i18n/I18nTagLib.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class I18nTagLib implements TagLibrary {
def i18n = { attrs ->
Properties manifest = AssetPipelineConfigHolder.manifest
ApplicationContext ctx = grailsApplication.mainContext
String mapping = assetProcessorService.assetMapping


def l = attrs.remove('locale') ?: ''
String locale = ''
if (l instanceof Locale || l instanceof CharSequence) {
Expand All @@ -79,9 +78,9 @@ class I18nTagLib implements TagLibrary {
String [] parts = locale.split('_')

String src = null
for (int i = parts.length - 1; i >= 0 && !src; --i) {
for (int i = parts.length; i >= 0 && !src; --i) {
StringBuilder buf = new StringBuilder(name)
for (int j = 0; j <= i; j++) {
for (int j = 0; j < i; j++) {
buf << '_' << parts[j]
}
buf << '.js'
Expand Down Expand Up @@ -116,7 +115,8 @@ class I18nTagLib implements TagLibrary {
log.debug "Localized asset not found - using default asset '${name}.js'"
}
}

out << asset.javascript(src: src ?: (name + '.js'))
if(src){
out << asset.javascript(src: src ?: (name + '.js'))
}
}
}
11 changes: 1 addition & 10 deletions src/main/groovy/asset/pipeline/i18n/I18nAssetFile.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,7 @@ class I18nAssetFile extends AbstractAssetFile {
String processedStream(AssetCompiler precompiler) {
def skipCache = precompiler ?: (!processors || processors.size() == 0)

String fileText
if(baseFile?.encoding || encoding) {
fileText = inputStream?.getText(
baseFile?.encoding ? baseFile.encoding : encoding
)
} else {
fileText = inputStream?.text
}

fileText = I18nPreprocessor.instance.preprocess(this, fileText)
String fileText = I18nPreprocessor.instance.preprocess(this)

def md5 = AssetHelper.getByteDigest(fileText.bytes)
if (!skipCache) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

package asset.pipeline.i18n

import grails.plugins.*

import grails.plugins.Plugin

class I18nAssetPipelineGrailsPlugin extends Plugin {

Expand All @@ -44,28 +43,24 @@ class I18nAssetPipelineGrailsPlugin extends Plugin {
url: 'https://github.com/dellermann/i18n-asset-pipeline/issues'
]
def scm = [url: 'https://github.com/dellermann/i18n-asset-pipeline']

def watchedResources = ['file:./grails-app/i18n/*.properties']

//-- Public methods -------------------------

Closure doWithSpring() {
{ ->
// TODO Implement runtime spring config (optional)
}
}
Closure doWithSpring() {{ ->

}}

void doWithDynamicMethods() {
// TODO Implement registering dynamic methods to classes (optional)
}

void doWithApplicationContext() {
// TODO Implement post initialization spring config (optional)

}

void onChange(Map<String, Object> event) {
// TODO Implement code that is executed when any artefact that this plugin is
// watching is modified and reloaded. The event contains: event.source,
// event.application, event.manager, event.ctx, and event.plugin.
}

void onConfigChange(Map<String, Object> event) {
Expand Down
Loading