Metadata-Version: 2.4
Name: django-nublado-translation
Version: 0.2.1
Summary: A Django app for simple model-field translations.
Author: C Nublado
License: Copyright (c) Nublado and individual contributors.
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without modification,
        are permitted provided that the following conditions are met:
        
            1. Redistributions of source code must retain the above copyright notice,
               this list of conditions and the following disclaimer.
        
            2. Redistributions in binary form must reproduce the above copyright
               notice, this list of conditions and the following disclaimer in the
               documentation and/or other materials provided with the distribution.
        
            3. Neither the name of Django nor the names of its contributors may be used
               to endorse or promote products derived from this software without
               specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
        ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: django
Requires-Dist: django-nublado-core<0.5.0,>=0.4.2
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-django; extra == "test"
Requires-Dist: pytest-mock; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: pytest-html; extra == "test"
Requires-Dist: psycopg; extra == "test"
Dynamic: license-file


# django-nublado-translation

A minimal, explicit model-field translation system for Django.

Translations are implemented via a strict two-model contract:
- a source model
- a translation model

No GenericForeignKeys.  
No automatic field injection on the source model.  
Source model remains unchanged.

---

## Features

- Abstract base models:
  - `TranslationSourceModel`:
  - `TranslationModel`: 
- Manager:
  - `TranslationSourceManager`: Manager for `TranslationSourceManager`
- Guarantees:
  - One translation per `(source, language)`.
  - Source-model unique fields are unique **per language** in the translation model.
- Base-language fallback support (e.g., `es-ar` → `es`).

---

## Installation

```bash
pip install django-nublado-translation
```

```python
INSTALLED_APPS = [
    ...,
    "django_nublado_translation",
]
```

---

## Abstract models

### TranslationSourceModel

Base model for translatable objects.

- Translations are stored and cached in a language-code-indexed dictionary
`translations_dict` for convenience.

```python
from django.db import models

from django_nublado_translation.models import TranslationSourceModel
from django_nublado_translation.managers import TranslationSourceManager


class Article(TranslationSourceModel):
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique=True)
    content = models.TextField()

    translated_fields = [
        "title",
        "slug",
        "content",
    ]

    objects = TranslationSourceManager()
```

#### Notes

- `translated_fields` is **required** for translations, and is the single source of truth for translated fields.
- `TranslationSourceManager` is optional, but recommended, to take advantage of its translation-filtering capability.

---

### TranslationModel

Base model for translations of a `TranslationSourceModel` subclass.

- A foreign key to the source model is generated automatically.
- Default foreign key name: `source`.
- Default reverse relation name: `translations`.
- Fields are derived from `translated_fields` in related source model.
- Constraint naming is derived from the **translation model name**.

Subclasses must define:
- `source_model`
- No translated field declarations are allowed on the translation model.

```python
from django_nublado_translation.models import TranslationModel


class ArticleTranslation(TranslationModel):
    source_model = Article


# Source object
article = Article.objects.create(
    title="Hello everybody",
    slug="hello-everybody",
    content="Hello, everybody.",
)

# Translation object
ArticleTranslation.objects.create(
    source=article,
    language="es",
    title="Hola a todos",
    slug="hola-a-todos",
    content="Hola a todos.",
)
```

#### Notes

- `source_name` may be overridden **before migrations**.
- Translated fields are copied from `TranslationSourceModel.translated_fields`.

---

## Working with translations

### Using a subclass of `TranslationSource`

#### Getting translations:

```python
# Assume the default language is "en", available translation languages are
# "es" and "de", and the object has a translation for "es".

# Fetching a translation.
article.get_translation("es")
article.get_translation("es-ar")   # Falls back to "es" if no translation found for "es-ar".
article.get_current_translation()  # Uses active Django language
```

- Returns `None` if no translation exists.

#### Getting translation information:

```python
# Find out if an object has a translation in a given language.
article.has_translation("es") # True
article.has_translation("de") # False

# Get translation-languages object doesn't have a translation for.
article.get_available_translation_languages() # ["de"]
```

#### Cache management for translation_dict.

```python
article.clear_translations_dict_cache()
```

- Invalidates the internal translation cache.
- Required when translations are modified outside the instance lifecycle.

---

### Using `TranslationSourceManager`

#### Prefetching translations

```python
Article.objects.prefetch_translations()

Article.objects.prefetch_translations().filter(slug="source-article-slug")

# Prefetch filtered translations.
translation_qs = ArticleTranslation.objects.filter(language="es", published_status="published")

Article.objects.prefetch_translations(queryset=translation_qs)
```

---

## App settings

```python
from django_nublado_translation.conf.app_settings import app_settings

app_settings.SOURCE_LANGUAGE
```

### Available settings

| Setting | Default |
|-------|---------|
| `SOURCE_LANGUAGE` | `django.conf.settings.LANGUAGE_CODE` |

### Override

```python
DJANGO_NUBLADO_TRANSLATION = {
    "SOURCE_LANGUAGE": "en",
}
```

---

## Testing

```bash
pytest
```

Requires `pytest-django`.
