Flask-Translator


NameFlask-Translator JSON
Version 0.1 PyPI version JSON
download
home_pagehttps://github.com/inalbilal/flask-translator/
SummaryFlask-Translator is a quick and easy-to-use extension for Flask projects, enabling translation from YAML files.
upload_time2023-12-23 13:57:22
maintainer
docs_urlNone
authorBilal Inal
requires_python
licenseBSD
keywords flask flask extension translation i18n l10n multilingual language internationalization localization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Flask Translator



Flask Translator is a simple Flask extension for handling translations in your web applications. It provides easy

integration with Flask applications to support multiple languages.



## Installation



You can install Flask Translator using pip:



```bash

pip install flask-translator

```



## Usage



### Example Project Directory Structure



```bash

your_project_directory/

|-- app.py

|-- translations/

| |-- messages.en.yaml

| |-- messages.tr.yaml

|-- templates/

| |-- index.html

|-- README.md

|-- requirements.txt

```



### Translation File Examples



```yaml

# translations/messages.en.yaml



request:

  schema:

    invalid_email: 'Please enter a valid email address!'

    invalid_username: 'Please enter a valid username!'

    invalid_or_email_username: 'Please enter a valid username or email address!'

```



```yaml

# translations/messages.tr.yaml



request:

  schema:

    invalid_email: 'Lütfen geçerli bir e-posta adresi girin!'

    invalid_username: 'Lütfen geçerli bir kullanıcı adı girin!'

    invalid_or_email_username: 'Lütfen geçerli bir kullanıcı adı veya e-posta adresi girin!'

```



Make sure to place your translation files in the specified `TRANSLATIONS_PATH` directory with the corresponding language codes following the ISO 639-1 standard.





### App Factory Usage



```python

# app.py



from flask import Flask, render_template

from flask_translator import Translator





def create_app():

    app = Flask(__name__)



    # Configure Flask Translator

    app.config['DEFAULT_LANG'] = 'tr'

    app.config['LANG_INITIALIZER'] = 'headers'

    app.config['DEFAULT_MESSAGE'] = 'Process finished!'

    app.config['TRANSLATIONS_PATH'] = 'translations'



    # Initialize Flask Translator with the app

    t = Translator()

    t.init_app(app)



    @app.route('/')

    def home():

        message = t.translate('request.schema.invalid_or_email_username')

        return render_template('index.html', message=message)



    return app

```



### Normal Application Usage



```python

# app.py



from flask import Flask, render_template

from flask_translator import Translator



app = Flask(__name__)



# Configure Flask Translator

app.config['DEFAULT_LANG'] = 'tr'

app.config['LANG_INITIALIZER'] = 'headers'

app.config['DEFAULT_MESSAGE'] = 'Process finished!'

app.config['TRANSLATIONS_PATH'] = 'translations'



# Initialize Flask Translator with the app

t = Translator(app)



@app.route('/')

def home():

    message = t.translate('request.schema.invalid_or_email_username')

    return render_template('index.html', message=message)



```



### Configuration Options



`DEFAULT_LANG`: The default language to use if no language is explicitly specified. Should be in ISO 639-1 code

standards. Default is '`en`' (English).



`LANG_INITIALIZER`: Determines the method to initialize the language. Possible values are 'headers', 'cookies', '

sessions'. If none of these values are present, it falls back to the `DEFAULT_LANG` value. Default is '`headers`'.



`DEFAULT_MESSAGE`: The default message to display if a translation is not found for the given key. Default

is '`Process finished!`'.



`TRANSLATIONS_PATH`: The path to the directory containing translation files. Default is '`translations`'.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/inalbilal/flask-translator/",
    "name": "Flask-Translator",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "Flask,Flask extension,translation,i18n,l10n,multilingual,language,internationalization,localization",
    "author": "Bilal Inal",
    "author_email": "01bilalinal@gmail.com",
    "download_url": "",
    "platform": null,
    "description": "\r\n# Flask Translator\r\n\r\n\r\n\r\nFlask Translator is a simple Flask extension for handling translations in your web applications. It provides easy\r\n\r\nintegration with Flask applications to support multiple languages.\r\n\r\n\r\n\r\n## Installation\r\n\r\n\r\n\r\nYou can install Flask Translator using pip:\r\n\r\n\r\n\r\n```bash\r\n\r\npip install flask-translator\r\n\r\n```\r\n\r\n\r\n\r\n## Usage\r\n\r\n\r\n\r\n### Example Project Directory Structure\r\n\r\n\r\n\r\n```bash\r\n\r\nyour_project_directory/\r\n\r\n|-- app.py\r\n\r\n|-- translations/\r\n\r\n| |-- messages.en.yaml\r\n\r\n| |-- messages.tr.yaml\r\n\r\n|-- templates/\r\n\r\n| |-- index.html\r\n\r\n|-- README.md\r\n\r\n|-- requirements.txt\r\n\r\n```\r\n\r\n\r\n\r\n### Translation File Examples\r\n\r\n\r\n\r\n```yaml\r\n\r\n# translations/messages.en.yaml\r\n\r\n\r\n\r\nrequest:\r\n\r\n  schema:\r\n\r\n    invalid_email: 'Please enter a valid email address!'\r\n\r\n    invalid_username: 'Please enter a valid username!'\r\n\r\n    invalid_or_email_username: 'Please enter a valid username or email address!'\r\n\r\n```\r\n\r\n\r\n\r\n```yaml\r\n\r\n# translations/messages.tr.yaml\r\n\r\n\r\n\r\nrequest:\r\n\r\n  schema:\r\n\r\n    invalid_email: 'L\u00fctfen ge\u00e7erli bir e-posta adresi girin!'\r\n\r\n    invalid_username: 'L\u00fctfen ge\u00e7erli bir kullan\u0131c\u0131 ad\u0131 girin!'\r\n\r\n    invalid_or_email_username: 'L\u00fctfen ge\u00e7erli bir kullan\u0131c\u0131 ad\u0131 veya e-posta adresi girin!'\r\n\r\n```\r\n\r\n\r\n\r\nMake sure to place your translation files in the specified `TRANSLATIONS_PATH` directory with the corresponding language codes following the ISO 639-1 standard.\r\n\r\n\r\n\r\n\r\n\r\n### App Factory Usage\r\n\r\n\r\n\r\n```python\r\n\r\n# app.py\r\n\r\n\r\n\r\nfrom flask import Flask, render_template\r\n\r\nfrom flask_translator import Translator\r\n\r\n\r\n\r\n\r\n\r\ndef create_app():\r\n\r\n    app = Flask(__name__)\r\n\r\n\r\n\r\n    # Configure Flask Translator\r\n\r\n    app.config['DEFAULT_LANG'] = 'tr'\r\n\r\n    app.config['LANG_INITIALIZER'] = 'headers'\r\n\r\n    app.config['DEFAULT_MESSAGE'] = 'Process finished!'\r\n\r\n    app.config['TRANSLATIONS_PATH'] = 'translations'\r\n\r\n\r\n\r\n    # Initialize Flask Translator with the app\r\n\r\n    t = Translator()\r\n\r\n    t.init_app(app)\r\n\r\n\r\n\r\n    @app.route('/')\r\n\r\n    def home():\r\n\r\n        message = t.translate('request.schema.invalid_or_email_username')\r\n\r\n        return render_template('index.html', message=message)\r\n\r\n\r\n\r\n    return app\r\n\r\n```\r\n\r\n\r\n\r\n### Normal Application Usage\r\n\r\n\r\n\r\n```python\r\n\r\n# app.py\r\n\r\n\r\n\r\nfrom flask import Flask, render_template\r\n\r\nfrom flask_translator import Translator\r\n\r\n\r\n\r\napp = Flask(__name__)\r\n\r\n\r\n\r\n# Configure Flask Translator\r\n\r\napp.config['DEFAULT_LANG'] = 'tr'\r\n\r\napp.config['LANG_INITIALIZER'] = 'headers'\r\n\r\napp.config['DEFAULT_MESSAGE'] = 'Process finished!'\r\n\r\napp.config['TRANSLATIONS_PATH'] = 'translations'\r\n\r\n\r\n\r\n# Initialize Flask Translator with the app\r\n\r\nt = Translator(app)\r\n\r\n\r\n\r\n@app.route('/')\r\n\r\ndef home():\r\n\r\n    message = t.translate('request.schema.invalid_or_email_username')\r\n\r\n    return render_template('index.html', message=message)\r\n\r\n\r\n\r\n```\r\n\r\n\r\n\r\n### Configuration Options\r\n\r\n\r\n\r\n`DEFAULT_LANG`: The default language to use if no language is explicitly specified. Should be in ISO 639-1 code\r\n\r\nstandards. Default is '`en`' (English).\r\n\r\n\r\n\r\n`LANG_INITIALIZER`: Determines the method to initialize the language. Possible values are 'headers', 'cookies', '\r\n\r\nsessions'. If none of these values are present, it falls back to the `DEFAULT_LANG` value. Default is '`headers`'.\r\n\r\n\r\n\r\n`DEFAULT_MESSAGE`: The default message to display if a translation is not found for the given key. Default\r\n\r\nis '`Process finished!`'.\r\n\r\n\r\n\r\n`TRANSLATIONS_PATH`: The path to the directory containing translation files. Default is '`translations`'.\r\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Flask-Translator is a quick and easy-to-use extension for Flask projects, enabling translation from YAML files.",
    "version": "0.1",
    "project_urls": {
        "Homepage": "https://github.com/inalbilal/flask-translator/"
    },
    "split_keywords": [
        "flask",
        "flask extension",
        "translation",
        "i18n",
        "l10n",
        "multilingual",
        "language",
        "internationalization",
        "localization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1b2b8c8eec922c6a862377aa331764978883c46a402b3e1eeb4bad35483835b7",
                "md5": "fe5e3925405af9668df65ce1313bb25f",
                "sha256": "5a5a41db867d48833354d333d12556e1e4645bafefad0e4e3f71e51e77967928"
            },
            "downloads": -1,
            "filename": "Flask_Translator-0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fe5e3925405af9668df65ce1313bb25f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4704,
            "upload_time": "2023-12-23T13:57:22",
            "upload_time_iso_8601": "2023-12-23T13:57:22.751096Z",
            "url": "https://files.pythonhosted.org/packages/1b/2b/8c8eec922c6a862377aa331764978883c46a402b3e1eeb4bad35483835b7/Flask_Translator-0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-23 13:57:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "inalbilal",
    "github_project": "flask-translator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "flask-translator"
}
        
Elapsed time: 0.20967s