elstar IT

Fullstack | Java | Tech Speaker | Tech Coach | Frank van der Linden

  • About me
  • Blog license
  • My Open source projects

XPages tip: add a new language to your app

08-03-2016 one response flinden68 development

import export

In a previous post I blogged about how to make a XPages application multi language.

The next step is to add 1 new language in total and no need to modify all the property documents of this new language one by one.

Export the property file

As I have the master property file, with all the labels and messages, in my application I can export this property file as a downloadable file.

I have trigger the code below from a XPage in the browser and will be produce txt file.

public void exportDefaultPropertyFile(){
List lines = new ArrayList();
XspHttpServletResponse response = null;
PrintWriter pw = null;
StringBuilder sb = new StringBuilder();
FacesContext facesContext = FacesContext.getCurrentInstance();
try{
ExternalContext extCon = facesContext.getExternalContext();
response = (XspHttpServletResponse) extCon.getResponse();
pw = response.getWriter();

//only HTTP POST is allowed
HttpServletRequest request = (HttpServletRequest) extCon.getRequest();
//set up output object
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition", "attachment; filename=default_properties.txt");
response.setDateHeader("Expires", -1);

int row = 0;
ResourceBundle bundle = JSFUtil.getXSPContext().bundle(DEFAULT_PROPERTIES_FILE);
if (bundle != null) {
Enumeration keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = bundle.getString(key);
lines.add(key+"="+value);
row += 1;
}
}

//sort
Collections.sort(lines, new Comparator() {
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});

//put together
for(String line : lines){
sb.append(line);
sb.append(System.getProperty("line.separator"));
}

pw.print(sb.toString());

response.commitResponse();
} catch (Exception e) {
XspOpenLogUtil.logError(e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

pw.print("Error:"+e.getMessage());

try {
response.commitResponse();
} catch (IOException e1) {
e1.printStackTrace();
}
} finally{
facesContext.responseComplete();
}

Translate and save new properties file

Now I can translate all the labels at once and save the property file. To make it easier for the importer it expects a special formatted name of the newly created property file.

countrycode_countryname.txt, e.g. nl_nederlands.txt

Import the new language file

The importer code is triggered by a button on XPage with a file upload and first save the document as temporary document. It will pass on to the actual importer code in my FileController bean.

The importer will first split the attachment name in the right variables.

Next these variables will passed to my LanguageHandler to check if the language is already present. If it is not present it will create a language document and add it to list, which will populate the language dropdown in the navigation top bar.

After this action it is time to import the lines from the imported properties file. There will be a check if there is already a property available with this key and country code.


public void importPropertyFile(DominoDocument dominodocument) {
InputStream is = null;
BufferedReader br = null;
try {
Document document = XSPUtil.wrap(dominodocument.getDocument());
if(document.hasEmbedded()){
String attachmentName = document.getItemValueString("importfile");
attachmentName = attachmentName.substring(3, attachmentName.length()).trim();

String[] attachmentNameSplit = attachmentName.split("_");
String code = attachmentNameSplit[0].toUpperCase();
String language = StringUtils.capitalize(attachmentNameSplit[1].substring(0,attachmentNameSplit[1].indexOf(".") ));

handleLanguage(language, code);
EmbeddedObject embObj = (EmbeddedObject) document.getAttachment(attachmentName);
if(embObj!=null){
is = embObj.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String line = null;
int row = 0;
while ((line = br.readLine()) != null) {
if(!"".equals(line)){
if(!line.startsWith("#")){
String[] lineSplit = line.split("=");
String key = lineSplit[0];
String value = lineSplit[1];
if(!PropertyHandler.get().hasProperty(key, code)){
//is not present yet, so create and add property
//this way we can run in it multiple time without running in trouble

Document docProperty = XSPUtil.getCurrentDatabase().createDocument();
docProperty.replaceItemValue("form", "property");
docProperty.replaceItemValue("code", code);
docProperty.replaceItemValue("language", language);
docProperty.replaceItemValue("key", key);
docProperty.replaceItemValue("value", value);

docProperty.save();

//add to Properties list
PropertyHandler.get().addProperty(key, value, code);

row += 1;
}
}
}

}
JSFUtil.addMessage(FacesMessage.SEVERITY_INFO, LanguageHandler.get().getLanguageString("message.import.properties.total") + (row));
}
}

//remove the Document
document.remove(true);
} catch (Exception e) {
XspOpenLogUtil.logError(e);
} finally{
if (br != null) {
try {
br.close();
} catch (IOException e) {
XspOpenLogUtil.logError(e);
}
}

}
}

Happy coding

Tags: development, java, xpages

One thought on “XPages tip: add a new language to your app”

  1. Pingback: XPages tip: Demo code for multi language support - elstar IT

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • « XPages tip: Multi language support for labels
  • XPages help: DateUtils class on XSnippets »

Contact me

My name is Frank van der Linden and I am an independent software developer based in the Netherlands. The last 2 years I was awarded as IBM Champion. Also I am on the board of OpenNTF. My specialisations are Java, Web development and Domino.


If you want to hire me, please fill in the Contact form


IBM Champion web badge
Apache Logo

All the code on this blog are under the Apache License 2.0. For more details, see Apache License 2.0

Most recent posts

  • Engage 2020: Hello are you listening, There is stream for everything
  • Spring Cloud Function on Azure run locally
  • Deploy Spring Cloud Function to IBM Cloud
  • Speaking (again) at Engage in a Zoo
  • Congratulations, you’re an IBM Champion (again)!

Latest reactions

  • Spring Cloud Function on Azure run locally - elstar IT on Deploy Spring Cloud Function to IBM Cloud
  • flinden68 on Quick XPages tip: add Fullcalendar plugin to your application
  • Rajesh samal on Quick tip: Swagger support for Spring Webflux
  • dsieyx on Quick XPages tip: add Fullcalendar plugin to your application
  • John on Named as IBM Champion 2019

Archive

  • March 2020
  • February 2020
  • January 2020
  • October 2019
  • September 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • December 2018
  • October 2018
  • September 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014

Category

  • bluemix
  • business
  • cloudant
  • community
  • development
  • hrassistant
  • openntf
  • running
  • salesforce
  • Springboot
  • Tesla
  • trailrunning
  • Uncategorized
  • watson
  • OpenNTF
  • Collaboration Today
  • XSnippets
  • Stackoverflow
  • IBM Collaboration Solutions
  • Social Business Toolkit
  • About me
  • Dutch curriculum vitae
  • English curriculum vitae
  • Google+
  • LinkedIn profile
  • Twitter
  • Slideshare
  • Blog license
  • My open source projects