Business Console 10.11 | webMethods Business Console Documentation | Developing Gadgets for Business Console | Getting Started | Localizing a Gadget
 
Localizing a Gadget
1. Create a Gadget. For more information, see Creating HelloWorld Gadget .
2. Open the gadgetDefinition.xml file located at /WEB-INF/gadgets/<Gadget_ID>.
3. Select the Gadget Defintion Editor tab.
4. Add a new parameter locale in the Gadget Definition Editor and assign it the default value en.
5. Save the gadgetDefinition.xml file.
6. Open the controller.js file located at /WebContent/<Gadget_name>/scripts.
7. Define one of the following functions inside the defineScope code block as follows:
*restInvocation

this.$scope.restInvocation = function(locale) {
var $scope = this;
var REST_URL = {url:
'/wm_bc_gadgets_samples/samplerest/localize/'+locale,method:'GET', isArray:true}
$scope.restClient.invoke(
REST_URL,
function(response, status, headers, config, $scope) {
var data = response;

$scope.dictionary = new Object();

for(var key in data){
if(data.hasOwnProperty(key)){
$scope.dictionary[data[key].key]=data[key].value;
}
}

},
function(response, status, headers, config, $scope) {
console.log("Error calling the REST url");
},
null, null, $scope, null);
},
Update the REST_URL in the restInvocation function based on the RESTful APIs that you have defined.
* successCallback
this.$scope.successCallback=(function (data) {
// store the returned array in the dictionary
var $scope = this;

for(var key in data){
if(data.hasOwnProperty(key)){
$scope.dictionary[data[key].key]=data[key].value;
}
}

})
8. Inside the init code block of the controller.js file, do one of the following:
*If you defined the restInvocation function, add:

this.$scope.locale = config.params.locale;
this.$scope.restInvocation(config.params.locale);
*If you defined the successCallback function, add:
var url = '<url to the JavaScript file with Localized String constants>;
return.$http({ method:"GET", url:url,
cache:false }).success(_this.successCallback);
9. (Optional) You can create the localize RESTful service as shown in the following sample LocalizationResource.java code.

@Path("localize")
public class LocalizationResource{

public static final String RESOURCE_BUNDLE_NAME = "java.util.ResourceBundle";
public static final String RESOURCE_TYPE_NAME =
"caf.bc.gadget.samples.resources";

@GET
@Produces(MediaType.APPLICATION_JSON)
public String getLocalizedData(@Context HttpServletRequest request) {
ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_TYPE_NAME +
".messages", Locale.ENGLISH);
return buildJson(bundle);
}

@GET
@Path("{locale}")
@Produces(MediaType.APPLICATION_JSON)
public String getLocalizedData(@Context HttpServletRequest request,
@PathParam("locale") String locale) {
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle(RESOURCE_TYPE_NAME +
".messages_" + locale, new Locale(locale));
} catch (MissingResourceException mre) {
bundle = ResourceBundle.getBundle(RESOURCE_TYPE_NAME +
".messages", Locale.ENGLISH);
}
return buildJson(bundle);
}

private String buildJson(ResourceBundle bundle) {
Enumeration<String> keys = bundle.getKeys();
StringBuilder builder = new StringBuilder("[\n");
int c = 0;
while (keys.hasMoreElements()) {
String key = keys.nextElement();

if (c == 0) {
c = 1;
builder.append("\t\t{\"key\":\"" + key +
"\",\"value\":\"" + bundle.getString(key) + "\"}");
} else {
builder.append(",\n\t\t{\"key\":\"" + key +
"\",\"value\":\"" + bundle.getString(key) + "\"}");
}
}
builder.append("\n]");
return builder.toString();
}

}

Sample content in the expected .js file when using the successCallback function is as follows:

“[
{"key":"greetings","value":"Hello"},
{"key":"inquiry","value":"How are you?"},
{"key":"farewell","value":"Goodbye"}
]”