Better i18n with gettext: use class-based API
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- cli, internationalization, localization
Direzione di ricerca
Inizia individuando gli usi attuali di from gettext import _ in Click e i relativi percorsi di inizializzazione di comandi e gruppi. Esamina quindi le API gettext classica e basata su classi di Python, poi definisci la copertura della compatibilità per la vecchia API e per l’integrazione proposta di click.i18n. Il lavoro è completato quando Click non modifica più il dominio globale di gettext durante l’importazione e i consumer possono separare i domini di traduzione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hi lovely Click maintainers,
Currently, Click implements gettext using the classic GNU gettext API. That looks like this:
from gettext import _
print(_("Hello, world!"))
This API depends on a global state in the gettext module. By calling gettext.textdomain(), the active translation domain is changed for all Python modules that use the classic GNU gettext API.
This side effect is usually desirable, except when your module is imported by another module as a library. So you usually don't want to call gettext.textdomain() without putting it behind some sort of function call. With argparse, this is easy: put it in your main function before you even create the ArgumentParser object. With Click, I'm not sure this is possible:
- Your main group/command is decorated with all manner of Click magic and its contents may not actually be run (e.g.
--help). - There is no way to pre-hook a group/command with a function call that I am aware of.
So you end up having to call gettext.textdomain() on import of your module containing your Click groups/commands.
We can fix that by switching to the class-based API. Because Click will still need to support the old API as well for backwards compatibility, my proposal looks a little as follows. Create a module click.i18n with the following contents (simplified):
import gettext as _gettext_module
TRANSLATIONS = None
def gettext(message):
if TRANSLATIONS is None:
return _gettext_module.gettext(message)
return TRANSLATIONS.gettext(message)
# alias
_ = gettext
Now, elsewhere in Click, you replace all from gettext import _ with from .i18n import _.
Subsequently, we can create a function install_translations(translations) in i18n.py that replaces the TRANSLATIONS global constant with an instantiated GNUTranslations object. This function would still need to be called before the consumer's main function, but it wouldn't change the gettext global state—it would only change Click's. Which, as far perfectionism goes, is probably tolerable. It would be better still if there was a pre-hook, but this is fine.
Furthermore, the consumer could use different domains for Click's TRANSLATIONS object and their own, allowing them to separate their own translations from Click's, and hypothetically reuse the Click translations in other projects.
In fact, having done this plumbing, Click could even ship its own translation strings, getting rid of duplication efforts of translating the same Click strings. Click's own translations could then be activated using e.g. install_click_translations() without any arguments.
In summary, the problems solved by this:
- Importing the Click consumer's module no longer changes the global state of the gettext module.
- Users can separate their own module's translations from Click's.
- Click would be enabled to provide its own translations to reduce duplication of efforts.
I am not aware of other ways to achieve the above that do not require changes to Click. Adding a pre-hook to groups/commands might partially address the problem.
I am willing to make a PR if this issue is validated.
I wrote a blog post here that provides more context on how I use gettext + Click (+ some other components). It has more context than is necessary to understand this issue.
- Lingua principale
- Python
- Stelle
- 17.8k
- Fork
- 3.3k
- Merge medio
- 3g 3h
- PR unite (30g)
- 13
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pallets/click
-
`show_envvar=True` Drops Environment Variable from Error Messages When Using `auto_envvar_prefix`Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno
-
More private utilities that need to be marked and public api deprecatedForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertatyping
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
pallets/click#3847 · 9 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Automatically append ellipsis (`...`) to metavars when `multiple=True` in optionsForse già presa @SparshGarg999 l’ha presa 89 giorni fa. Apertahelp output
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pallets/click#3652 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
`HelpFormatter.write_usage` breaks options at a hyphenForse già presa @kdeldycke l’ha presa 9 giorni fa. Apertahelp output
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
pallets/click#3362 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Add glossary of terms to click reference docsForse già presa @rohitbarhate111-commits l’ha presa 55 giorni fa. Apertadocs
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
pallets/click#3077 · 5 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di pallets/click
Issue simili
-
needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
gke-labs/kube-agents#5671 ·
I maintainer di solito rispondono entro 1 giorno
-
docs(auth): Method B step 4 should tell users to tick "Use this client for an AI-powered agent"Apertadocumentation next-release v0.9.6
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
khuisman/mcp-gee-sweet#1446 ·
I maintainer di solito rispondono entro 1 giorno
-
In-box secret files written before their chmod: lc-creds, stacks tee, and the workspace tokenApertamodel:sonnet runtime:claude security size:S
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
FootprintAI/Containarium#3671 ·
I maintainer di solito rispondono entro 1 giorno
-
HolidayBase: reassigning `language` does not re-initialize translations or re-populate holidaysForse già presa @shikhabhadoria l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
vacanza/holidays#4116 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug CLI exec
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno