Difference between revisions of "Internationalization"

From OpenKM Documentation
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
It's good idea centralizing Internationalization in a single class package for all OpenKM extensions.
+
The idea is add in database some records like
 +
 
 +
<source lang="sql">
 +
INSERT INTO OKM_TRANSLATION (TR_MODULE, TR_KEY, TR_TEXT, TR_LANGUAGE) VALUES ('extension', 'new_extension.button.add', 'Add', 'en-GB');
 +
INSERT INTO OKM_TRANSLATION (TR_MODULE, TR_KEY, TR_TEXT, TR_LANGUAGE) VALUES ('extension', 'new_extension.button.add', 'Añadir', 'es-ES');
 +
</source>
 +
 
 +
And then call in source code with
 +
 
 +
<source lang="java">
 +
GeneralComunicator.i18nExtension("new_extension.button.add");
 +
</source>
 +
 
 +
or you can use some of OpenKM frontend translations with
 +
 
 +
<source lang="java">
 +
GeneralComunicator.i18n("button.close");
 +
</source>
 +
 
  
  
For example if you want to '''internationalization to english and spanish''' you migh create two classes '''Lang_en_GB.java''' and '''Lang_es_ES.java'''
+
== OpenKM 4.x and older ==
 +
It's good idea centralizing Internationalization in a single class package for all OpenKM extensions.
  
 +
For example if you want to make '''internationalization for english and spanish''' you migh create two classes '''Lang_en_GB.java''' and '''Lang_es_ES.java'''
  
 
<source lang="java">
 
<source lang="java">
Line 11: Line 31:
  
 
public class Lang_en_GB {
 
public class Lang_en_GB {
 
 
public final static HashMap<String, String> lang;
 
public final static HashMap<String, String> lang;
  static {
+
static {
    lang = new HashMap<String, String>();
+
lang = new HashMap<String, String>();
   
+
 
    // Download button
+
// Download button
    lang.put("download.button.tittle", "Download document");
+
lang.put("download.button.tittle", "Download document");    
   
+
}
  }
 
 
}
 
}
 
</source>
 
</source>
 
 
  
 
<source lang="java">
 
<source lang="java">
Line 31: Line 47:
  
 
public class Lang_es_ES {
 
public class Lang_es_ES {
 
 
public final static HashMap<String, String> lang;
 
public final static HashMap<String, String> lang;
  static {
+
static {
    lang = new HashMap<String, String>();
+
lang = new HashMap<String, String>();
   
+
 
    // Download button
+
// Download button
    lang.put("download.button.tittle", "Descargar documento");
+
lang.put("download.button.tittle", "Descargar documento");
   
+
}
  }
 
 
}
 
}
 
</source>
 
</source>
 
  
 
And has a main '''Lang.java''' class to get '''language mapping translations'''
 
And has a main '''Lang.java''' class to get '''language mapping translations'''
Line 55: Line 68:
 
public static final String LANG_es_ES = "es-ES";  
 
public static final String LANG_es_ES = "es-ES";  
 
public static final String LANG_en_GB = "en-GB";
 
public static final String LANG_en_GB = "en-GB";
+
 
 
public static HashMap<String, String> getLang(String lang) {
 
public static HashMap<String, String> getLang(String lang) {
 
HashMap<String, String> hLang = new HashMap<String, String>();
 
HashMap<String, String> hLang = new HashMap<String, String>();
Line 71: Line 84:
 
}
 
}
 
</source>
 
</source>
 
  
 
As you can see in '''Lang.java''' the variables '''LANG_es_ES''' and '''LANG_en_GB''' refers to '''"es-ES"''' and '''"en-GB"'''. When in your extensions makes a call to '''GeneralComunicator.getLang()''' to get language it returns a String with that format '''"language-Country"'''.
 
As you can see in '''Lang.java''' the variables '''LANG_es_ES''' and '''LANG_en_GB''' refers to '''"es-ES"''' and '''"en-GB"'''. When in your extensions makes a call to '''GeneralComunicator.getLang()''' to get language it returns a String with that format '''"language-Country"'''.
 
  
 
Here there's some example how you could use it in your extensions:
 
Here there's some example how you could use it in your extensions:
  
 
<source lang="java">
 
<source lang="java">
    String lang = GeneralComunicator.getLang();
+
String lang = GeneralComunicator.getLang();
    String translation = Lang.getLang(lang).get("download.button.tittle")
+
String translation = Lang.getLang(lang).get("download.button.tittle")
 
</source>
 
</source>
 
  
 
Remember any extension internationalized must implements LanguageHandlerExtension, for example:
 
Remember any extension internationalized must implements LanguageHandlerExtension, for example:
Line 88: Line 98:
 
<source lang="java">
 
<source lang="java">
 
private class ToolBarButton extends ToolBarButtonExtension implements LanguageHandlerExtension {
 
private class ToolBarButton extends ToolBarButtonExtension implements LanguageHandlerExtension {
+
public ToolBarButton(Image image, String title, ClickHandler handler) {
public ToolBarButton(Image image, String title, ClickHandler handler) {
+
super(image, title, handler);
super(image, title, handler);
+
}
}
+
 
 +
@Override
 +
public void checkPermissions(GWTFolder folder, GWTFolder folderParent, int originPanel) {
 +
// TODO
 +
}
  
@Override
+
@Override
public void checkPermissions(GWTFolder folder, GWTFolder folderParent, int originPanel) {
+
public void checkPermissions(GWTDocument doc, GWTFolder folder) {
// todo
+
// TODO
}
+
}
  
@Override
+
@Override
public void checkPermissions(GWTDocument doc, GWTFolder folder) {
+
public void checkPermissions(GWTMail mail, GWTFolder folder) {
// todo
+
// TODO
}
+
}
  
@Override
+
@Override
public void checkPermissions(GWTMail mail, GWTFolder folder) {
+
public void enable(boolean enable) {
// todo
+
// TODO
}
+
}
  
@Override
+
@Override
public void enable(boolean enable) {
+
public boolean isEnabled() {
// todo
+
return true;
}
+
}
  
@Override
+
@Override
public boolean isEnabled() {
+
public void onChange(LanguageEventConstant event) {
return true;
+
if (event.equals(HasLanguageEvent.LANGUAGE_CHANGED)) {
}
+
String lang = GeneralComunicator.getLang();
+
setTitle(Lang.getLang(lang).get("download.button.tittle"));
@Override
 
public void onChange(LanguageEventConstant event) {
 
if (event.equals(HasLanguageEvent.LANGUAGE_CHANGED)) {
 
String lang = GeneralComunicator.getLang();
 
setTitle(Lang.getLang(lang).get("download.button.tittle"));
 
}
 
 
}
 
}
 +
}
 
}
 
}
 
</source>
 
</source>
  
 
+
[[Category: Extension Guide]]
[[Category: OpenKM plugin extensions]]
 
[[Category:OKM Network]]
 

Latest revision as of 23:26, 7 July 2012

The idea is add in database some records like

INSERT INTO OKM_TRANSLATION (TR_MODULE, TR_KEY, TR_TEXT, TR_LANGUAGE) VALUES ('extension', 'new_extension.button.add', 'Add', 'en-GB');
INSERT INTO OKM_TRANSLATION (TR_MODULE, TR_KEY, TR_TEXT, TR_LANGUAGE) VALUES ('extension', 'new_extension.button.add', 'Añadir', 'es-ES');

And then call in source code with

GeneralComunicator.i18nExtension("new_extension.button.add");

or you can use some of OpenKM frontend translations with

GeneralComunicator.i18n("button.close");


OpenKM 4.x and older

It's good idea centralizing Internationalization in a single class package for all OpenKM extensions.

For example if you want to make internationalization for english and spanish you migh create two classes Lang_en_GB.java and Lang_es_ES.java

package com.openkm.extension.frontend.client.lang;

import java.util.HashMap;

public class Lang_en_GB {
	public final static HashMap<String, String> lang;
	static {
		lang = new HashMap<String, String>();

		// Download button
		lang.put("download.button.tittle", "Download document");	    
	}
}
package com.openkm.extension.frontend.client.lang;

import java.util.HashMap;

public class Lang_es_ES {
	public final static HashMap<String, String> lang;
	static {
		lang = new HashMap<String, String>();

		// Download button
		lang.put("download.button.tittle", "Descargar documento");
	}
}

And has a main Lang.java class to get language mapping translations

package com.openkm.extension.frontend.client.lang;

import java.util.HashMap;

public class Lang {
	// Languages
	public static final String LANG_es_ES = "es-ES"; 
	public static final String LANG_en_GB = "en-GB";

	public static HashMap<String, String> getLang(String lang) {
		HashMap<String, String> hLang = new HashMap<String, String>();
		
		if (LANG_es_ES.equalsIgnoreCase(lang) || LANG_es_ES.substring(0, 2).equalsIgnoreCase(lang.substring(0, 2)))  {
			hLang = Lang_es_ES.lang;
		} else if (LANG_en_GB.equalsIgnoreCase(lang) || LANG_en_GB.substring(0, 2).equalsIgnoreCase(lang.substring(0, 2)))  {
			hLang = Lang_en_GB.lang;
		} else {
			hLang = Lang_en_GB.lang;
		}
		
		return hLang;
	}
}

As you can see in Lang.java the variables LANG_es_ES and LANG_en_GB refers to "es-ES" and "en-GB". When in your extensions makes a call to GeneralComunicator.getLang() to get language it returns a String with that format "language-Country".

Here there's some example how you could use it in your extensions:

	String lang = GeneralComunicator.getLang();
	String translation = Lang.getLang(lang).get("download.button.tittle")

Remember any extension internationalized must implements LanguageHandlerExtension, for example:

private class ToolBarButton extends ToolBarButtonExtension implements LanguageHandlerExtension {
	public ToolBarButton(Image image, String title, ClickHandler handler) {
		super(image, title, handler);
	}

	@Override
	public void checkPermissions(GWTFolder folder, GWTFolder folderParent, int originPanel) {
		// TODO
	}

	@Override
	public void checkPermissions(GWTDocument doc, GWTFolder folder) {
		// TODO
	}

	@Override
	public void checkPermissions(GWTMail mail, GWTFolder folder) {
		// TODO
	}

	@Override
	public void enable(boolean enable) {
		// TODO
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public void onChange(LanguageEventConstant event) {
		if (event.equals(HasLanguageEvent.LANGUAGE_CHANGED)) {
			String lang = GeneralComunicator.getLang();
			setTitle(Lang.getLang(lang).get("download.button.tittle"));
		}
	}
}