Archive for the 'Programming' Category
Java Internationalization Reference
ResourceBundle and MessageFormat
try {
Locale locale = getLocale();
String rbName = getBundleName();
ResourceBundle bundle = ResourceBundle.getBundle(rbName, locale);
String retMessage = bundle.getString( key );
if ( varargs.length > 0 )
{
MessageFormat mf = new MessageFormat( retMessage, locale );
retMessage = mf.format( varargs );
//TESTING: Writing Java Unicode to UTF-8
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter (new FileOutputStream(filename),"UTF-8"));
bw.write(retMessage);
bw.close();
}
} catch ( Exception e ) {
e.printStackTrace();
}
References:
I18N Messages and Logging
Provides an API that allows your Java applications to generate and log internationalized messages along with creating localized exceptions. Also provides Java annotations and an ANT task to help automatically generate language resource bundles.
http://sourceforge.net/project/showfiles.php?group_id=169460
Java internationalization with the Properties Pre-Processor
An XML-based utility for handling localized strings
http://www.javaworld.com/javaworld/jw-08-2007/jw-08-i18nproperties.html?page=4#resources
Sun Java Tutorials:
http://java.sun.com/docs/books/tutorial/information/download.html
DateFormat:
import java.util.*;
import java.text.*;
static public void showBothStyles(Locale currentLocale) {
Date today;
String result;
DateFormat formatter;
int[] styles = {
DateFormat.DEFAULT,
DateFormat.SHORT,
DateFormat.MEDIUM,
DateFormat.LONG,
DateFormat.FULL
};
System.out.println();
System.out.println("Locale: " + currentLocale.toString());
System.out.println();
today = new Date();
for (int k = 0; k < styles.length; k++) {
formatter = DateFormat.getDateTimeInstance(
styles[k], styles[k], currentLocale);
result = formatter.format(today);
System.out.println(result);
}
}
DecimalFormat:
static public void localizedFormat(String pattern, double value,
Locale loc ) {
NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
System.out.println(pattern + " " + output + " " + loc.toString());
}
NumberFormat:
static public void displayNumber(Locale currentLocale) {
Integer quantity = new Integer(123456);
Double amount = new Double(345987.246);
NumberFormat numberFormatter;
String quantityOut;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
quantityOut = numberFormatter.format(quantity);
amountOut = numberFormatter.format(amount);
System.out.println(quantityOut + " " + currentLocale.toString());
System.out.println(amountOut + " " + currentLocale.toString());
}
static public void displayCurrency(Locale currentLocale) {
Double currency = new Double(9876543.21);
NumberFormat currencyFormatter;
String currencyOut;
currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
currencyOut = currencyFormatter.format(currency);
System.out.println(currencyOut + " " + currentLocale.toString());
}
static public void displayPercent(Locale currentLocale) {
Double percent = new Double(0.75);
NumberFormat percentFormatter;
String percentOut;
percentFormatter = NumberFormat.getPercentInstance(currentLocale);
percentOut = percentFormatter.format(percent);
System.out.println(percentOut + " " + currentLocale.toString());
}
Simple MessageFormat:
static void displayMessage(Locale currentLocale) {
System.out.println("currentLocale = " + currentLocale.toString());
System.out.println();
ResourceBundle messages =
ResourceBundle.getBundle("MessageBundle",currentLocale);
Object[] messageArguments = {
messages.getString("planet"),
new Integer(7),
new Date()
};
MessageFormat formatter = new MessageFormat("");
formatter.setLocale(currentLocale);
formatter.applyPattern(messages.getString("template"));
String output = formatter.format(messageArguments);
System.out.println(output);
}
References:
http://www.jorendorff.com/articles/unicode/index.html
No comments
Struts, Tomcat, MySQL and Eclipse
1. Install tomcat
2. Install Struts framework: http://struts.apache.org/
3. download Eclipse (webtools)
4. download Eclipse Tomcat plugin:
http://www.eclipsetotale.com/tomcatPlugin.html#A3
and unzip and copy it to C:\eclipse\plugins
5. Install mysql
6. Business Logic Frameworks
http://springframework.org/
7. Data Access Frameworks
http://www.hibernate.org/
8. Tutorials:
http://struts.apache.org/2.x/docs/big-picture.html
http://struts.apache.org/primer.html
http://struts.apache.org/2.x/docs/hello-world.html
http://javaboutique.internet.com/tutorials/three/
No commentsHow to use PEAR i18n library
1. Download PEAR i18n package to your local machine
http://pear.php.net/packages.php?catpid=28&catname=Internationalization
2. Upload it to your server
3. Add the library path to php include_path using ini_set:
ini_set (”include_path”, “.:/www/facebook/pear:/usr/local/php5/lib/php”);
4. Use it
//loading PEAR I18Nv2
require_once ‘I18Nv2.php’;
require_once ‘I18Nv2/Locale.php’;
require_once ‘I18Nv2/Language.php’;
$i18nv2_lang = &new I18Nv2_Language($locale, ‘UTF-8′);
$i18nv2_locale = &I18Nv2::createLocale($locale);
- Getting translated language names
foreach ($i18nv2_lang->codes as $lang_code => $lang_translated) {
…
}
- Getting formatted date & time
$fdate= $i18nv2_locale->formatDate($time, I18Nv2_DATETIME_MEDIUM) ;
$ftime = $i18nv2_locale->formatTime($time, I18Nv2_DATETIME_MEDIUM);
How to use session with Facebook apps
if (isset($_POST[”fb_sig_session_key”]))
{
$_fb_sig_session_key = str_replace(”-”,”0″,$_POST[”fb_sig_session_key”]);
session_id($_fb_sig_session_key);
}
session_start();