django-sage-language


Namedjango-sage-language JSON
Version 0.1.2 PyPI version JSON
download
home_pageNone
SummaryA Django Project for changing language.
upload_time2024-12-19 04:47:31
maintainerNone
docs_urlNone
authorMohmdFo
requires_python<4.0,>=3.10
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Sage Language Middleware for Django

This Django project provides middleware and utility classes to support multilingual applications through URL prefixes and cookies for language settings. The core feature is a custom `CookieLocaleMiddleware`, which dynamically manages user language preferences and redirects users to the appropriate language version of the site based on URL paths or cookies.

## Features

- **Dynamic Language Detection**: Determines the preferred language by examining URL prefixes and cookies.
- **Custom Middleware**: `CookieLocaleMiddleware` extends Django's default `LocaleMiddleware`, enhancing support for multilingual content.
- **URL Prefix Management**: Utilities to add or remove language prefixes in URLs, supporting a clean and consistent URL structure.
- **Customizable Language Settings**: Integrates with Django's internationalization settings (`LANGUAGES`, `LANGUAGE_CODE`, etc.) for easy customization.
- **Error Checking**: Ensures proper configuration of language settings and middleware placement.

## Project Structure

- `middlewares/cookie.py`: Contains `CookieLocaleMiddleware` to manage language preferences dynamically.
- `utils/locale.py`: Provides `MultilingualService`, a utility class for adding and removing language prefixes in URLs.
- `views/locale.py`: Implements `SetLanguageView`, allowing users to set their preferred language through a POST request.
- `checks.py`: Validates the configuration of middleware, language settings, and cookies.
- `settings.py`: Configures Django's settings, including `LANGUAGES`, `LANGUAGE_CODE`, and middleware setup.
- `urls.py`: Configures routes for language switching and i18n patterns.

## Installation

### Using pip

1. **Create a Virtual Environment**:
    ```bash
    python -m venv .venv
    ```

2. **Activate the Virtual Environment**:
    - On Windows:
        ```bash
        .venv\Scripts\activate
        ```
    - On macOS and Linux:
        ```bash
        source .venv/bin/activate
        ```

3. **Install the Package**:
    ```bash
    pip install python-sage-bbb
    ```

### Using Poetry

1. **Install Poetry**: Follow the official installation instructions at the [Poetry website](https://python-poetry.org/docs/#installation).

2. **Create a New Project (Optional)**:
    ```bash
    poetry new myproject
    cd myproject
    ```

3. **Add the Package as a Dependency**:
    ```bash
    poetry add python-sage-bbb
    ```

4. **Activate the Virtual Environment**:
    ```bash
    poetry shell
    ```

## Configuration

1. **Add `sage_language` to `INSTALLED_APPS`** in `settings.py`:
   ```python
   INSTALLED_APPS = [
       ...
       'sage_language',
   ]
   ```

2. **Middleware Configuration**:
   Ensure `CookieLocaleMiddleware` is added immediately after `SessionMiddleware` in the `MIDDLEWARE` setting:
   ```python
   MIDDLEWARE = [
       'django.middleware.security.SecurityMiddleware',
       'django.contrib.sessions.middleware.SessionMiddleware',
       'sage_language.middlewares.cookie.CookieLocaleMiddleware',
       ...
   ]
   ```

3. **Set Language Settings**:
   Define supported languages, default language, and cookie names:
   ```python
   LANGUAGES = [
       ('en', 'English'),
       ('fa', 'Farsi'),
       ('es', 'Spanish'),
   ]
   LANGUAGE_CODE = 'en'
   SAGE_LANGUAGE_COOKIE_NAME = "ivan_language"
   LANGUAGE_COOKIE_NAME = SAGE_LANGUAGE_COOKIE_NAME
   ```

4. **Add URL Configuration for Language Switching**:
   In `urls.py`, set up the language switching view:
   ```python
   from sage_language.views import SetLanguageView
   urlpatterns = [
       path('set-language/', SetLanguageView.as_view(), name='set_language'),
       path("i18n/", include("django.conf.urls.i18n")),
   ]
   ```

## Usage

1. **Setting Language Preference**:
   To change the language preference, make a POST request to `/set-language/` with `language` and `next` parameters:
   - `language`: The desired language code (e.g., 'en', 'fa', 'es').
   - `next`: The URL to redirect to after setting the language.

2. **URL Prefix Management**:
   The middleware automatically adds or removes language prefixes from URLs based on the user’s selected language and cookie settings.

3. **Automatic Language Redirection**:
   If a user’s preferred language (stored in a cookie) differs from the language in the URL, the middleware will redirect them to the correct URL.

## Notes

- Make sure to set `USE_I18N`, `USE_L10N`, and `USE_TZ` to `True` in `settings.py` for proper internationalization support.
- For production use, ensure `SAGE_LANGUAGE_COOKIE_NAME` and `LANGUAGE_COOKIE_NAME` are set to the same value for consistent cookie management.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-sage-language",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "MohmdFo",
    "author_email": "mohammad.fotouhi80@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c4/e4/ea48e2b7c303593f297742f2c0fd7da358a47768a42a19a9d9a2f50a935d/django_sage_language-0.1.2.tar.gz",
    "platform": null,
    "description": "# Sage Language Middleware for Django\n\nThis Django project provides middleware and utility classes to support multilingual applications through URL prefixes and cookies for language settings. The core feature is a custom `CookieLocaleMiddleware`, which dynamically manages user language preferences and redirects users to the appropriate language version of the site based on URL paths or cookies.\n\n## Features\n\n- **Dynamic Language Detection**: Determines the preferred language by examining URL prefixes and cookies.\n- **Custom Middleware**: `CookieLocaleMiddleware` extends Django's default `LocaleMiddleware`, enhancing support for multilingual content.\n- **URL Prefix Management**: Utilities to add or remove language prefixes in URLs, supporting a clean and consistent URL structure.\n- **Customizable Language Settings**: Integrates with Django's internationalization settings (`LANGUAGES`, `LANGUAGE_CODE`, etc.) for easy customization.\n- **Error Checking**: Ensures proper configuration of language settings and middleware placement.\n\n## Project Structure\n\n- `middlewares/cookie.py`: Contains `CookieLocaleMiddleware` to manage language preferences dynamically.\n- `utils/locale.py`: Provides `MultilingualService`, a utility class for adding and removing language prefixes in URLs.\n- `views/locale.py`: Implements `SetLanguageView`, allowing users to set their preferred language through a POST request.\n- `checks.py`: Validates the configuration of middleware, language settings, and cookies.\n- `settings.py`: Configures Django's settings, including `LANGUAGES`, `LANGUAGE_CODE`, and middleware setup.\n- `urls.py`: Configures routes for language switching and i18n patterns.\n\n## Installation\n\n### Using pip\n\n1. **Create a Virtual Environment**:\n    ```bash\n    python -m venv .venv\n    ```\n\n2. **Activate the Virtual Environment**:\n    - On Windows:\n        ```bash\n        .venv\\Scripts\\activate\n        ```\n    - On macOS and Linux:\n        ```bash\n        source .venv/bin/activate\n        ```\n\n3. **Install the Package**:\n    ```bash\n    pip install python-sage-bbb\n    ```\n\n### Using Poetry\n\n1. **Install Poetry**: Follow the official installation instructions at the [Poetry website](https://python-poetry.org/docs/#installation).\n\n2. **Create a New Project (Optional)**:\n    ```bash\n    poetry new myproject\n    cd myproject\n    ```\n\n3. **Add the Package as a Dependency**:\n    ```bash\n    poetry add python-sage-bbb\n    ```\n\n4. **Activate the Virtual Environment**:\n    ```bash\n    poetry shell\n    ```\n\n## Configuration\n\n1. **Add `sage_language` to `INSTALLED_APPS`** in `settings.py`:\n   ```python\n   INSTALLED_APPS = [\n       ...\n       'sage_language',\n   ]\n   ```\n\n2. **Middleware Configuration**:\n   Ensure `CookieLocaleMiddleware` is added immediately after `SessionMiddleware` in the `MIDDLEWARE` setting:\n   ```python\n   MIDDLEWARE = [\n       'django.middleware.security.SecurityMiddleware',\n       'django.contrib.sessions.middleware.SessionMiddleware',\n       'sage_language.middlewares.cookie.CookieLocaleMiddleware',\n       ...\n   ]\n   ```\n\n3. **Set Language Settings**:\n   Define supported languages, default language, and cookie names:\n   ```python\n   LANGUAGES = [\n       ('en', 'English'),\n       ('fa', 'Farsi'),\n       ('es', 'Spanish'),\n   ]\n   LANGUAGE_CODE = 'en'\n   SAGE_LANGUAGE_COOKIE_NAME = \"ivan_language\"\n   LANGUAGE_COOKIE_NAME = SAGE_LANGUAGE_COOKIE_NAME\n   ```\n\n4. **Add URL Configuration for Language Switching**:\n   In `urls.py`, set up the language switching view:\n   ```python\n   from sage_language.views import SetLanguageView\n   urlpatterns = [\n       path('set-language/', SetLanguageView.as_view(), name='set_language'),\n       path(\"i18n/\", include(\"django.conf.urls.i18n\")),\n   ]\n   ```\n\n## Usage\n\n1. **Setting Language Preference**:\n   To change the language preference, make a POST request to `/set-language/` with `language` and `next` parameters:\n   - `language`: The desired language code (e.g., 'en', 'fa', 'es').\n   - `next`: The URL to redirect to after setting the language.\n\n2. **URL Prefix Management**:\n   The middleware automatically adds or removes language prefixes from URLs based on the user\u2019s selected language and cookie settings.\n\n3. **Automatic Language Redirection**:\n   If a user\u2019s preferred language (stored in a cookie) differs from the language in the URL, the middleware will redirect them to the correct URL.\n\n## Notes\n\n- Make sure to set `USE_I18N`, `USE_L10N`, and `USE_TZ` to `True` in `settings.py` for proper internationalization support.\n- For production use, ensure `SAGE_LANGUAGE_COOKIE_NAME` and `LANGUAGE_COOKIE_NAME` are set to the same value for consistent cookie management.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Django Project for changing language.",
    "version": "0.1.2",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "45ebdcb83cd9d8078cf566f21662c31e14ef36108383fedf673b612e93df58aa",
                "md5": "4964167d64a98c53bdc6964e6e5c21a5",
                "sha256": "e691c6e49b184348991e28903d1cf98176eeb8b8cd9b539121b3c301d8fb6ce5"
            },
            "downloads": -1,
            "filename": "django_sage_language-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4964167d64a98c53bdc6964e6e5c21a5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 13036,
            "upload_time": "2024-12-19T04:47:30",
            "upload_time_iso_8601": "2024-12-19T04:47:30.412020Z",
            "url": "https://files.pythonhosted.org/packages/45/eb/dcb83cd9d8078cf566f21662c31e14ef36108383fedf673b612e93df58aa/django_sage_language-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c4e4ea48e2b7c303593f297742f2c0fd7da358a47768a42a19a9d9a2f50a935d",
                "md5": "2b53afd5b1e918c6d9ef9567be91faf4",
                "sha256": "c647f4a988fe91e27a4210ebcc3b0a851edab63ab589c2cdec38118a30f41bb8"
            },
            "downloads": -1,
            "filename": "django_sage_language-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "2b53afd5b1e918c6d9ef9567be91faf4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 13220,
            "upload_time": "2024-12-19T04:47:31",
            "upload_time_iso_8601": "2024-12-19T04:47:31.568541Z",
            "url": "https://files.pythonhosted.org/packages/c4/e4/ea48e2b7c303593f297742f2c0fd7da358a47768a42a19a9d9a2f50a935d/django_sage_language-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-19 04:47:31",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "django-sage-language"
}
        
Elapsed time: 3.38426s