Skip to main content

A simple language manager for Python projects.

Project description

Lang Custom v1.0.11

Lang Custom is a simple Python library that helps manage and load translations from JSON files.

Why did I create Lang Custom?

One day, I decided to make my bot support multiple languages. However, when searching for translation libraries, I realized that most of them were quite bad. So, I decided to create my own language files with customizable tones.

Initially, managing language files seemed simple, but then I realized that without a standard library, everything became very messy. Even though they were all JSON files, different code segments loaded language data in their own way—especially if you use AI tools like ChatGPT for assistance. There was no common standard.

Looking back at my source code, I could only exclaim: "It's amazing it doesn't crash :v" I wasn't sure if my code was working as expected, and every time I made changes, I was always worried that some parts would still work fine, but others might encounter errors due to inconsistent handling.

So, I created Lang Custom—a library that helps manage the language system more easily, consistently, and without headaches.

Installation

You can install this library using pip:

pip install lang_custom

What's new

Updated logic: old

lang_custom.set_group("name")
lang_custom.get_text("en", "text")

to make it easier to control

lang_custom.lang("en").group("name").get_text("text")

Usage Guide

1. Import the library

import lang_custom

2. Get the list of available language files

The library will automatically detect all JSON files in the Lang_data directory in your source code. To list the available language files, use:

languages = lang_custom.get()
print(languages)  # Example: ['en', 'vi', 'jp',..] depending on the number of JSON files in the Lang_Data directory

console example

['en', 'vi', 'jp']

Each element in the list represents a JSON file in the language directory.

3. Select language and data group

Before retrieving text data, you need to select a language and data group from the JSON file:

lang_custom.lang("en").group("bot_random", cache=True)

Where:

  • "en" is the language you want to use.
  • "bot_random" is the group you want to access in the JSON structure.
  • cache=True is an option to use cache to help the bot retrieve data faster (the downside is that it does not update in real-time, the default if not specified is True). You must use the reload method to update if needed.

4. Retrieve text data

After selecting the language and group, you can retrieve the text using:

text = lang_custom.lang("en").group("bot_reply", cache=True).get_text("text1")
print(text)  # Displays the value corresponding to the key "text1" in the group "bot_random" from en.json

console example

hello :D

Or retrieve random text from a list:

random_text = lang_custom.lang("en").group("bot_random").random_text("text_random")
print(random_text)  # Displays a random value from the list "text_random" in the group "bot_random" from en.json

console example

text1 or text2 or 3

5. Clear and update cache

If you want to clear and update all cache, you can use the reload method:

lang_custom.reload()

This method will clear all cache and update data from the JSON files.

Language file structure

Each language file is stored in the Lang_Custom directory (default translations) or Lang_data directory (user-added translations). Example of Lang_Custom/en.json:

{
    "bot_reply": {
        "text1": "hello :D",
        "text2": "hi :3"
    },
    "bot_random": {
        "instruct": "use square brackets to random",
        "text_random": ["text1", "text2", "text.."]
    }
}

Users can add their own language JSON files in the Lang_data directory, as long as they follow the valid structure.

Feedback & Issues

If you have feedback or encounter issues, please contact me: Discord me

Thank you for using Lang_Custom!

Thank you

Lang Custom v1.0.11

Lang Custom là một thư viện Python đơn giản giúp quản lý và tải bản dịch từ các tệp JSON.

Tại sao tôi tạo ra Lang Custom?

Một ngày nọ, tôi quyết định làm cho bot của mình hỗ trợ nhiều ngôn ngữ. Tuy nhiên, khi tìm kiếm các thư viện dịch thuật, tôi nhận ra rằng hầu hết chúng đều khá tệ. Vì vậy, tôi quyết định tự tạo các tệp ngôn ngữ với ngữ điệu có thể tùy chỉnh.

Ban đầu, việc quản lý các tệp ngôn ngữ có vẻ đơn giản, nhưng sau đó tôi nhận ra rằng nếu không có một thư viện chuẩn, mọi thứ trở nên rất lộn xộn. Dù tất cả đều là tệp JSON, nhưng các đoạn mã khác nhau lại tải dữ liệu ngôn ngữ theo cách riêng—đặc biệt nếu bạn dùng các công cụ AI như ChatGPT để hỗ trợ. Không có một tiêu chuẩn chung nào cả.

Nhìn lại mã nguồn của mình, tôi chỉ có thể thốt lên: "Tởm vl nó ko crash được cũng hay đấy :v" không chắc liệu đoạn mã của mình có hoạt động đúng như mong muốn không, và mỗi khi chỉnh sửa, lúc nào cũng lo sợ rằng một số phần vẫn hoạt động tốt, nhưng những phần khác có thể gặp lỗi do xử lý không đồng nhất.

Vì vậy, tôi đã tạo ra Lang Custom—một thư viện giúp quản lý hệ thống ngôn ngữ dễ dàng hơn, nhất quán hơn và không còn gây đau đầu nữa.

Cài đặt

Bạn có thể cài đặt thư viện này bằng pip:

pip install lang_custom

Có gì mới

Sửa lại logic: cũ

lang_custom.set_group("name")
lang_custom.get_text("en", "text")

thành giúp dễ dàng kiểm soát hơn

lang_custom.lang("en").group("name").get_text("text")

Hướng dẫn sử dụng

1. Nhập thư viện

import lang_custom

2. Lấy danh sách các tệp ngôn ngữ có sẵn

Thư viện sẽ tự động phát hiện tất cả các tệp JSON trong thư mục Lang_data trong mã nguồn của bạn. Để liệt kê các tệp ngôn ngữ có sẵn, sử dụng:

languages = lang_custom.get()
print(languages)  # Ví dụ: ['en', 'vi', 'jp',..] tùy vào số lượng file json trong thư mục Lang_Data

console example

['en', 'vi', 'jp']

Mỗi phần tử trong danh sách đại diện cho một tệp JSON có trong thư mục ngôn ngữ.

3. Chọn ngôn ngữ và nhóm dữ liệu

Trước khi lấy dữ liệu văn bản, bạn cần chọn ngôn ngữ và nhóm dữ liệu từ tệp JSON:

lang_custom.lang("en").group("bot_random", cache=True)

Trong đó:

  • "en" là ngôn ngữ bạn muốn sử dụng.
  • "bot_random" là nhóm bạn muốn truy cập trong cấu trúc JSON.
  • cache=True là tùy chọn để sử dụng cache giúp bot truy xuất dữ liệu nhanh hơn (nhược điểm không cập nhật nóng được, mặc định nếu bạn không đề cập là True). Bạn phải sử dụng phương thức reload để cập nhật lại nếu muốn.

4. Lấy dữ liệu văn bản

Sau khi chọn ngôn ngữ và nhóm, bạn có thể lấy văn bản bằng cách sử dụng:

text = lang_custom.lang("en").group("bot_reply", cache=True).get_text("text1")
print(text)  # Hiển thị giá trị tương ứng với khóa "text1" trong nhóm "bot_random" từ en.json

console example

hello :D

Hoặc lấy văn bản ngẫu nhiên từ danh sách:

random_text = lang_custom.lang("en").group("bot_random").random_text("text_random")
print(random_text)  # Hiển thị một giá trị ngẫu nhiên từ danh sách "text_random" trong nhóm "bot_random" từ en.json

console example

text1 or text2 or 3

5. Xóa và cập nhật lại cache

Nếu bạn muốn xóa và cập nhật lại tất cả cache, bạn có thể sử dụng phương thức reload:

lang_custom.reload()

Phương thức này sẽ xóa toàn bộ cache và cập nhật lại dữ liệu từ các tệp JSON.

Cấu trúc tệp ngôn ngữ

Mỗi tệp ngôn ngữ được lưu trong thư mục Lang_Custom (bản dịch mặc định) hoặc Lang_data (bản dịch do người dùng thêm vào). Ví dụ về Lang_Custom/en.json:

{
    "bot_reply": {
        "text1": "hello :D",
        "text2": "hi :3"
    },
    "bot_random": {
        "instruct": "use square brackets to random",
        "text_random": ["text1", "text2", "text.."]
    }
}

Người dùng có thể thêm các tệp JSON ngôn ngữ của riêng mình trong thư mục Lang_data, miễn là tuân theo cấu trúc hợp lệ.

Phản hồi & Báo lỗi

Nếu bạn có phản hồi hoặc gặp vấn đề, vui lòng liên hệ tôi: Discord me

Cảm ơn bạn đã sử dụng Lang_Custom!

Cảm ơn

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lang_custom-1.0.12.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lang_custom-1.0.12-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file lang_custom-1.0.12.tar.gz.

File metadata

  • Download URL: lang_custom-1.0.12.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lang_custom-1.0.12.tar.gz
Algorithm Hash digest
SHA256 670331218a1d991d2eb2e6c00cbb96c16041e3adc2faeb69f5fed5eb27d39e9c
MD5 04107b1e05721908d9007f7889e976fd
BLAKE2b-256 8c6be435da23cce142a8f30598bf1be1c7cdc29d408dc803c601e797da78b407

See more details on using hashes here.

File details

Details for the file lang_custom-1.0.12-py3-none-any.whl.

File metadata

  • Download URL: lang_custom-1.0.12-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lang_custom-1.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4ba05584db89ad538a332ceee7287275c3319f13e665ce805d7c819dd33a8224
MD5 1a94005fcf545c8aafb91e9ef39c6c4e
BLAKE2b-256 6c24a2c6b80e94e6a13553a8bc7785630251bf763de78173c7f6359727a95ee2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page