Dealing with Compound MessagesA compound message may contain several k การแปล - Dealing with Compound MessagesA compound message may contain several k ไทย วิธีการพูด

Dealing with Compound MessagesA com

Dealing with Compound Messages
A compound message may contain several kinds of variables: dates, times, strings, numbers, currencies, and percentages. To format a compound message in a locale-independent manner, you construct a pattern that you apply to a MessageFormat object, and store this pattern in a ResourceBundle.

By stepping through a sample program, this section demonstrates how to internationalize a compound message. The sample program makes use of the MessageFormat class. The full source code for this program is in the file called MessageFormatDemo.java. The German locale properties are in the file called MessageBundle_de_DE.properties.

1. Identify the Variables in the Message
Suppose that you want to internationalize the following message:


Notice that we've underlined the variable data and have identified what kind of objects will represent this data.

2. Isolate the Message Pattern in a ResourceBundle
Store the message in a ResourceBundle named MessageBundle, as follows:

ResourceBundle messages =
ResourceBundle.getBundle("MessageBundle", currentLocale);
This ResourceBundle is backed by a properties file for each Locale. Since the ResourceBundle is called MessageBundle, the properties file for U.S. English is named MessageBundle_en_US.properties. The contents of this file is as follows:

template = At {2,time,short} on {2,date,long},
we detected {1,number,integer} spaceships on
the planet {0}.
planet = Mars
The first line of the properties file contains the message pattern. If you compare this pattern with the message text shown in step 1, you'll see that an argument enclosed in braces replaces each variable in the message text. Each argument starts with a digit called the argument number, which matches the index of an element in an Object array that holds the argument values. Note that in the pattern the argument numbers are not in any particular order. You can place the arguments anywhere in the pattern. The only requirement is that the argument number have a matching element in the array of argument values.

The next step discusses the argument value array, but first let's look at each of the arguments in the pattern. The following table provides some details about the arguments:

Arguments for template in MessageBundle_en_US.properties Argument Description
{2,time,short} The time portion of a Date object. The short style specifies the DateFormat.SHORT formatting style.
{2,date,long} The date portion of a Date object. The same Date object is used for both the date and time variables. In the Object array of arguments the index of the element holding the Date object is 2. (This is described in the next step.)
{1,number,integer} A Number object, further qualified with the integer number style.
{0} The String in the ResourceBundle that corresponds to the planet key.

For a full description of the argument syntax, see the API documentation for the MessageFormat class.

3. Set the Message Arguments
The following lines of code assign values to each argument in the pattern. The indexes of the elements in the messageArguments array match the argument numbers in the pattern. For example, the Integer element at index 1 corresponds to the {1,number,integer} argument in the pattern. Because it must be translated, the String object at element 0 will be fetched from the ResourceBundle with the getString method. Here is the code that defines the array of message arguments:

Object[] messageArguments = {
messages.getString("planet"),
new Integer(7),
new Date()
};
4. Create the Formatter
Next, create a MessageFormat object. You set the Locale because the message contains Date and Number objects, which should be formatted in a locale-sensitive manner.

MessageFormat formatter = new MessageFormat("");
formatter.setLocale(currentLocale);
5. Format the Message Using the Pattern and the Arguments
This step shows how the pattern, message arguments, and formatter all work together. First, fetch the pattern String from the ResourceBundle with the getString method. The key to the pattern is template. Pass the pattern String to the formatter with the applyPattern method. Then format the message using the array of message arguments, by invoking the format method. The String returned by the format method is ready to be displayed. All of this is accomplished with just two lines of code:

formatter.applyPattern(messages.getString("template"));
String output = formatter.format(messageArguments);
6. Run the Demo Program
The demo program prints the translated messages for the English and German locales and properly formats the date and time variables. Note that the English and German verbs ("detected" and "entdeckt") are in different locations relative to the variables:

currentLocale = en_US
At 10:16 AM on July 31, 2009, we detected 7
spaceships on the planet Mars.
currentLocale = de_DE
Um 10:16 am 31. Juli 2009 haben wir 7 Raumschiffe
auf dem Planeten Mars entdeckt.
0/5000
จาก: -
เป็น: -
ผลลัพธ์ (ไทย) 1: [สำเนา]
คัดลอก!
การจัดการกับข้อความที่ซับซ้อนข้อความที่ซับซ้อนอาจประกอบด้วยตัวแปรหลายชนิด: วัน เวลา สาย หมายเลข สกุลเงิน และเปอร์เซ็นต์การ การจัดรูปแบบข้อความที่ซับซ้อนในลักษณะที่ไม่ขึ้นกับตำแหน่งที่ตั้ง คุณสร้างรูปแบบที่คุณใช้วัตถุ MessageFormat และเก็บรูปแบบนี้ในการ ResourceBundleโดยดำเนินงานผ่านทางโปรแกรมตัวอย่าง ส่วนนี้อธิบายวิธีการ internationalize ข้อความผสม โปรแกรมตัวอย่างช่วยให้ใช้ MessageFormat การเรียนการ รหัสแหล่งที่มาทั้งหมดสำหรับโปรแกรมนี้เป็นไฟล์ที่เรียกว่า MessageFormatDemo.java คุณสมบัติภาษาเยอรมันอยู่ในไฟล์ที่เรียกว่า MessageBundle_de_DE.properties1. ตัวแปรในข้อความระบุสมมติว่า คุณต้องการ internationalize ข้อความต่อไปนี้:สังเกตว่า เราได้ขีดเส้นใต้ข้อมูลตัวแปร และระบุชนิดของวัตถุจะแสดงข้อมูลนี้2. รูปแบบข้อความใน ResourceBundle แยกเก็บข้อความใน ResourceBundle ที่ชื่อ MessageBundle เป็นดังนี้:ข้อความที่ ResourceBundle =ResourceBundle.getBundle ("MessageBundle", currentLocale);ResourceBundle นี้มีสนับสนุนจากคุณสมบัติแฟ้มสำหรับแต่ละภาษา เนื่องจาก ResourceBundle เรียกว่า MessageBundle แฟ้มคุณสมบัติสำหรับสหรัฐอเมริกาอังกฤษมีชื่อว่า MessageBundle_en_US.properties เนื้อหาของแฟ้มนี้จะเป็นดังนี้:แม่ =ที่ {2 เวลา สั้น} กับ {2 วัน ยาว}, เราพบ {1 หมายเลข เต็ม} ยานอวกาศใน โลก { 0 }ดาวเคราะห์ =ดาวอังคารบรรทัดแรกของแฟ้มคุณสมบัติประกอบด้วยรูปแบบข้อความ ถ้าคุณเปรียบเทียบรูปแบบนี้ มีข้อความแสดงในขั้นตอนที่ 1 คุณจะเห็นว่า มีอาร์กิวเมนต์ที่อยู่ในเครื่องหมายวงเล็บปีกกาแทนแต่ละตัวแปรในข้อความ อาร์กิวเมนต์แต่ละเริ่มต้น ด้วยตัวเลขที่เรียกว่าอาร์กิวเมนต์หมาย ซึ่งตรงกับจำนวนในอาร์เรย์วัตถุที่มีค่าอาร์กิวเมนต์ หมายเหตุในรูปแบบ หมายเลขของอาร์กิวเมนต์ไม่มีลำดับ คุณสามารถใส่อาร์กิวเมนต์ใด ๆ ในรูปแบบ ความต้องการเท่านั้นได้ว่า หมายเลขของอาร์กิวเมนต์ที่มีองค์ประกอบที่ตรงกันในอาร์เรย์ของค่าอาร์กิวเมนต์ขั้นตอนต่อไปกล่าวถึงอาร์กิวเมนต์ค่าอาร์เรย์ แต่แรกลองดูแต่ละอาร์กิวเมนต์ในรูปแบบการ ตารางต่อไปนี้ให้รายละเอียดบางอย่างเกี่ยวกับอาร์กิวเมนต์:อาร์กิวเมนต์สำหรับแม่แบบในคำอธิบายของอาร์กิวเมนต์ MessageBundle_en_US.properties {2 เวลา ย่อ} ส่วนเวลาของวัตถุวัน แบบสั้น ๆ ระบุ DateFormat.SHORT ที่จัดรูปแบบลักษณะ {2 วัน ยาว} ส่วนวันที่ของวันวัตถุ วัตถุวันเดียวกันจะใช้สำหรับตัวแปรวันและเวลา ในอาร์เรย์วัตถุของอาร์กิวเมนต์ ดัชนีองค์ประกอบถือวัตถุวันคือ 2 (ซึ่งจะอธิบายไว้ในขั้นตอนถัดไป) {หมายเลข จำนวนเต็ม 1 } วัตถุหมายเลข มีคุณสมบัติเพิ่มเติมกับแบบเลขจำนวนเต็ม {0} อักขระที่ใน ResourceBundle ที่สอดคล้องกับกุญแจสำคัญในโลก สำหรับคำอธิบายเต็มของไวยากรณ์อาร์กิวเมนต์ ดูเอกสาร API สำหรับคลาส MessageFormat3. ตั้งค่าอาร์กิวเมนต์ข้อความบรรทัดของรหัสต่อไปนี้กำหนดค่าให้กับแต่ละอาร์กิวเมนต์ในรูปแบบ ดัชนีขององค์ประกอบในอาร์เรย์ messageArguments ตรงกับหมายเลขของอาร์กิวเมนต์ในรูปแบบ องค์ประกอบที่ 1 ดัชนีจำนวนเต็มสอดคล้องกับตัวอย่าง {1 หมายเลข จำนวนเต็ม} อาร์กิวเมนต์ในรูปแบบการ เนื่องจากต้องแปลว่า วัตถุสตริงที่องค์ประกอบ 0 จะถูกดึงจาก ResourceBundle ด้วยวิธี getString นี่คือรหัสที่กำหนดแถวลำดับของอาร์กิวเมนต์ข้อความ:วัตถุ messageArguments [] ={messages.getString("planet")Integer(7) ใหม่Date() ใหม่};4. สร้าง Formatter แบบถัดไป สร้างวัตถุ MessageFormat คุณตั้งค่าตำแหน่งกระทำการเนื่องจากข้อความประกอบด้วยวันและวัตถุหมายเลข ที่ควรจัดรูปแบบในลักษณะที่ตรงตามตำแหน่งที่ตั้งMessageFormat formatter = MessageFormat("") ใหม่formatter.setLocale(currentLocale)5. รูปแบบข้อความโดยใช้รูปแบบและอาร์กิวเมนต์ขั้นตอนนี้แสดงวิธีการรูปแบบ อาร์กิวเมนต์ข้อความ และ formatter ทั้งหมดทำงานร่วมกัน ครั้งแรก ดึงรูปแบบสายอักขระจาก ResourceBundle ด้วยวิธี getString หลักสำคัญของรูปแบบคือ แบบ ผ่านรูปแบบสายอักขระเพื่อที่ formatter ด้วยวิธี applyPattern แล้ว จัดรูปแบบข้อความโดยใช้อาร์เรย์ของอาร์กิวเมนต์ข้อความ โดยเมธอดรูปแบบ สายอักขระที่ส่งกลับ โดยวิธีการรูปแบบพร้อมจะแสดง ทั้งหมดนี้ได้ มีเพียงสองบรรทัดของรหัส:formatter.applyPattern(messages.getString("template"))สตริงที่ผล = formatter.format(messageArguments)6. โปรแกรมสาธิตโปรแกรมสาธิตพิมพ์ข้อความที่แปลสำหรับภาษาอังกฤษและเยอรมัน และถูกจัดรูปแบบตัวแปรวันและเวลา โปรดสังเกตว่า ในภาษาอังกฤษ และภาษาเยอรมันคำกริยา ("พบ" และ "entdeckt") อยู่ในสถานที่ที่สัมพันธ์กับตัวแปร:currentLocale = en_USเวลา 10:16 น.วันที่ 31 กรกฎาคม 2552 เราพบ 7ยานอวกาศบนดาวอังคารดาวเคราะห์currentLocale = de_DEอึม 10:16 น. 31 Wir haben Juli 2009 7 Raumschiffeเอาฟ์ dem entdeckt ดาวอังคาร Planeten
การแปล กรุณารอสักครู่..
ผลลัพธ์ (ไทย) 3:[สำเนา]
คัดลอก!
จัดการกับข้อความสารประกอบ
สารประกอบข้อความอาจประกอบด้วยหลายประเภทของตัวแปร : วันที่ , เวลา , สตริง , ตัวเลข , สกุลเงิน , และร้อยละ การจัดรูปแบบข้อความในรูปแบบภาษาอิสระผสมลักษณะที่คุณสร้างรูปแบบที่คุณใช้กับ messageformat วัตถุและเก็บรูปแบบนี้ใน resourcebundle

โดยก้าวผ่านโปรแกรมตัวอย่างส่วนนี้แสดงให้เห็นถึงวิธีที่จะทำให้เป็นสากลและข้อความ โปรแกรมตัวอย่างที่ทำให้การใช้งานของ messageformat ชั้นเรียน รหัสแหล่งที่มาเต็มรูปแบบสำหรับโปรแกรมนี้อยู่ในไฟล์ที่เรียกว่า messageformatdemo.java . คุณสมบัติสถานที่เยอรมันอยู่ในไฟล์ที่เรียกว่า messagebundle_de_de คุณสมบัติ .

1 . ระบุตัวแปรในข้อความ
สมมติว่าคุณต้องการที่จะสากลนิยมข้อความต่อไปนี้ :


สังเกตว่าเราได้ขีดเส้นใต้ตัวแปรข้อมูลและระบุชนิดของวัตถุจะแสดงข้อมูลนี้

2 แยกข้อความแบบใน resourcebundle
เก็บข้อความใน resourcebundle ชื่อ messagebundle ดังนี้

resourcebundle ข้อความ =
resourcebundle . getbundle ( " messagebundle "
currentlocale )resourcebundle นี้ได้รับการสนับสนุนโดยคุณสมบัติของแฟ้มสำหรับแต่ละสถานที่ ตั้งแต่ resourcebundle เรียกว่า messagebundle แฟ้ม คุณสมบัติสำหรับสหรัฐอเมริกา ภาษาอังกฤษ เป็นชื่อ messagebundle_en_us.properties . เนื้อหาของแฟ้มนี้จะเป็นดังนี้ :

ที่แม่แบบ = { 2 , เวลาสั้น } { 2 } , วันที่ , ยาว , เราพบ n
{ 1 , } เลขจำนวนเต็มบนยานอวกาศ
โลก { 0 } .

= ดาวอังคารดาวเคราะห์บรรทัดแรกของไฟล์คุณสมบัติประกอบด้วยข้อความรูปแบบ ถ้าคุณเปรียบเทียบรูปแบบนี้กับข้อความที่แสดงในขั้นตอนที่ 1 , คุณจะเห็นว่า การโต้แย้งอยู่ในวงเล็บปีกกาแทนที่ตัวแปรแต่ละตัวในข้อความ แต่ละอาร์กิวเมนต์ที่เริ่มต้นด้วยตัวเลขที่เรียกว่าอาร์กิวเมนต์จำนวน ซึ่งตรงกับดัชนีขององค์ประกอบในวัตถุอาร์เรย์ที่มีอาร์กิวเมนต์ค่าทราบว่าในรูปแบบของอาร์กิวเมนต์ตัวเลขไม่ใช่ในส่วนใด คุณสามารถวางที่ใดก็ได้ในการรูปแบบ ความต้องการเพียงอย่างเดียวคืออาร์กิวเมนต์ตัวเลขมีการจับองค์ประกอบในอาร์เรย์ของอาร์กิวเมนต์ค่า

ต่อไปกล่าวถึงค่าอาร์กิวเมนต์อาร์เรย์ แต่อย่างแรก เรามาดูแต่ละอาร์กิวเมนต์ในรูปแบบ
การแปล กรุณารอสักครู่..
 
ภาษาอื่น ๆ
การสนับสนุนเครื่องมือแปลภาษา: กรีก, กันนาดา, กาลิเชียน, คลิงออน, คอร์สิกา, คาซัค, คาตาลัน, คินยารวันดา, คีร์กิซ, คุชราต, จอร์เจีย, จีน, จีนดั้งเดิม, ชวา, ชิเชวา, ซามัว, ซีบัวโน, ซุนดา, ซูลู, ญี่ปุ่น, ดัตช์, ตรวจหาภาษา, ตุรกี, ทมิฬ, ทาจิก, ทาทาร์, นอร์เวย์, บอสเนีย, บัลแกเรีย, บาสก์, ปัญจาป, ฝรั่งเศส, พาชตู, ฟริเชียน, ฟินแลนด์, ฟิลิปปินส์, ภาษาอินโดนีเซี, มองโกเลีย, มัลทีส, มาซีโดเนีย, มาราฐี, มาลากาซี, มาลายาลัม, มาเลย์, ม้ง, ยิดดิช, ยูเครน, รัสเซีย, ละติน, ลักเซมเบิร์ก, ลัตเวีย, ลาว, ลิทัวเนีย, สวาฮิลี, สวีเดน, สิงหล, สินธี, สเปน, สโลวัก, สโลวีเนีย, อังกฤษ, อัมฮาริก, อาร์เซอร์ไบจัน, อาร์เมเนีย, อาหรับ, อิกโบ, อิตาลี, อุยกูร์, อุสเบกิสถาน, อูรดู, ฮังการี, ฮัวซา, ฮาวาย, ฮินดี, ฮีบรู, เกลิกสกอต, เกาหลี, เขมร, เคิร์ด, เช็ก, เซอร์เบียน, เซโซโท, เดนมาร์ก, เตลูกู, เติร์กเมน, เนปาล, เบงกอล, เบลารุส, เปอร์เซีย, เมารี, เมียนมา (พม่า), เยอรมัน, เวลส์, เวียดนาม, เอสเปอแรนโต, เอสโทเนีย, เฮติครีโอล, แอฟริกา, แอลเบเนีย, โคซา, โครเอเชีย, โชนา, โซมาลี, โปรตุเกส, โปแลนด์, โยรูบา, โรมาเนีย, โอเดีย (โอริยา), ไทย, ไอซ์แลนด์, ไอร์แลนด์, การแปลภาษา.

Copyright ©2024 I Love Translation. All reserved.

E-mail: