Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.basicsExamples;

import java.text.ChoiceFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;



/** ****************************************************************************
 * LOCALIZATION EXAMPLE CLASS
 * 
 * FOR LOCALIZATION NUMBER, CURRENCY, PERCENTS, AND DATE 
 * SEE CLASSES: FormatingExample and DateExample
 * 
 * @author stanislav.zorjan
 */
public class LocalizationExample {
    
   
    
    private Locale us = new Locale.Builder().setLanguage("en").setRegion("US").build();
    private Locale sr = new Locale.Builder().setLanguage("sr").setRegion("SR").build();
    private Locale cz = new Locale.Builder().setLanguage("cs").setRegion("CZ").build();
    
    private Locale loc;
    
    private ResourceBundle bundle;
    private MessageFormat mf;
    private Date date;

    

    /** ************************************************************************
     * Default constructor
     */
    public LocalizationExample() {
	
	loc = us;
	bundle  = ResourceBundle.getBundle("resources/MessagesBundle", loc);
	date = Calendar.getInstance(loc).getTime();
	
	//simpleLocalization();
	//compoundMessagesLocalization();
	//dataLocalization();
	pluralLocalization();
    }
    
    
    
    /** ************************************************************************
     * Simple localization
     */
    private void simpleLocalization(){
	System.out.println(bundle.getString("Hello"));
	System.out.println(bundle.getString("How"));
	System.out.println(bundle.getString("one"));
    }
    
    
    
    /** ************************************************************************
     * Compound message localization
     */
    private void compoundMessagesLocalization(){
	Object [] sentenceArguments = {"Mandy", "prijatelj", "planeti"};
	
	mf = new MessageFormat("");
	mf.setLocale(loc);
	mf.applyPattern(bundle.getString("sentence"));
	String str = mf.format(sentenceArguments);
	
	System.out.println(str);
    }
    
    
    
    /** ************************************************************************
     * Primitive and date localization
     */
    private void dataLocalization(){
	
	byte={0, byte, byte}
	short={0, number, short}
	int={0, number, integer}
	long={0, number, long}
	float={0, number, float}
	double={0, number, double}
	char={0}
	boolean={0, boolean}
	timeShort={0, time, short}
	timeMedium={0, time, medium}
	timeLong={0, time, long}
	timeFull={0, time, full}
	dateShort={0, date, short}
	dateMedium={0, date, medium}
	dateLong={0, date, long}
	dateFull={0, date, full}
	
	
	mf = new MessageFormat("");
	mf.setLocale(loc);
	
	Object [] byteArgument = {1};
	Object [] shortArgument = {230};
	Object [] intArgument = {123545};
	Object [] longArgument = {1235456534L};
	Object [] floatArgument = {1234.2F};
	Object [] doubleArgument = {1234.2};
	Object [] charArgument = {'c'};
	Object [] booleanArgument = {true};
	Object [] timeShortArgument = {date};
	Object [] timeMediumArgument = {date};
	Object [] timeLongArgument = {date};
	Object [] timeFullArgument = {date};
	Object [] dateShortArgument = {date};
	Object [] dateMediumArgument = {date};
	Object [] dateLongArgument = {date};
	Object [] dateFullArgument = {date};
	
	mf.applyPattern(bundle.getString("byte"));
	System.out.println(mf.format(byteArgument));
	
	mf.applyPattern(bundle.getString("short"));
	System.out.println(mf.format(shortArgument));
	
	mf.applyPattern(bundle.getString("int"));
	System.out.println(mf.format(intArgument));
	
	mf.applyPattern(bundle.getString("long"));
	System.out.println(mf.format(longArgument));
	
	mf.applyPattern(bundle.getString("float"));
	System.out.println(mf.format(floatArgument));
	
	mf.applyPattern(bundle.getString("double"));
	System.out.println(mf.format(doubleArgument));
	
	mf.applyPattern(bundle.getString("char"));
	System.out.println(mf.format(charArgument));
	
	mf.applyPattern(bundle.getString("boolean"));
	System.out.println(mf.format(booleanArgument));
	
	mf.applyPattern(bundle.getString("timeShort"));
	System.out.println(mf.format(timeShortArgument));
	
	mf.applyPattern(bundle.getString("timeMedium"));
	System.out.println(mf.format(timeMediumArgument));
	
	mf.applyPattern(bundle.getString("timeLong"));
	System.out.println(mf.format(timeLongArgument));
	
	mf.applyPattern(bundle.getString("timeFull"));
	System.out.println(mf.format(timeFullArgument));

	mf.applyPattern(bundle.getString("dateShort"));
	System.out.println(mf.format(dateShortArgument));
	
	mf.applyPattern(bundle.getString("dateMedium"));
	System.out.println(mf.format(dateMediumArgument));
	
	mf.applyPattern(bundle.getString("dateLong"));
	System.out.println(mf.format(dateLongArgument));
	
	mf.applyPattern(bundle.getString("dateFull"));
	System.out.println(mf.format(dateFullArgument));
	
    }
    
    private void pluralLocalization(){
	/*
	    itemsTemplate=There {0} in the {1}
	    noItems=are no items
	    oneItem=is one item
	    moreItems=are {0, number, integer} items
	    itemHolder=box.
	 */
	
	mf = new MessageFormat("");
	mf.setLocale(loc);
	
	double [] itemCounts = {0, 1, 2};
	String [] items = {bundle.getString("noItems"), bundle.getString("oneItem"), bundle.getString("moreItems")};
	
	ChoiceFormat cf = new ChoiceFormat(itemCounts, items);
	String itemsTemplate = bundle.getString("itemsTemplate");
	mf.applyPattern(itemsTemplate);
	
	Format[] formats = {cf, null, NumberFormat.getInstance()};
	mf.setFormats(formats);
	
	//Object [] itemArguments = {0, bundle.getString("itemHolder"), 100};
	//Object [] itemArguments = {1, bundle.getString("itemHolder"), 100};
	Object [] itemArguments = {1, bundle.getString("itemHolder"), 100};
	String out = mf.format(itemArguments);
	System.out.println(out);
	
    }
    
}