Creating Applications with Mozilla-Chapter 11. Localization-P1

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:17

lượt xem

Creating Applications with Mozilla-Chapter 11. Localization-P1

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'creating applications with mozilla-chapter 11. localization-p1', công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:

Nội dung Text: Creating Applications with Mozilla-Chapter 11. Localization-P1

  1. Chapter 11. Localization-P1 This chapter describes how to use Mozilla's internationalization (I18N) and localization (L10N) technologies to make applications usable by people around the world. Because the Mozilla community (and the Internet community in general), is global, it is vital to be able to cross language barriers by localizing your application and making it available to a wider audience. In this chapter, you are given step-by-step instructions on how to change the visible text for your application in the XUL interface and how to handle nonstatic strings that arise from dynamic string handling in other areas of your application code. While the basic technologies that are used are not new, Mozilla is innovating in areas such as Unicode support and quick access language pack installs. The information in this chapter about the internationalization ( and localization ( projects will give you a solid foundation for what is possible in your own application. 11.1. Localization Basics Before learning how to localize your Mozilla application, it's useful to run through some of the high-level goals and features of the Mozilla internationalization and localization projects. First, here are some definitions: Internationalization (I18N)
  2. The design and development of software to function in a particular locale. The shorthand term, I18N, refers to the 18 letters between the initial "i" and final "n." Localization (L10N) The modification of software to meet the language of a location and the adaptation of resources, such as the user interface (UI) and documentation, for that region. L10N is an acronym for localization and refers to the 10 letters between the initial "l" and final "n." Locale "A set of conventions affected or determined by human language and customs, as defined within a particular geo-political region. These conventions include (but are not necessarily limited to) the written language, formats for dates, numbers and currency, sorting orders, etc.," according to the official Mozilla document found at Locale in the context of this chapter is related specifically to the display of text in the user interface. The focus will be on UI localization of XUL files and strings contained in JavaScript and C++ files, as well as the methods employed for localization. Here are some main features of the Mozilla internationalization capabilities, which are relevant to the user front end application level: • Mozilla is Unicode-enabled for Latin-based languages, Cyrillic, Greek, Chinese, Japanese, and Korean. Mozilla widgets and HTML rendering can support the input and display of these languages.
  3. Unicode-enabling for other languages and character sets is an ongoing process. • Mozilla can be easily localized into different languages, even if not supported by the underlying operating system. • Most Mozilla localization work involves translating strings as entities in Document Type Definition (DTD) format and properties file format (an idea taken from Java), which are based on open standards. • Localization can be done once and run on Windows, Macintosh, Unix, and other platforms -- something we have come to expect from the Mozilla framework. This is a great time saver, and indeed a cost saver if you come at it from that perspective. • Mozilla supports BIDI, the display and input of text in a bidirectional format for such languages as Arabic and Hebrew, yet the capabilities for this in the UI were not mature when we were writing this book. • The UI locale DTD files use UTF-8 as the default encoding for translated items. Mozilla then maps to Unicode or non-Unicode fonts, depending on which platform you're running on or what fonts you installed in your system. You are encouraged to encode your DTD files as UTF-8 when possible. Recalling the architecture of the XPFE toolkit described in Chapter 2, the locale component can be easily plugged in and out of the application that you are working on without impacting any other components. This functionality is ideal, for instance, for people with linguistic skills and less experience with technical issues to become involved in a Mozilla-related project.
  4. 11.1.1. For the Developer Many available resources show you how to help localize an existing application into a specific language or to find out how to add localization support to your own application. The Mozilla Localization Project hosts various localization teams and provides help whenever possible. The Mozilla community includes a discussion group that uses many languages to discuss Mozilla development issues. The netscape.public.mozilla.l10n and netscape.public.mozilla.i18n newsgroups are a great place to discuss these issues with other developers. When developing an application, some words and phrases that developers like to hear (according to the Mozilla organization, at are: standards compliant, simple, leveragable, portable, extensible, separable, consistent, dynamic, valid, parser friendly, invisible (part of the XUL authoring process), and efficient. The following sections will help you understand how these terms and goals impact the chosen technologies and how to use those technologies. The ultimate aim is to help you localize your application easily. 11.1.2. Files and File Formats Here are the main file types you'll see when learning about locale and that you will use when localizing your Mozilla application. A good home for all of these resources is in the locale area of the application chrome. DTD (.dtd) Files containing entities that host the strings from XUL content files. Property (.properties) or string bundles
  5. Files containing strings that are accessed by JavaScript, C++, and possibly other scripting or component files. RDF RDF files are described in XML syntax, so use entities. HTML and text Suitable for long text, HTML and XML documents and other content that needs to be localized. The next two sections will help you start localizing your application. The sections focus on DTD files and string bundles, which are the core formats for XUL-localizable content. Before getting started, here is a review of some general principles that might help you design and implement the locale component. 11.1.3. UI Aesthetics and Principles To put locale in context, this section looks at some issues you may encounter when localizing your Mozilla application. Some are universal principles and others are unique to the environment. This reference is by no means exhaustive, but it contains some scenarios and tips the authors came across in their experience with locale in Mozilla. Space management One of the guiding principles in UI design is for your interface to not get too crowded. Although estimates are not specific, it is wise to leave about 30 percent expansion space in your window and dialogs. To achieve this flexibility, you have to ensure that the XUL window has ample space in the first place for all the widgets to fit.
  6. More specifically, the application needs to have space for widgets to expand or contract without detracting from the overall look and feel. Intuitive use of the XUL box model (refer to Chapter 3 for more information) and correct choice of widgets goes a long way in achieving this goal. The factors that can cause this space to be filled include using languages/character sets that are more verbose than the one that was there originally, and the users changing their font size settings. Some safeguards that have been built into Mozilla already handle this problem. Much of it is done in CSS, but other methods are available. The section "Language Quirks," later in this chapter, outlines one of these methods. Help system If you choose to integrate a Help system into your application, a localizable resource will be most content. Opinions differ within technical writing circles, but having screenshots in your documents is generally not considered advantageous. For example, they can get out of date easily in the constantly evolving world of software, or they need to be retaken frequently when new features are added to the UI. Tooltips Tooltips are a sometimes overlooked yet valuable way of relaying information to the user. They can be used as an alternative to a help system if you are looking for something simpler. They can also expand an explanation of something that was annotated in the UI text. Sometimes text can have multiple meanings in context, and expanding it with a tooltip can clear up any confusion. In an editor or multifile browser, for example, you
  7. might have a find button. A tooltip can clear up the confusion about whether the results of the action searches in the current file or in all files. Most XUL widgets support tooltips. Implementation is as straightforward as adding a tooltip attribute to the widget with an associated value. For it to be localizable, it must be in the form of a DTD entity. The Section 11.2.1 section, later in this chapter, provides more information on the rationale for using entities and how to insert them into XUL content. Grammar In any user interface, there is limited screen space. When possible, however, provide complete or near-complete sentences. These sentences are better than using text based on phrases or acronyms. They provide meaning to the translator and clearer instructions to the user. Commenting Commenting was mentioned before, but is worth stressing again. The translators may have not even seen the software that you are working on, but you hope that is not the case! Commenting is very useful for giving context and flagging strings that should not be commented. You can comment your HTML, XML, or DTD files by wrapping it in a block.
  8. Note that a bundle file uses the # notation at the beginning of each line to signify a comment. # This text is used in the view menu for launching the page choices dialog pageChoices=Go To... Web resources Localizable resources are not only strings of text that need to be translated into different languages; they are any variable information that is liable to change over the lifetime of your application. The handling of URLs is a case in point. You may have references interspersed throughout your UI that point to web resources. These references can be explicit listings or widgets that, once activated, launch a client to bring you to a certain location. Images are another resource commonly used in documentation. A tutorial on your application may have screenshots of the UI in action. If you do use images, keep an eye out for localizable content in them. 11.2. DTD Entities Entities in XUL work the same way as they do in any other XML application. They are used to reference data that was abstracted from the content. This process encourages reuse of data, but in the context of Mozilla's XPFE, it is used to extract visible text in interface widgets. This extraction ensures that the content can remain untouched during the localization process. 11.2.1. Inserting Entities
  9. Example 11-1 shows how to put DTD entities into your XUL code by using attribute values for the text of a menu item (label) and the keyboard access shortcuts (accesskey). The syntax requires that an entity be placed in quotes as the value of the attribute. This is a useful example because it highlights the localization of a widget label, which is common to many widgets, and a supplementary attribute, which, in this case, is an accesskey. Example 11-1. XUL menu with entity references for text and accesskeys
  10. &explanation.text; Table 11-1 represents the DTD files that accompany the XUL content in Example 11-1. Two languages, English and Spanish, are separated into different files. These files have the same name as the DTD file referenced in the XUL file that contains the entities. However, each file for every different language exists in a separate locale folder. Each entry, or entity, in the DTD file has a name that matches the name referenced in the XUL and a value to be filled in for that entity. The value is enclosed in quotes. When generating these files, you will need to create the file only once and copy it to a different directory where you can replace the values in the entities. A good tool would carry out this process for you. Refer to the Localization Tools sidebar later in the chapter for more information. Table 11-1. Entity definitions for the XUL menu English DTD Spanish DTD "Archivo"> "Nuevo"> "Abrir Archivo..."> "Cerrar">
  11. English DTD Spanish DTD "Save"> "Salvar"> "Salvar Como..."> "Imprimir..."> "Salir"> menuFile.accesskey "a"> menuNew.accesskey "n"> menuOpen.accesskey "o"> menuClose.accesskey "c"> menuSave.accesskey "s"> menuSaveAs.accesskey "a"> menuPrint.accesskey "i">
  12. English DTD Spanish DTD menuExit.accesskey "x"> menuExit.accesskey "r"> Figure 11-1 shows the resulting XUL menus. There can only be one value for each entity and only one language taking precedence, or appearing in the UI, at a time. Figure 11-1. Localized menus in English and Spanish This example presents only two languages, but theoretically, you can have as many languages as you require. The locale-switching mechanism and the chrome registry must determine which one should be used, which is explained later in the section "The Chrome Registry and Locale." 11.2.2. External and Inline Entities You may ask, how are the entities accessed? You can associate the DTD with your XUL file in two ways. The first is internally, which involves wrapping the strings in a DTD data type enclosure by using the DOCTYPE declaration.
  13. ]> The second is an external DTD file, which is associated with your XUL that also uses the DOCTYPE declaration, and a reference pointing to the file: The node referenced in the DOCTYPE declaration is usually followed by the XUL document's root node. In this case, it is window, but can be other elements like page or dialog (however, it is not actually validated so it can be any value). If you have a small application, the DTD files can reside in the same folder as your XUL files, but putting them into their own locale directory within your chrome structure is good practice. Consider the main Editor window in Mozilla. Its declaration in Example 11- 2 is flexible enough to associate multiple DTD files with your content. Example 11-2. The Editor's Doctype definitions %editorOverlayDTD;
  14. %brandDTD; ]> The declaration first stores the document associated with the chrome URL in an associated parameter entity. It then simply uses it. XML does not have a one-step way of storing and using the entity as in other languages. In other words, the declaration is the equivalent of the import foo in Python, or #include "foo.h" in C. Certain localizable resources lend themselves to reuse. It makes sense to use the same strings across different content, which explains the inclusion of a DTD file in more than one XUL document. In Mozilla, this includes brand information, build ID numbers, and help resources. Which is more appropriate to use: internal or external entities? Using the external approach is preferable because the content (XUL) does not have to be touched during the translation process. If someone opts to create a tool to extract and/or insert strings, their job would be much easier if they had to parse one less file type. This may remove context somewhat, but it can be overcome by actively commenting the DTD file. 11.3. String Bundles String bundles are flat text files that contain text for the UI that is accessed in JavaScript, C++, and theoretically any language that fits within the Mozilla framework. These bundles are strings that can be presented visually to the user via some functionality in the application at any time. This may be anything from a dynamically changing menu item to an alert box, or from a URL to a placeholder that is filled depending on the context in which it is
  15. accessed. The bundle files are given an extension of .properties and they commonly reside in the locale directory with the DTD files. A user interface can use one or more string bundles, each of which is defined in a element and surrounded by a element. Example 11-3 contains the bundles used by the Mozilla browser. Example 11-3. String bundles used by the Mozilla browser
  16. src="chrome://global/locale/"/ >
Đồng bộ tài khoản