Skip to content

Commit

Permalink
Merge pull request #1582 from AtlasOfLivingAustralia/feature/i18n
Browse files Browse the repository at this point in the history
added async internationalisation ko binding
  • Loading branch information
temi authored Apr 18, 2024
2 parents 196b4f4 + efd1723 commit b0bed74
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
biocollectVersion=6.7-SNAPSHOT
biocollectVersion=6.8-i18n-SNAPSHOT
grailsVersion=5.1.9
grailsGradlePluginVersion=5.1.5
assetPipelineVersion=3.3.4
Expand Down
49 changes: 49 additions & 0 deletions grails-app/assets/javascripts/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2019 Atlas of Living Australia
* All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Created by Temi on 15/11/19.
*/

(function() {
var messages = {},
deffer = $.Deferred();
$.get({
url: fcConfig.i18nURL,
cache: true
}).done(function (data) {
messages = data;
deffer.resolve();
}).fail(function () {
deffer.reject();
});

$i18n = function(key, defaultValue) {
if (messages[key] !== undefined) {
return messages[key];
} else {
return defaultValue || key;
}
};

$i18nAsync = function(key, defaultValue, callback) {
if (callback) {
deffer.done(function () {
callback($i18n(key, defaultValue));
}).fail(function () {
callback($i18n(key, defaultValue));
})
}
}

})();
47 changes: 46 additions & 1 deletion grails-app/assets/javascripts/knockout-custom-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,4 +1148,49 @@ ko.bindingHandlers.debug = {
console.log(element);
console.log(ko.toJS(valueAccessor()));
}
};
};


/**
* This binding requires i18n.js to be loaded. It also requires fcConfig.i18nURL to be set.
* Params can be a string or an object. If string, it is treated as key and translated to text. Object parameter has the
* following properties:
* @contentType can be 'text' or 'html' (default is 'text')
* @key is the key to be translated
* @defaultValue is the default value to be used if the key is not found
*
* Usage examples:
* <div data-bind="i18n: 'g.cancel'"></div>
* <div data-bind="i18n: {key: 'record.edit.verificationStatusTypes.help', contentType: 'html', defaultValue: '<b>simple help</b>'}"></div>
*
*/
ko.bindingHandlers.i18n = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var value = valueAccessor();
value = ko.unwrap(value);
var contentType = value && value.contentType || 'text'

// $i18nAsync is required to be defined
if(typeof $i18nAsync === 'undefined')
return

if( typeof value === 'string') {
$i18nAsync(value, '',function(text) {
$(element).text(text);
});
}
else if (typeof value === 'object') {
$i18nAsync(value.key, value.defaultValue,function(text) {
switch (contentType) {
default:
case 'text':
$(element).text(text);
break;
case 'html':
$(element).html(text);
break;
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HomeController {
def settingService
def metadataService
def userService
CommonService commonService

@PreAuthorise(accessLevel = 'alaAdmin', redirectController = "admin")
@SSO
Expand Down Expand Up @@ -61,6 +62,13 @@ class HomeController {
def works() {
}

def i18n() {
if (request.isGet()) {
Map props = commonService.i18n(request.locale)
render props as JSON
}
}

/**
* The purpose of this method is to enable the display of the spatial object corresponding to a selected
* value from a geographic facet (e.g. to display the polygon representing NSW on the map if the user has
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package au.org.ala.biocollect.merit
import grails.converters.JSON
import grails.web.mapping.LinkGenerator
import grails.web.servlet.mvc.GrailsParameterMap
import org.springframework.context.MessageSource

import javax.servlet.http.HttpServletRequest
import javax.xml.bind.DatatypeConverter
Expand All @@ -13,6 +14,7 @@ class CommonService {
UserService userService

LinkGenerator grailsLinkGenerator
MessageSource messageSource

List ignores = ["action","controller"]

Expand Down Expand Up @@ -92,4 +94,7 @@ class CommonService {
queryParams
}

def i18n(Locale locale) {
messageSource.getMergedProperties(locale)?.properties
}
}

0 comments on commit b0bed74

Please sign in to comment.