TIP 687: locale support for word handling in text and entry

Login
Author:		Jan Nijtmans <[email protected]>
State:		Final
Type:		Project
Vote:		Done
Created:	30-1-2024
Tcl-Version:	9.1
Tk-Branch:	tip-687
Forum:          d7d9b9edd8
Vote-Summary:	Accepted 9/0/0
Votes-For:	AN, HO, JN, KW, MC, SL
Votes-Against:	none
Votes-Present:	none

Abstract

This TIP proposes to add the -locale option to the text and entry widget and it's derivatives (so ttk and spinbox as well). This option can be used to change the algorithm used in virtual events <<NextChar>>, <<PrevChar>>, <<NextWord>>, <<PrevWord>> (and it's variants like <<SelectNextChar>>). The -locale will be handed to the ICU library, so it can adapt the char- or word-division algorithm to the specific locale.

If the -locale of the widgets is set to "C" (the default), the ICU default locale is used. A special value tcl8 can be used to fall-back to the algorithm used in Tcl 8.x.

Rationale

This TIP started with a ticket, requesting to make the word break algorithm switchable: The ICU method of char-/word-division is designed for natural languages, less for programming languages.

The difference can be noted - for example - in the word aujourd'hui. With the ICU algorithm, this is a single word. Using the tcl8 locale, it's split into two parts.

The locale string consists of possibly 4 parts, in the form aaa_Bbbb_cc@dddd.

  1. a two- or three-character lowercase language code (ISO 639)

  2. a 4-character camel-case script code, like "Cyrl" or "Latn".

  3. a two-character uppercase or 3-digit numerical region code (ISO 3166).

  4. an optional variant, like @latin, @euro, @adlam, or @cjknarrow. Cannot be combined with a 4-character script code.

Before the 4) variant, an encoding indicator can be specified preceded with a dot .. It will simply be ignored, but doesn't lead to a syntax error.

There are also two special locales C and POSIX, which are synonimes for en_US, and guaranteed to be present. Not all systems support all language-, country- and variant- codes.

This syntax is a superset of the syntax used by the msgcat::mclocale command, noting that msgcat::mclocale always returns a lowercase string. Since locales are case-insensitive, the result of msgcat::mclocale can be used directly as value for the -locale option. Locales also accept a minus-sign in stead of an underscore.

The -locale option doesn't give an error-message if the locale is not available on this system, it just falls-back to the closest existing locale. If nothing is found, the fallback will be to the en_US locale. It will give an error-message if the syntax doesn't match the above described syntax.

The best way to check what locales are present on your system, use the locale -a command on your system (if it is UNIX-like). Strip off the encoding indicators (like .utf8), and this will give you the list of supported locales.

Implementation

The text widget and the entry widget (and it's derivatives, ttk::entry and spinbox) will get a new -locale option, which can be set to any valid locale value.

Text tags have the new -locale option too, which is '' by default. There's also a new method [$text locale index] which can be used to determine which locale is set for a specific text index. If the text tag -locale value is set to the empty string, the value of the text widget's -locale will be used.

Implementation is in Tk branch "tip-687".

Copyright

This document has been placed in the public domain.