String management and multilanguage support in J2ME

Source: Internet
Author: User
Tags resource stringbuffer

Recent study J2ME Development, also talk about my string management of a solution.

The purpose of string management is to manage strings in a resource-managed format.

1. This system should be accessible through static methods;

2. The string is stored in the resource;

3. can easily switch various languages to achieve multi-language support;

4. A string resource has a name that accesses the resource by name;

5. The rule of the first name should be the class name + internal name.

The next question is what format the resource is stored in:

Logically speaking, XML is the ideal way, but considering the particularity of J2ME system, it is more suitable to use plain text.

Format is String-name String-value

The name and value are split between spaces or \ t, and the different strings are delimited by \ n.

Internal use of Hashtable storage.

Through the above analysis, you can write the following Stringmanager

Package Vmlinux.app
Import java.io.*;
import java.util.*;
public class Stringmanager {
String l Ang_;
String name_;
Hashtable Strings_;
Boolean loaded_;
Public Stringmanager (String Lang)
{
Lang_=lang
Loaded_=false;
}
public void Setlang (String lang)
{
if (Lang_==lang)
return;
Lang_=lang;
Loaded_=false;
}
public void Load (String name)
{
if (name==name_ && loaded_)
return;
if (strings_==null)
St Rings_=new Hashtable ();
Else
Strings_.clear ();
InputStream Is=this.getclass (). getResourceAsStream ("/strings/" +name+ "." +lang_);
Try
{
byte[] buf=new byte[1024];
int c=is.read (BUF);
StringBuffer o=new StringBuffer ();
String Key=null;
do{
String data=new string (buf,0,c);
int s=0;
for (int i=0;i
{
Char x=data.charat (i);
if (s==0 && (x== ' | | x== ' \ t '))
{
key=o.tostring ();
O.delete (0,o.length ());
S=1;

Else if (s==1 && x== ' \ n ')
{
if (key!=null && key.length () >0)
Strings_.put (Key, O.tostring ());
Key=null;
O.delete (0,o.length ());
S=0;
}
Else
O.append (x);
}
}while ((C=is.read (BUF)) >0);
if (key!=null && key.length () >0)
Strings_.put (key,o.tostring ());
Is.close ();
}
catch (Exception ex)
{
}
Name_=name;
Loaded_=true;
}
public string getString (string name)
{
Object o=strings_.get (name),
if (o==null)
return name;
Els E
Return (String) o;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.