# python: Pattern di traduzione delle stringhe
URL: https://generaltranslation.com/it/docs/python/guides/strings.mdx
---
title: Pattern di traduzione delle stringhe
description: Traduzione delle stringhe in Python con t(), msg(), variabili e derive()
---
Esistono due modi per tradurre le stringhe nelle librerie Python di GT:
1. **Inline con `t()`** — traduci le stringhe direttamente negli handler delle route
2. **pre-registrate con `msg()`** — definisci le stringhe nell'ambito del modulo e risolvile a runtime
## Traduzione in linea con `t()`
Usa [`t()`](/docs/python/api/t) per tradurre una stringa nell'impostazione regionale della richiesta corrente:
```python
from gt_flask import t
@app.route('/api/greeting')
def greeting():
return { 'message': t('Hello, world!') }
```
```python
from gt_fastapi import t
@app.get('/api/greeting')
def greeting():
return { 'message': t('Hello, world!') }
```
## Stringhe pre-registrate con `msg()`
Usa [`msg()`](/docs/python/api/t) per registrare stringhe nell'ambito del modulo, quindi usale negli handler:
```python title="messages.py"
from gt_flask import msg # o gt_fastapi
GREETING = msg('Hello, world!')
WELCOME = msg('Welcome, {name}!')
NOT_FOUND = msg('Resource not found.')
```
```python title="routes.py"
from messages import GREETING, WELCOME, NOT_FOUND
@app.route('/api/greeting')
def greeting():
return {
'greeting': GREETING(),
'welcome': WELCOME(name='Alice'),
}
@app.errorhandler(404)
def not_found(e):
return { 'error': NOT_FOUND() }, 404
```
Sia `t()` che `msg()` producono traduzioni identiche. Usa `msg()` quando la stessa stringa compare in più punti o quando vuoi raccogliere tutte le stringhe in un unico file.
## Interpolazione di variabili
### Con `t()`
Usa i segnaposto `{name}` e passa i valori come argomenti con parola chiave:
```python
t('Welcome, {name}!', name=user.display_name)
t('You have {count} new messages.', count=unread_count)
```
### Con `declare_var()` e `decode_vars()`
Per avere un maggiore controllo sulla gestione delle variabili, usa [`declare_var()`](/docs/python/api/declare-var) e [`decode_vars()`](/docs/python/api/decode-vars):
```python
from gt_flask import declare_var, decode_vars
template = t('Hello, {name}! You have {count} items.')
result = decode_vars(template, {
'name': declare_var('name', 'Guest'),
'count': declare_var('count', 0),
})
```
## Uso di `derive()` per la concordanza
[`derive()`](/docs/python/api/derive) genera varianti grammaticalmente corrette ed è utile per le lingue in cui le parole cambiano forma in base al contesto (genere, numero, caso):
```python
from gt_flask import t, derive
count = 3
item_word = derive('item', {'one': 'item', 'other': 'items'})
message = t('You have {count} {item}.', count=count, item=item_word)
```
Consulta il [tutorial sulla derivazione](/docs/python/tutorials/derive) per altri pattern.
## Uso di `$context` per la disambiguazione
Le stringhe ambigue possono portare a traduzioni imprecise. Aggiungi `$context` per guidare il traduttore:
```python
t('Apple', context='the technology company')
t('Spring', context='the season, not a coil')
t('Bank', context='a financial institution')
# Funziona anche con msg()
APPLE = msg('Apple', context='the fruit')
```
## Passi successivi
* [`t()` riferimento API](/docs/python/api/t)
* [`declare_var()` riferimento API](/docs/python/api/declare-var)
* [`derive()` riferimento API](/docs/python/api/derive)
* [Rilevamento dell'impostazione regionale e middleware](/docs/python/guides/middleware) — come funziona il contesto dell'impostazione regionale