Multi-language package

xiaoxiao2021-03-06  115

Multilingual packages use the review of Resource, all features are defined in resource.

There is a main method in the XMLResource class, which can be run directly.

Execute the XMLResource.Import method before calling.

The software has enabled a resource file as an XML file, and the format is finally shown.

The following files are compiled and run

======================= rouce.java =================================================================================================================================================================================

Package org.fswan;

Import java.util.locale;

Import java.util.properties;

/ **

* @Author Swan Fong (Fang Zhiwen)

* e-mail: fswan@yah.net

* Use the interface to read the resource file, get different strings with different languages

* If you get the same incoming area code for the resource is null, use the default area code.

* /

Public interface resource {

/ **

* Get the content of resources corresponding to the name

* @Param Name Name

* @Param locale language

* @return resources content

* /

Public String GetResource (String Name, Locale Locale);

/ **

* Get the content of resources with parameters

* @Param Name resource name

* @Param Property resource parameter value

* @Param locale language

* @Return's content

* /

Public String getResource (String Name, String [] Property, Locale Locale;

/ **

* Get a group of resources

* If the directory is named Common.title, it is displayed in all the resources under Title under Common.

* Property is the parameter name, corresponding to the length of the resource under Category, the second dimension length. If the length is less than the length of Category

* The last one as the parameters of all resources thereafter, if all resources are not parameters if NULL.

* @Param category directory name

* @Param Property parameters

* @Param locale language

* @return resource array

* /

Public Properties GetResource (String Category, String [] Property, Locale Locale;

/ **

* Get the name of all resources under the directory

* @Param category directory name

* @Param locale language

* @Return's arrays of all children

* /

PUBLIC STRING [] GetResourceNames (String Category, Locale Locale);

/ **

* Get all resources or subdirectories under the directory

* @Param category directory name

* @Param iscategory returns a subdirectory or resources

* @Param locale language

* @return returned content

* /

Public String [] GetResourceNames (String Category, Boolean ISCategory, Locale Locale);

================================================

============== XmlResource.java ======================

Package org.fswan;

Import java.io.file;

Import java.io.ioException;

Import java.util.arraylist;

Import java.util.hashtable;

Import java.util.locale;

Import java.util.properties;

Import java.util.StringTokenizer;

Import javax.xml.parsers.documentbuilder;

Import javax.xml.parsers.documentBuilderFactory;

Import javax.xml.parsers.FactoryConfigurationError;

Import javax.xml.parsers.ParserConfigurationException;

Import org.w3c.dom.document;

Import Org.w3c.dom.Element;

Import org.w3c.dom.nodeelist;

Import org.xml.sax.saxexception;

/ **

* @Author Swan Fong (Fang Zhiwen)

* E-mail: fswan@yeah.net

* Get the class from the XML file

* The location of the resource is separated from the tree layer.

* If the resource file content is the following internal space

*

* title1 title2

*

*

* Get the contents of resource form1.title1 to Title1

* Get the contents of resource Form1 as Title1 and Title2

*

* You can also replace% 1, such as title of% 1

* When you use GetResource ("XXXX", "BOOK"), you will become Title of Book.

*

* /

Public Class XmlResource Implements Resource

{

/ **

* Used to store resources, Hashtable's key is Locale type, the value is ELEMENT, corresponding to different locale has different Document

* /

Private static hashtable resources;

/ **

* Default language

* /

Private Static Locale DefaultLocale = New Locale ("EN", "CN"); static {

ArrayList List = ResourceLib.getxmlResource ();

For (int i = 0; i

{

ImportXML (list.get (i) .tostring ());

}

}

Public Static Void ImportXML (String FileName)

{

Try

{

DocumentBuilderFactory Builderfac = DocumentBuilderFactory.newInstance ();

DocumentBuilder Builder = builderfac.newdocumentbuilder ();

Document Doc = NULL;

IF (New File (filename) .exists ())

DOC = Builder.Parse (New File (file (filename));

Else

Doc = builder.Parse (XmlResource.class.getResourceASstream (FileName));

Element root = doc.getdocumentelement ();

Nodelist nl = root.getLetelementsBytagname ("resource");

Resources = new hashtable ();

For (int i = 0; i

{

ELEMENT EL = (Element) NL.Item (i);

Locale Locale = New Locale (El.getaTribute ("Language"), El.getaTribute ("Country"));

Resources.Put (Locale, EL);

}

} catch (FactoryConfigurationError E)

{

E.PrintStackTrace ();

} catch (ParserConfigurationException E)

{

E.PrintStackTrace ();

} catch (SAXEXCEPTION E)

{

E.PrintStackTrace ();

} catch (ioException e)

{

E.PrintStackTrace ();

}

}

/ * (non-javadoc)

* @see org.fswan.db.resource # getResource (java.lang.string, java.util.locale)

* /

Public String GetResource (String Name, Locale Locale)

{

IF (locale == null)

Locale = defaultlocale;

ELEMENT EL = NULL;

IF (resources.get (locale)! = null)

EL = (element) resources.get (locale);

Else

EL = (Element) resources.get (defaultlocale);

StringTokenizer Stk = New StringTokenizer (Name, ".");

While (STK.hasmoreToKens ())

{

Nodelist nl = el.GtelementsBytagname (STK.NEXTTOKEN ()); if (nl.getlength () <1)

Return NULL;

EL = (element) nl.Item (0);

}

Return el.getchildNodes (). Item (0) .GetnodeValue ();

}

/ * (non-javadoc)

* @see org.fswan.db.resource # getResource (java.lang.string, java.lang.string [], java.util.locale)

* /

Public string getresource (string name, string [] Property, Locale Locale)

{

IF (Property == Null || Property.Length == 0)

Property = new string [] {""};

String Tmpstr = getResource (name, local);

StringBuffer SB = New StringBuffer (TMPSTR);

INT i = 1;

INT POS = getPosition (SB, I);

While (POS! = -1)

{

Sb.replace

POS,

POS ("%" i) .length (),

Property [(i - 1> Property.length)? Property.Length - 1: i - 1]);

i ;

POS = getPosition (SB, I);

}

Return sb.toString ();

}

/ **

* Get the location of% i in the StringBuffer, if you are / wrong, if you can't find it, return -1

* @Param Str To find the string

* @Param i to find the content

* @return location

* /

Private int getPosition (StringBuffer Str, INT i)

{

INT POS = Str.indexof ("%" i);

While (POS! = -1 && str.charat (POS - 1) == '//')

{

POS = Str.Indexof ("%" i, pOS);

}

Return POS;

}

/ * (non-javadoc)

* @see org.fswan.db.resource # getResource (java.lang.string, java.lang.string [] [], java.util.locale)

* /

Public Properties GetResource (String Category, String "[] Property, Locale Locale

{

IF (Property == Null || Property.Length == 0)

{

Property = New String [1] [1];

Property [0] [0] = ""

}

String [] Names = GetResourceNames (Category, False, Locale);

String [] Values ​​= new string [names.length];

Properties P = New Properties (); for (int i = 0; i

{

String [] Pro = Property [(i> Property.Length - 1)? Property.Length - 1: i];

VALUES [I] = GetResource (Category "." Names [i], pro, local);

P.SetProperty (Names [i], values ​​[i]);

}

Return P;

}

/ * (non-javadoc)

* @see org.fswan.db.resource # getResourceNames (java.lang.string, boolean, java.util.locale)

* /

Public String [] GetResourceNames (String Category, Boolean ISCategory, Locale Locale)

{

IF (locale == null)

Locale = defaultlocale;

ELEMENT EL = NULL;

IF (resources.get (locale)! = null)

EL = (element) resources.get (locale);

Else

EL = (Element) resources.get (defaultlocale);

StringTokenizer Stk = New StringTokenizer (category, ".");

While (STK.hasmoreToKens ())

{

Nodelist NL = El.GtelementsBytagname (STK.NEXTTOKEN ());

IF (nl.getlength () <1)

Return NULL;

EL = (element) nl.Item (0);

}

Nodelist NL = El.getElementsBytagname ("*");

IF (nl == null || nl.getLength () == 0)

Return new string [] {

}

String [] resources = new string [nl.getLength ()];

For (int i = 0; i

{

Resources [i] = nl.Item (i) .Getnodename ();

}

Return resources;

}

/ * (non-javadoc)

* @see org.fswan.db.resource # getResourceNames (java.lang.string, java.util.locale)

* /

Public string [] getResourceNames (String Category, Locale Locale)

{

IF (locale == null)

Locale = defaultlocale;

ELEMENT EL = NULL;

IF (resources.get (locale)! = null)

EL = (element) resources.get (locale);

Else

EL = (Element) resources.get (defaultlocale);

StringTokenizer Stk = New StringTokenizer (category, "."); While (STK.hasmoreToKens ())

{

Nodelist NL = El.GtelementsBytagname (STK.NEXTTOKEN ());

IF (nl.getlength () <1)

Return NULL;

EL = (element) nl.Item (0);

}

Nodelist NL = El.getElementsBytagname ("*");

IF (nl == null || nl.getLength () == 0)

Return new string [] {

}

String [] resources = new string [nl.getLength ()];

For (int i = 0; i

{

Resources [i] = nl.Item (i) .Getnodename ();

}

Return resources;

}

Public static void main (string [] args)

{

XmlResource.ImportXML ("COM / FSWAN / Application / AddressBook / Resource.xml");

Properties first = new XmlResource (). Getresource ("addressbook", (String [] []) null, null;

Object [] s = first.keyset (). Toarray ();

For (int i = 0; i

{

System.out.println (s [i] " first.getproperty (s [i] .tostring ()))));

}

}

}

========================== ========================================================================================================================================================================================================================== ==========

======================================================================================================================================================================================================== =====

Human Resource Management System </ Title></p> <p><menu></p> <p><File> File </ file></p> <p><new> new </ new></p> <p><save> save </ save></p> <p><Load> Load </ loading></p> <p><saveas> save as ... </ saveas></p> <p><Edit> Edit </ Edit></p> <p><copy> Copy </ copy></p> <p><Parse> Parse </ PARSE></p> <p><delete> delete </ delete></p> <p><NextPage> Next Page </ nextpage></p> <p><Tool> Tool </ TOOL></p> <p><Language> Language </ Language></p> <p><default> Default </ default></p> <p><english> English </ English></p> <p><china> Simplified </ China></p> <p><tchina> Tradition </ tchina></p> <p><Employee> Employee Management </ EMPLOYEE></p> <p><detail> detail </ detail></p> <p><Employeelist> Employee List </ EMPLOYELIST></p> <p><Lookandfeel> Look and Feel </ LookandFeel></p> <p><javalook> Java Look </ javalook></p> <p><Motiflook> Motif Look </ Motiflook></p> <p><Windowlook> Window Look </ Windowlook></p> <p><defaultlook> Default </ defaultlook></p> <p><Emeraldlook> Emerald </ Emeraldlook></p> <p><sapphi> sapphire </ saphire></p> <p></ menu></p> <p></ mainframe></p> <p></ resource></p> <p><resource language = "zh" country = "cn"></p> <p><mainframe></p> <p><Title> Human Resource Management System </ Title></p> <p><menu></p> <p><File> Document </ file></p> <p><New> New </ New></p> <p><save> Save </ save></p> <p><load> storage </ loading></p> <p><saveas> Save As </ saveas></p> <p><Edit> Edit </ Edit></p> <p><copy> Copy </ copy></p> <p><PARSE> Paste </ Parse></p> <p><delete> Delete </ delete></p> <p><NextPage> Next page </ nextpage></p> <p><TOOL> Tool </ Tool></p> <p><Language> Language </ Language> <Default> Default </ default></p> <p><china> Chinese </ China></p> <p><tchina> Traditional Chinese </ tchina></p> <p><中文> English </ eNGLish></p> <p><Employee> Employee Management </ Employee></p> <p><Detail> Employee Information </ detail></p> <p><Employeelist> Well »</ EMPLOYEELIST></p> <p><Lookandfeel> Appearance </ LookandFeel></p> <p><javalook> Java appearance </ javalook></p> <p><Motiflook> MotiF appearance </ Motiflook></p> <p><Windowlook> Windows appearance </ Windowlook></p> <p><defaultlook> Default </ defaultlook></p> <p><Emeraldlook> Emerald </ Emeraldlook></p> <p><sapphire> Sea Blue Gem </ SAPPHIRE></p> <p></ menu></p> <p></ mainframe></p> <p></ resource></p> <p></ root></p> <p>========================= End ====================================================================================================================================================== ===========</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-98976.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="98976" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.045</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'ZSUl2z6qi_2B_2F4SEFCsrh5sGf6sEov9RxkhAEDNlWNUbiTy9HLK5COF6VE8cyorgQxB_2BIrOjj46XHKmWxcJoCS4w_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>