Get all available language resource files for language selection

February 15, 2013 – 8:49 am

Assume you have built a multi language XPage application and want to add a language switcher to the application. The content for the selector should be build automatically when you add a new language resource to the application.

I store my language resource files in the files section of Domino Designer. Each file has a file suffix of “.lang.properties”

langFiles

To get a list of all files with this specific suffix I put together some Java. The code builds a NoteCollection of MiscFormatElements and then does a serach within the collection to return only elements that match the search criteria.

package de.eknori;
 
import java.util.Vector;
 
import javax.faces.context.FacesContext;
 
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NoteCollection;
import lotus.domino.NotesException;
 
public class Design {
	private static final String SUFFIX_PROPERTY = ".lang.properties";
	private static final String EMPTY_STRING = "";
	private static final String FIELD_TITLE = "$TITLE";
 
	public static Vector getLangFiles() throws NotesException {
		Vector vec = new Vector();
		try {
			FacesContext fc = FacesContext.getCurrentInstance();
			Database db = (Database) fc.getApplication().getVariableResolver()
			.resolveVariable(fc, "database");
			NoteCollection nc = null;
			nc = db.createNoteCollection(false);
			nc.setSelectMiscFormatElements(true);
			nc.setSelectionFormula("@Contains(" + FIELD_TITLE + ";\""
					+ SUFFIX_PROPERTY + "\")");
			nc.buildCollection();
			String nid = EMPTY_STRING;
			nid = nc.getFirstNoteID();
			Document doc = null;
			while (!(EMPTY_STRING.equals(nid))) {
				doc = db.getDocumentByID(nid);
				vec.add(doc.getItemValueString(FIELD_TITLE).replace(
						SUFFIX_PROPERTY, EMPTY_STRING));
				nid = nc.getNextNoteID(nid);
				doc.recycle();
			}
			nc.recycle();
		} catch (Exception e) {
 
		}
		return vec;
	}
}

In your XPage, you can then simply make a call to the method from a combobox

< ?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
 
	<xp:label value="Select Language: " id="lblSelLanguage" for="cmbSelLanguage"></xp:label>
 
	<xp:combobox id="cmbSelLanguage">
		<xp:selectitems value="#{javascript:new de.eknori.Design.getLangFiles()}"></xp:selectitems>
	</xp:combobox>
 
</xp:view>

And here is the result

selLang

And with just a little tweak you can use the code to get a list of available themes and build a theme switcher …  Happy coding!

Sorry, comments for this entry are closed at this time.