Metadata-Version: 2.4
Name: django-nublado-translation
Version: 0.1.0
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.4.0,>=0.3.1
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 simple, reliable model-field translation system for Django projects.**

Lightweight, focused, no bloat: translate model fields via a two-model contract (source model + translation model) without the tangles of GenericForeignKeys or automatic field injection. Source models stay fully intact.

## Features

- Abstract base models for translation:  
  - `TranslationSourceModel` — for models that can be translated. 
  - `TranslationModel` — for the translations of a source model. 
- Translation managers:  
  - `TranslationSourceManager` — helper methods for working with translations. 
- Guarantees:  
  - A source object can have at most one translation per language. 
  - Unique fields on the source model are unique per language in the translation model.  

## Installation

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

Add to `INSTALLED_APPS`:

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

## Abstract models

### `TranslationSourceModel`

An abstract base model for objects that can be translated.


```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()
    
    objects = TranslationSourceManager()

```

#### Notes

- Using `TranslationSourceManager` is optional but recommended. You can subclass it for app-specific translation requirements.

### `TranslationModel`

An abstract base model for the translations of a `TranslationSourceModel` subclass.
- A foreign key to the source model is generated automatically.
- The foreign key name (default `"source"`) and the reverse relation name (default `"translations"`) can be customized via
the `source_name` and `translations_name` attributes.

Subclasses must define:
- source_model: a subclass of TranslationSourceModel
- translation_fields: a list of fields from the source model to be translated.

```python
from django_nublado_translation.models import TranslationModel

class ArticleTranslation(TranslationModel):
    source_model = Article
    translation_fields = [
        "title",
        "slug",
        "content",
    ]

# Example usage
article  = Article(
    title="Hello everybody",
    slug="hello-everybody", 
    content="Hello, everybody."
)
article.save()

article_translation_es = ArticleTranslation(
    source=article,
    language="es",
    title="Hola a todos",
    slug="hola-a-todos",
    content="Hola a todos.",
)
article_translation_es.save()

```

#### Notes

- If `source_name` or `translations_name` is overridden, it must occur **before migration** for it to take effect.


## Working with translations

### From the source model:

```python
article.get_translation("es")
article.get_current_translation() # Uses currently active Django language.
```

### Using `TranslationSourceManager`

```python
# Prefetch all translations.
Article.objects.prefetch_translations()

# prefetch_translations() returns a QuerySet.
Article.objects.prefetch_translations().filter(slug="source-article-slug")

# Filter translations.
translation_qs =  ArticleTranslation.objects.filter(language="es")
Article.objects.prefetch_translations(queryset=translation_qs)
```

#### Notes

- The translation queryset can be freely customized.
- This is especially useful when subclassing managers with specific translation filters.

## App settings

Access app settings via:

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

# Example usage
print(app_settings.SOURCE_LANGUAGE)
```

### Available settings and default values:
- `SOURCE_LANGUAGE`: defaults to `django.config.settings.LANGUAGE_CODE`.

### Overriding settings

In your project's `settings.py`, define a dictionary named `DJANGO_NUBLADO_TRANSLATION`.

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

## Testing

```bash
pytest
```

#### Notes:
- Runs all tests for the app.
- Requires `pytest-django`.
