Development/Tutorials/Localization/Unicode (de): Difference between revisions

    From KDE TechBase
    (Some translation)
    (Some more translation)
    Line 25: Line 25:


    == UTF-8 und UTF-16 ==
    == UTF-8 und UTF-16 ==
    UTF steht für "Unicode Transformation Format". Die beiden am häufigsten verwendeten Varianten, UTF-8 und UTF-16, definieren, wie der einem Unicode-Zeichen zugeordnete Codenummer in Bits ausgedrückt wird.


    UTF stands for "Unicode Transformation Format". The two most commonly used variants, UTF-8 and UTF-16, define how to express a Unicode character's assigned codepoint in bits.
    In UTF-16 wird jedes Zeichen durch einen 16-bit Wert seiner Unicode-Nummer dargestellt. So ist zum Beispiel in UTF-16 <tt>b</tt> (LATIN SMALL LETTER B) repräsentiert als 0x0062. Es gibt auch Ersatzpaare um Werte über 0xFFFF zu codieren.
     
    In UTF-8 werden die Unicode Zeichen in einem Strom von Bytes in einem bestimmten Verschlüsselungsschema dargestellt, welches in [http://www.cl.cam.ac.uk/~mgk25/ucs/utf-8-history.txt] und [http://tools.ietf.org/html/rfc3629] beschrieben wird. Anders als UTF-16 ist es damit kompatibel zu ASCII. Höhere Codenummern benötigen zwei oder drei Bytes um verschlüsselt zu werden, selten mehr.
    UTF-16 signifies that every character is represented by the 16-bit value of its Unicode number. For example, in UTF-16 <tt>b</tt> (LATIN SMALL LETTER B) has a hex representation of 0x0062. There are also surrogate pairs used to encode characters with codepoints beyond 0xFFFF.
     
    UTF-8 signifies that the Unicode characters are represented by a stream of bytes in a special encoding scheme described in [http://www.cl.cam.ac.uk/~mgk25/ucs/utf-8-history.txt] and [http://tools.ietf.org/html/rfc3629]. Unlike UTF-16, it is compatible to ASCII. Higher codepoints take two or three bytes to encode, rarely more than that.


    == Unicode in KDE Applikationen ==
    == Unicode in KDE Applikationen ==

    Revision as of 19:06, 20 December 2007


    Development/Tutorials/Localization/Unicode


    Einführung in Unicode
    Anleitungsserie   Lokalisation
    Voriges Kapitel   None
    Nächstes Kapitel   Beim Applikationen schreiben an die Lokalisation denken
    Weiterführende Texte   The Unicode website
    Unicode article at Wikipedia
    Unicode for Unix/Linux FAQ
    Navigation   Deutsche Startseite

    Zusammenfassung

    Unicode ist ein Standard der für jedes Zeichen und Symbol in verschiedenen Schreibsystemen rund um die Welt eine eindeutige Nummer zur Verfügung stellt. Dadurch wird die Übersetzung von Software und Texten vereinfacht. Diese Anleitung bietet eine kurze Einführung in Unicode und wie man mit Unicode-Daten unter Qt/KDE arbeitet.

    Was ist Unicode?

    Unicode ist hardware-, entwicklungsplatform und sprachunabhängig. In der Version 5 gibt es Codenummern bis zu 0x10FFFF (doch nicht alle werden benutzt), die ersten 256 Einträge stimmen mit ISO 8859-1 a.k.a. ISO Latin-1 überein. Das bedeutet auch das die ersten 128 Einträge mit dem bekannten ASCII übereinstimmen. Codenummern wurden zugeordnet, um die Implementtion von bereits bestehenden Zeichensätzen zu vereinfachen, daher ist Unicode teilweise mit doppelten Zeichen belegt, damit die Übersetzungstabellen und -logiken kurz gehalten werden können.

    Warum Unicode?

    Benutzt man einen bestehenden Zeichensatz wie ISO 8859-15 ist es schwer, Dokumente zu erzeugen, die mehrere Sprachen beninhalten. Diese Dokumente können nur mit Personen ausgetauscht werden, die den gleichen Zeichensatz benutzen. Und auch kann haben mehrere Schreibsysteme mehr Zeichen als durch eine 8-bit Darstellung eindeutig zugewiesen werden können.

    Aus diesem Grund ist Unicode die beste der möglichen Wege verschiedene Sprachen und Zeichen in einerm Dokument zu vermischen und diese Dokumente mit Personen auszutauschen, die unterschiedliche Sprachen benutzen. Unicode macht es zum Beispiel möglich so etwas wie ein Russisch-Ungarisches Wörterbuch zu schreiben.

    UTF-8 und UTF-16

    UTF steht für "Unicode Transformation Format". Die beiden am häufigsten verwendeten Varianten, UTF-8 und UTF-16, definieren, wie der einem Unicode-Zeichen zugeordnete Codenummer in Bits ausgedrückt wird.

    In UTF-16 wird jedes Zeichen durch einen 16-bit Wert seiner Unicode-Nummer dargestellt. So ist zum Beispiel in UTF-16 b (LATIN SMALL LETTER B) repräsentiert als 0x0062. Es gibt auch Ersatzpaare um Werte über 0xFFFF zu codieren. In UTF-8 werden die Unicode Zeichen in einem Strom von Bytes in einem bestimmten Verschlüsselungsschema dargestellt, welches in [1] und [2] beschrieben wird. Anders als UTF-16 ist es damit kompatibel zu ASCII. Höhere Codenummern benötigen zwei oder drei Bytes um verschlüsselt zu werden, selten mehr.

    Unicode in KDE Applikationen

    Storing Unicode strings in memory and displaying Unicode on screen is very easy with Qt: QString and QChar provide full Unicode support transparently to your application.

    QString and QChar both internally represent characters using UTF-16. If you read in text pieces from or write it to an 8-bit source such as QTextStream you must use a QTextCodec to convert the text between representations. Normally you would use the UTF-8 codec to convert a Unicode string between a QByteArray and a QString.

    Getting an appropriate Unicode QTextCodec is quite simple:

    QTextCodec Utf8Codec = QTextCodec::codecForName("utf-8"); QTextCodec Utf16Codec = QTextCodec::codecForName("utf-16");

    Anzeige

    Any widget that uses QString or QChar when painting text will therefore be able to show Unicode characters as long as the user has an appropriate font available.

    For flexible text editing and display, consult the Qt documentation on rich text processing. The Scribe system provides a set of classes centered around the QTextDocument class that makes it easy to accurately display and print formated text, including Unicode.

    In-Memory Puffer

    To read Unicode data from an in-memory buffer, such as a QByteArray, one can use the codecs created earlier in this section:

    QByteArray buffer;

    // UTF-16 QString string = Utf16Codec->toUnicode(buffer);

    // UTF-8 QString string = Utf8Codec->toUnicode(buffer);

    Writing to a buffer looks very similar:

    QString string; // my Unicode text QByteArray array; // Buffer to store UTF-16 Unicode QTextStream textStream(array, IO_WriteOnly);

    textStream.setEncoding(QTextStream::Unicode); textStream << string;

    Dateibehandlung

    For storage and retrieval to and from a file KDE applications should provide the option to store text data in Unicode instead of the locale character set. This way the user of your application can read and save documents with the locale charset as the default while having the option to store it in Unicode. Both UTF-8 and UTF-16 should be supported. European users will generally prefer UTF-8 while Asian users may prefer UTF-16.

    Reading Unicode data from a text file is demonstrated in the following example using the QTextCodecs we created earlier for UTF-8 data:

    QTextStream textStream; QString line;

    // UTF-16, if the file begins with a Unicode mark // the default is ok, otherwise: // textStream.setEncoding(QTextStream::Unicode); line = textStream.readLine();

    // UTF-8 textStream.setCodec(Utf8Codec); line = textStream.readLine();

    Writing is equally straight-forward, also using the QTextCodecs we created earlier:

    QTextStream textStream; QString line;

    // UTF-16 textStream.setEncoding(QTextStream::Unicode); textStream << line;

    // UTF-8 textStream.setCodec(Utf8Codec); textStream << line;

    Unicode Zeicheneingabe

    Being able to load, store and display Unicode is only part of the puzzle. One of the final pieces is allowing the user to input Unicode characters. This is generally solved using platform-specific multibyte input methods (IM), such as XIM on X11. KDE provides good support for these input methods via Qt and your application should not need to do anything special to enable such input.

    Einfache Unicode Dateien und Schrifttypen

    An easy way to get Unicode sample data is to take the kde-i18n package and convert some files from there to Unicode using the recode command line tool:

    recode KOI8-R..UTF-8 ru/messages/kdelibs.po recode ISO-8859-1..UTF-16 de/messages/kdelibs.po recode ISO-8859-3..UTF-8 eo/messages/kdelibs.po

    The majority of operating systems today come packaged with Unicode TrueType fonts, see [3].