Bài tập 1
File I18N.java:
//package i18n;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;
/**
*
* @author LongDT
*/
public class I18N {
public static void main(String[] args) {
String str="Internationalization can also be abbreviated as i18n because there are 18 characters in between I and N. Internationalization is a technique that allows us to create applications that can be adapted to many different languages, different regions. Localization can also be abbreviated as l10n because the 10 characters are in between L and N.";
Locale.setDefault(new Locale("vi"));
ResourceBundle labels = ResourceBundle.getBundle("messages");
String result="";
String s;
try (Scanner input = new Scanner(str)) {
while(input.hasNext()) {
s = input.next();
if (labels.containsKey(s))
result+=labels.getString(s)+" ";
}
}
result=result.trim();
System.out.println(result);
}
}
File messages_vi.properties:
Internationalization=Quoc te hoa
abbreviated=viet tat
applications=cac ung dung
regions.=vung mien.
technique=ky thuat
characters=ky tu
create=tao
because=boi vi
different=khac nhau
between=giua
allows=cho phep
languages=ngon ngu
adapted=thich nghi
can=co the
that=ma
are=la
and=va
in=o
also=cung
is=la
many=nhieu
the=
as=nhu la
there=co
to=
be=la
us=chung ta
i18n=i18n
18=18
I=I
N.=N.
Localization=Localization
l10n=l10n
10=10
L=L
Bài tập 2
File I18N.java:
//package i18n;
import java.io.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;
/**
*
* @author LongDT
*/
public class I18N {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("en.txt"));
// String str="Internationalization can also be abbreviated as i18n because there are 18 characters in between I and N. Internationalization is a technique that allows us to create applications that can be adapted to many different languages, different regions. Localization can also be abbreviated as l10n because the 10 characters are in between L and N.";
Locale.setDefault(new Locale("vi"));
ResourceBundle labels = ResourceBundle.getBundle("messages");
String result="";
String s;
try (Scanner input = new Scanner(br)) {
while(input.hasNext()) {
s = input.next();
if (labels.containsKey(s))
result+=labels.getString(s)+" ";
}
}finally {
if(br!=null)
br.close();
}
result=result.trim();
BufferedWriter bw=new BufferedWriter(new FileWriter("en.txt"));
bw.write(result);
bw.close();
}
}
File en.txt:
Quoc te hoa co the cung la viet tat nhu la i18n boi vi co la 18 ky tu o giua I va N. Quoc te hoa la ky thuat ma cho phep chung ta tao cac ung dung ma co the la thich nghi nhieu khac nhau khac nhau vung mien. Localization co the cung la viet tat nhu la l10n boi vi 10 ky tu la o giua L va N.