plugin-addgeneratedtext.dita 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "reference.dtd">
  3. <!-- This file is part of the DITA Open Toolkit project. See the accompanying LICENSE file for applicable license. -->
  4. <reference id="plugin-addgeneratedtext" xml:lang="en-US">
  5. <title>Customizing generated text</title>
  6. <shortdesc>Generated text is the term for strings that are automatically added by the build, such as "Note" before the
  7. contents of a <xmlelement>note</xmlelement> element.</shortdesc>
  8. <refbody>
  9. <section>
  10. <p>The generated text extension point is used to add new strings to the default set of generated text. There are
  11. several reasons you may want to use this:
  12. <ul>
  13. <li>It can be used to add new text for your own processing extensions; for example, it could be used to add
  14. localized versions of the string "User response" to aid in rendering troubleshooting information.</li>
  15. <li>It can be used to override the default strings in the toolkit; for example, it could be used to reset the
  16. English string "Figure" to "Fig".</li>
  17. <li>It can be used to add support for new languages (for non-PDF transforms only; PDF requires more
  18. complicated localization support). For example, it could be used to add support for Vietnamese or Gaelic; it
  19. could also be used to support a new variant of a previously supported language, such as Australian
  20. English.</li>
  21. </ul></p>
  22. <dl>
  23. <dlentry>
  24. <dt><codeph>dita.xsl.strings</codeph></dt>
  25. <dd>Add new strings to generated text file. </dd>
  26. </dlentry>
  27. </dl>
  28. </section>
  29. <example>
  30. <title>Example: adding new strings</title>
  31. <p>First copy the file <filepath>xsl/common/strings.xml</filepath> to your plug-in, and edit it to contain the
  32. languages that you are providing translations for ("en-US" must be present). For this sample, copy the file into
  33. your plug-in as <filepath>xsl/my-new-strings.xml</filepath>. The new strings file will look something like
  34. this:</p><codeblock>&lt;?xml version="1.0" encoding="utf-8"?>
  35. &lt;!-- Provide strings for my plug-in; this plug-in supports
  36. English, Icelandic, and Russian. -->
  37. &lt;langlist>
  38. &lt;lang xml:lang="en" filename="mystring-en-us.xml"/>
  39. &lt;lang xml:lang="en-US" filename="mystring-en-us.xml"/>
  40. &lt;lang xml:lang="is" filename="mystring-is-is.xml"/>
  41. &lt;lang xml:lang="is-IS" filename="mystring-is-is.xml"/>
  42. &lt;lang xml:lang="ru" filename="mystring-ru-ru.xml"/>
  43. &lt;lang xml:lang="ru-RU" filename="mystring-ru-ru.xml"/>
  44. &lt;/langlist></codeblock>
  45. <p>Next, copy the file <filepath>xsl/common/strings-en-us.xml</filepath> to your plug-in, and replace the content
  46. with your own strings (be sure to give them unique name attributes). Do the same for each language that you are
  47. providing a translation for. For example, the file <filepath>mystring-en-us.xml</filepath> might contain:</p><codeblock>&lt;?xml version="1.0" encoding="utf-8"?>
  48. &lt;strings xml:lang="en-US">
  49. &lt;str name="String1">English generated text&lt;/str>
  50. &lt;str name="Another String">Another String in English&lt;/str>
  51. &lt;/strings></codeblock>
  52. <p>Use the following extension code to include your strings in the set of generated text: </p><codeblock>&lt;plugin id="com.example.strings">
  53. &lt;feature extension="dita.xsl.strings" file="xsl/my-new-strings.xml"/>
  54. &lt;/plugin></codeblock>
  55. <p>The string is now available to the "getVariable" template used in many DITA-OT XSLT files.
  56. For example, if processing in a context where the xml:lang value is "en-US", the following
  57. call would return "Another String in English":</p><codeblock>&lt;xsl:call-template name="getVariable">
  58. &lt;xsl:with-param name="id" select="'Another String'"/>
  59. &lt;/xsl:call-template>
  60. </codeblock>
  61. <note>If two plug-ins define the same string, the results will be non-deterministic, so multiple plug-ins should
  62. not try to create the same generated text string. One common way to avoid this problem is to ensure the name
  63. attributes used to look up the string value are related to the ID or purpose of your plug-in.</note>
  64. </example>
  65. <example>
  66. <title>Example: modifying existing strings</title>
  67. <p>The process for modifying existing generated text is exactly the same as for adding new text, except that the
  68. strings you provide override values that already exist. To begin, set up the
  69. <filepath>xsl/my-new-strings.xml</filepath> file in your plug-in as in the previous example. </p>
  70. <p>Next, copy the file <filepath>xsl/common/strings-en-us.xml</filepath> to your plug-in, and choose the strings
  71. you wish to change (be sure to leave the name attribute unchanged, because this is the key used to look up the
  72. string). Create a strings file for each language that needs to modify existing strings. For example, the new
  73. file <filepath>mystring-en-us.xml</filepath> might contain:</p><codeblock>&lt;?xml version="1.0" encoding="utf-8"?>
  74. &lt;strings xml:lang="en-US">
  75. &lt;str name="Figure">Fig&lt;/str>
  76. &lt;str name="Draft comment">ADDRESS THIS DRAFT COMMENT&lt;/str>
  77. &lt;/strings></codeblock>
  78. <p>To include the new strings, use the same method as above to add these strings to your
  79. <filepath>plugin.xml</filepath> file. Once this plug-in is installed, where XHTML output previously generated
  80. the term "Figure", it will now generate "Fig"; where it previously generated "Draft comment", it will now
  81. generate "ADDRESS THIS DRAFT COMMENT". The same strings in other languages will not be modified unless you also
  82. provide new versions for those languages.</p><note>If two plug-ins override the same string in the same
  83. language, the results will be non-deterministic (either string may be used under different conditions). Multiple
  84. plug-ins should not override the same generated text string for a single language.</note>
  85. </example>
  86. <example>
  87. <title>Example: adding a new language</title>
  88. <p>The process for adding a new language is exactly the same as for adding new text, except you are effectively
  89. just translating an existing strings file. To begin, set up the <filepath>xsl/my-new-strings.xml</filepath> file
  90. in your plug-in as in the previous examples. In this case, the only difference is that you are adding a mapping
  91. to new languages; for example, the following file would be used to set up support for
  92. Vietnamese:<codeblock>&lt;?xml version="1.0" encoding="utf-8"?>
  93. &lt;!-- Map languages with xml:lang="vi" or xml:lang="vi-vn"
  94. to the translations in this plug-in. -->
  95. &lt;langlist>
  96. &lt;lang xml:lang="vi" filename="strings-vi.xml"/>
  97. &lt;lang xml:lang="vi-VN" filename="strings-vi.xml"/>
  98. &lt;/langlist></codeblock></p>
  99. <p>Next, copy the file <filepath>xsl/common/strings-en-us.xml</filepath> to your plug-in, and rename it to match
  100. the language you wish to add. For example, to support Vietnamese strings you may want to pick a name like
  101. <filepath>strings-vi.xml</filepath>. In that file, change the <xmlatt>xml:lang</xmlatt> attribute on the root
  102. element to match your new language.</p>
  103. <p>Once the file is ready, translate the contents of each <xmlelement>str</xmlelement> element (be sure to leave
  104. the name attribute unchanged). Repeat this process for each new language you wish to add.</p>
  105. <p>To include the new languages, use the same method as above to add these strings to your
  106. <filepath>plugin.xml</filepath> file. Once this plug-in is installed, non-PDF builds will include support for
  107. Vietnamese; instead of generating the English word "Caution", the element <codeph>&lt;note type="caution"
  108. xml:lang="vi"></codeph> may generate something like "<term xml:lang="vi">chú ý</term>".</p><note>If two
  109. plug-ins add support for the same language using different values, the results will be non-deterministic
  110. (translations from either plug-in may be picked up under different conditions).</note>
  111. </example>
  112. </refbody>
  113. </reference>