lococore


Namelococore JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/drago-suzuki58/LocoCore
SummaryLocoCore is a library that provides a base class for multilingual applications.
upload_time2024-11-17 18:39:09
maintainerNone
docs_urlNone
authordrago-suzuki58
requires_python<4.0,>=3.11
licenseMIT
keywords json localization translation localisation multi-language
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # LocoCore

LocoCore is a library that provides a base class for multilingual applications.
By inheriting this class and implementing your own data loading logic, you can flexibly handle translation data from any data source such as JSON files or databases.
If you want to use an already implemented subclass, please use [LocoJSON](https://github.com/drago-suzuki58/LocoJSON) or [LocoTOML](https://github.com/drago-suzuki58/LocoTOML).

## このライブラリの使い方

By inheriting the `LocoCore` base class and implementing the `_load_translations` method yourself, you can obtain translation data from your own files and achieve flexible implementation.

As long as you can load each translation into `self.translations[locale]` (where `locale` is the specified language) in a nested dictionary format (such as `Dict[str, Dict[str, Any]]`), theoretically any file format is possible.

## サンプルコード

Below is the code for the `LocoJSON` subclass library, which is an extension library.

```python
import json
import os
from lococore import LocoCore

class LocoJSON(LocoCore):
    def _load_translations(self, locale: str) -> None:
        if locale in self.cache:
            self.translations[locale] = self.cache[locale]
            return

        locale_file = os.path.join(self.locale_dir, f"{locale}.json")
        if os.path.exists(locale_file):
            try:
                with open(locale_file, "r", encoding="utf-8") as f:
                    self.translations[locale] = json.load(f)
                    self.cache[locale] = self.translations[locale]
                self.logger.info(f"Loaded JSON file for locale {locale}")
            except json.JSONDecodeError as e:
                self.logger.error(f"Failed to load JSON file for locale {locale}: {e}")
```

- Checking the Cache

  If the data is already loaded in self.cache, it avoids reloading and uses it directly.

- Loading JSON Files

  It loads the file corresponding to the specified language from self.locale_dir and registers it as nested dictionary translation data.

- Error Handling

  It logs errors if the file format is invalid or if loading fails.

You do not need to implement all of these features, but having these features will make it more user-friendly.

Also, to actually use this class, you can do the following:

```python
loc = LocoJSON("ja")

python(loc.example1())
```

By applying this, you can easily implement very flexible translation functionality using LocoCore.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/drago-suzuki58/LocoCore",
    "name": "lococore",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": "json, localization, translation, localisation, multi-language",
    "author": "drago-suzuki58",
    "author_email": "dorasuzu58@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/09/5a/5110ce6fb849b7689ef8f4b0d42fd273165d32a4a94f46d50177b71a4e32/lococore-0.2.0.tar.gz",
    "platform": null,
    "description": "# LocoCore\n\nLocoCore is a library that provides a base class for multilingual applications.\nBy inheriting this class and implementing your own data loading logic, you can flexibly handle translation data from any data source such as JSON files or databases.\nIf you want to use an already implemented subclass, please use [LocoJSON](https://github.com/drago-suzuki58/LocoJSON) or [LocoTOML](https://github.com/drago-suzuki58/LocoTOML).\n\n## \u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4f7f\u3044\u65b9\n\nBy inheriting the `LocoCore` base class and implementing the `_load_translations` method yourself, you can obtain translation data from your own files and achieve flexible implementation.\n\nAs long as you can load each translation into `self.translations[locale]` (where `locale` is the specified language) in a nested dictionary format (such as `Dict[str, Dict[str, Any]]`), theoretically any file format is possible.\n\n## \u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\n\nBelow is the code for the `LocoJSON` subclass library, which is an extension library.\n\n```python\nimport json\nimport os\nfrom lococore import LocoCore\n\nclass LocoJSON(LocoCore):\n    def _load_translations(self, locale: str) -> None:\n        if locale in self.cache:\n            self.translations[locale] = self.cache[locale]\n            return\n\n        locale_file = os.path.join(self.locale_dir, f\"{locale}.json\")\n        if os.path.exists(locale_file):\n            try:\n                with open(locale_file, \"r\", encoding=\"utf-8\") as f:\n                    self.translations[locale] = json.load(f)\n                    self.cache[locale] = self.translations[locale]\n                self.logger.info(f\"Loaded JSON file for locale {locale}\")\n            except json.JSONDecodeError as e:\n                self.logger.error(f\"Failed to load JSON file for locale {locale}: {e}\")\n```\n\n- Checking the Cache\n\n  If the data is already loaded in self.cache, it avoids reloading and uses it directly.\n\n- Loading JSON Files\n\n  It loads the file corresponding to the specified language from self.locale_dir and registers it as nested dictionary translation data.\n\n- Error Handling\n\n  It logs errors if the file format is invalid or if loading fails.\n\nYou do not need to implement all of these features, but having these features will make it more user-friendly.\n\nAlso, to actually use this class, you can do the following:\n\n```python\nloc = LocoJSON(\"ja\")\n\npython(loc.example1())\n```\n\nBy applying this, you can easily implement very flexible translation functionality using LocoCore.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "LocoCore is a library that provides a base class for multilingual applications.",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/drago-suzuki58/LocoCore",
        "Issues": "https://github.com/drago-suzuki58/LocoCore/issues",
        "Repository": "https://github.com/drago-suzuki58/LocoCore"
    },
    "split_keywords": [
        "json",
        " localization",
        " translation",
        " localisation",
        " multi-language"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "65909b228053ede11bab5cc0a942e5e41630c5e31a94123cfbe1a22ed6fccf67",
                "md5": "b182698a122b810b7d2125250434f03c",
                "sha256": "57749cf3f6430619f721ae847adcb1ef4297c3635f28e1eb9e34317a5c4a147e"
            },
            "downloads": -1,
            "filename": "lococore-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b182698a122b810b7d2125250434f03c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 5289,
            "upload_time": "2024-11-17T18:39:07",
            "upload_time_iso_8601": "2024-11-17T18:39:07.085942Z",
            "url": "https://files.pythonhosted.org/packages/65/90/9b228053ede11bab5cc0a942e5e41630c5e31a94123cfbe1a22ed6fccf67/lococore-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "095a5110ce6fb849b7689ef8f4b0d42fd273165d32a4a94f46d50177b71a4e32",
                "md5": "25d50efa0d9b1da656d87cc5f9f5d234",
                "sha256": "9987f54883806b064ed26e61cc7d91f909b5fdb43a39ec6884454aa12a19fb3e"
            },
            "downloads": -1,
            "filename": "lococore-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "25d50efa0d9b1da656d87cc5f9f5d234",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 4483,
            "upload_time": "2024-11-17T18:39:09",
            "upload_time_iso_8601": "2024-11-17T18:39:09.707122Z",
            "url": "https://files.pythonhosted.org/packages/09/5a/5110ce6fb849b7689ef8f4b0d42fd273165d32a4a94f46d50177b71a4e32/lococore-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-17 18:39:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "drago-suzuki58",
    "github_project": "LocoCore",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "lococore"
}
        
Elapsed time: 0.46525s