translate-projects-django 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (21) hide show
  1. translate-projects-django-0.0.2/MANIFEST.in +2 -0
  2. translate-projects-django-0.0.2/PKG-INFO +54 -0
  3. translate-projects-django-0.0.2/README.md +29 -0
  4. translate-projects-django-0.0.2/pyproject.toml +3 -0
  5. translate-projects-django-0.0.2/setup.cfg +4 -0
  6. translate-projects-django-0.0.2/setup.py +36 -0
  7. translate-projects-django-0.0.2/translate_projects_django/__init__.py +0 -0
  8. translate-projects-django-0.0.2/translate_projects_django/api_utils.py +32 -0
  9. translate-projects-django-0.0.2/translate_projects_django/apps.py +6 -0
  10. translate-projects-django-0.0.2/translate_projects_django/hash_utils.py +34 -0
  11. translate-projects-django-0.0.2/translate_projects_django/manage_translations.py +61 -0
  12. translate-projects-django-0.0.2/translate_projects_django/management/__init__.py +0 -0
  13. translate-projects-django-0.0.2/translate_projects_django/management/commands/__init__.py +0 -0
  14. translate-projects-django-0.0.2/translate_projects_django/management/commands/translate.py +64 -0
  15. translate-projects-django-0.0.2/translate_projects_django/migrations/__init__.py +0 -0
  16. translate-projects-django-0.0.2/translate_projects_django/translation_utils.py +31 -0
  17. translate-projects-django-0.0.2/translate_projects_django.egg-info/PKG-INFO +54 -0
  18. translate-projects-django-0.0.2/translate_projects_django.egg-info/SOURCES.txt +19 -0
  19. translate-projects-django-0.0.2/translate_projects_django.egg-info/dependency_links.txt +1 -0
  20. translate-projects-django-0.0.2/translate_projects_django.egg-info/requires.txt +2 -0
  21. translate-projects-django-0.0.2/translate_projects_django.egg-info/top_level.txt +1 -0
@@ -0,0 +1,2 @@
1
+ include README.md
2
+ include LICENSE
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.1
2
+ Name: translate-projects-django
3
+ Version: 0.0.2
4
+ Summary: Translate your django templates easily
5
+ Home-page: https://github.com/translate-projects/translate-projects-django
6
+ Author: Neider Ruiz
7
+ Author-email: contact@neiderruiz.com
8
+ License: MIT
9
+ Platform: UNKNOWN
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.7
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Framework :: Django
19
+ Classifier: Framework :: Django :: 3.0
20
+ Classifier: Framework :: Django :: 3.1
21
+ Classifier: Framework :: Django :: 3.2
22
+ Requires-Python: >=3.7
23
+ Description-Content-Type: text/markdown
24
+
25
+ # Translate Projects Django
26
+
27
+ Effortlessly manage and automate the translation of your Django-based projects with Translate Projects Django. This powerful package streamlines the process of translating your Django templates and messages, making it easier than ever to reach a global audience. Whether you're working on an open-source project or a private enterprise application, Translate Projects Django ensures that your application is ready for internationalization with minimal effort.
28
+
29
+ [See documentation](https://translateprojects.neiderruiz.com/docs/python/django)
30
+
31
+ ## Features
32
+
33
+ - **Easy Integration**: Seamlessly integrate with your existing Docusaurus project.
34
+ - **Multi-language Support**: Translate your documentation into multiple languages effortlessly.
35
+ - **Customizable**: Tailor the translation process to fit your project's needs.
36
+ - **Open Source**: Free to use and open to contributions.
37
+
38
+
39
+ ## Support
40
+
41
+ If you have any questions, need help, or want to contribute, feel free to reach out:
42
+
43
+ - **Website**: [neiderruiz.com](https://neiderruiz.com)
44
+ - **GitHub**: [@neiderruiz](https://github.com/neiderruiz)
45
+ - **Twitter**: [@neiderruiz](https://x.com/neiderruiz_)
46
+ - **YouTube**: [Neider Ruiz](https://youtube.com/@neiderruiz)
47
+
48
+
49
+ ## Support the Project
50
+
51
+ If you find this project useful, consider buying me a coffee to help me keep improving it. Your support means a lot! ā˜•
52
+
53
+ [![Buy me a coffee](https://img.buymeacoffee.com/button-api/?text=Buy%20me%20a%20coffee&emoji=ā˜•&slug=neiderruiz&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff)](https://www.buymeacoffee.com/neiderruiz)
54
+
@@ -0,0 +1,29 @@
1
+ # Translate Projects Django
2
+
3
+ Effortlessly manage and automate the translation of your Django-based projects with Translate Projects Django. This powerful package streamlines the process of translating your Django templates and messages, making it easier than ever to reach a global audience. Whether you're working on an open-source project or a private enterprise application, Translate Projects Django ensures that your application is ready for internationalization with minimal effort.
4
+
5
+ [See documentation](https://translateprojects.neiderruiz.com/docs/python/django)
6
+
7
+ ## Features
8
+
9
+ - **Easy Integration**: Seamlessly integrate with your existing Docusaurus project.
10
+ - **Multi-language Support**: Translate your documentation into multiple languages effortlessly.
11
+ - **Customizable**: Tailor the translation process to fit your project's needs.
12
+ - **Open Source**: Free to use and open to contributions.
13
+
14
+
15
+ ## Support
16
+
17
+ If you have any questions, need help, or want to contribute, feel free to reach out:
18
+
19
+ - **Website**: [neiderruiz.com](https://neiderruiz.com)
20
+ - **GitHub**: [@neiderruiz](https://github.com/neiderruiz)
21
+ - **Twitter**: [@neiderruiz](https://x.com/neiderruiz_)
22
+ - **YouTube**: [Neider Ruiz](https://youtube.com/@neiderruiz)
23
+
24
+
25
+ ## Support the Project
26
+
27
+ If you find this project useful, consider buying me a coffee to help me keep improving it. Your support means a lot! ā˜•
28
+
29
+ [![Buy me a coffee](https://img.buymeacoffee.com/button-api/?text=Buy%20me%20a%20coffee&emoji=ā˜•&slug=neiderruiz&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff)](https://www.buymeacoffee.com/neiderruiz)
@@ -0,0 +1,3 @@
1
+ [build-system]
2
+ requires = ["setuptools>=42", "wheel"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,36 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ with open("README.md", "r", encoding="utf-8") as fh:
4
+ long_description = fh.read()
5
+
6
+ setup(
7
+ name="translate-projects-django",
8
+ version="0.0.2",
9
+ author="Neider Ruiz",
10
+ author_email="contact@neiderruiz.com",
11
+ description="Translate your django templates easily",
12
+ long_description=long_description,
13
+ long_description_content_type="text/markdown",
14
+ url="https://github.com/translate-projects/translate-projects-django",
15
+ packages=find_packages(),
16
+ license="MIT",
17
+ classifiers=[
18
+ "Development Status :: 3 - Alpha",
19
+ "Intended Audience :: Developers",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.7",
23
+ "Programming Language :: Python :: 3.8",
24
+ "Programming Language :: Python :: 3.9",
25
+ "Programming Language :: Python :: 3.10",
26
+ "Framework :: Django",
27
+ "Framework :: Django :: 3.0",
28
+ "Framework :: Django :: 3.1",
29
+ "Framework :: Django :: 3.2",
30
+ ],
31
+ python_requires=">=3.7",
32
+ install_requires=[
33
+ "Django>=3.0",
34
+ "polib>=1.2.0",
35
+ ],
36
+ )
@@ -0,0 +1,32 @@
1
+ import requests
2
+ from django.conf import settings
3
+
4
+ def get_translations_from_api(data, source_lang, target_lang, route_file):
5
+ url_api = f'https://translateprojects.neiderruiz.com/api/general_translations/?source_lang={source_lang}&target_lang={target_lang}'
6
+
7
+ if route_file:
8
+ url_api += f"&route_file={route_file}"
9
+
10
+ url_api += '&type_project=django'
11
+
12
+ token = settings.TRANSLATEFILES_API_TOKEN
13
+
14
+ if token:
15
+ token = f"Token {token}"
16
+
17
+ response = requests.post(
18
+ url_api,
19
+ json=data,
20
+ headers={
21
+ 'Content-Type': 'application/json',
22
+ 'Accept': 'application/json',
23
+ 'Authorization': token,
24
+ }
25
+ )
26
+ if response.status_code == 524:
27
+ get_translations_from_api(data, source_lang, target_lang, route_file)
28
+
29
+ if response.status_code != 200 and response.status_code != 524:
30
+ raise Exception(f"Error on updating translation data: {response.status_code}")
31
+
32
+ return response.json()
@@ -0,0 +1,6 @@
1
+ from django.apps import AppConfig
2
+
3
+
4
+ class DjangoTranslateFilesConfig(AppConfig):
5
+ default_auto_field = 'django.db.models.BigAutoField'
6
+ name = 'translate_projects_django'
@@ -0,0 +1,34 @@
1
+ import hashlib
2
+
3
+ def base36_encode(number: int) -> str:
4
+ """
5
+ Encodes a number into a Base36 string.
6
+ Args:
7
+ number (int): The number to encode.
8
+ Returns:
9
+ str: The Base36 encoded string.
10
+ """
11
+ chars = "0123456789abcdefghijklmnopqrstuvwxyz"
12
+ result = ""
13
+ while number:
14
+ number, i = divmod(number, 36)
15
+ result = chars[i] + result
16
+ return result or "0"
17
+
18
+ def generate_short_hash(text: str, length: int = 15) -> str:
19
+ """
20
+ Generates a short hash for a given text using SHA-256 and Base36 encoding.
21
+ Args:
22
+ text (str): The text to hash.
23
+ length (int): The length of the generated hash (default is 15).
24
+ Returns:
25
+ str: The short Base36 hash.
26
+ """
27
+ # Generate SHA-256 hash from the text
28
+ hash_bytes = hashlib.sha256(text.encode()).digest()
29
+ # Convert the hash to an integer
30
+ hash_int = int.from_bytes(hash_bytes, 'big')
31
+ # Convert the integer to Base36
32
+ base36_hash = base36_encode(hash_int)
33
+ # Return a shorter portion of the hash
34
+ return base36_hash[:length] # Adjust the length here if needed
@@ -0,0 +1,61 @@
1
+ from django.core.management import call_command
2
+ from .translation_utils import generate_new_file_po
3
+ from .api_utils import get_translations_from_api
4
+ from .hash_utils import generate_short_hash
5
+ from django.conf import settings
6
+ import polib
7
+ import os
8
+
9
+ def make_recreate_json(original_translations, data_result_translations):
10
+ recreate_json = {}
11
+ for key, value in original_translations.items():
12
+ simple_hash = generate_short_hash(key)
13
+
14
+ if simple_hash in data_result_translations.keys():
15
+ recreate_json[key] = data_result_translations[simple_hash]
16
+ continue
17
+ return recreate_json
18
+
19
+ def generate_translations(self, target_lang, locale_dir):
20
+ default_language = settings.LANGUAGE_CODE
21
+
22
+ # Start
23
+ self.stdout.write(self.style.SUCCESS(f"\nšŸš€ Starting translation generation for language: {target_lang}"))
24
+
25
+ # Step 1: Generate files .po
26
+ self.stdout.write(self.style.WARNING(f" Step 1: Generating .po file for '{target_lang}'..."))
27
+
28
+ call_command('makemessages', locale=[target_lang], verbosity=0)
29
+
30
+ self.stdout.write(self.style.SUCCESS(f" āœ” .po file generated for '{target_lang}'"))
31
+
32
+ # Step 2: Read File .po
33
+ po_file_path = os.path.join(locale_dir, default_language, 'LC_MESSAGES', 'django.po')
34
+
35
+ po = polib.pofile(po_file_path)
36
+ self.stdout.write(self.style.SUCCESS(f" āœ” .po file read successfully (entries: {len(po)}))"))
37
+
38
+ original_translations = {entry.msgid: entry.msgstr for entry in po}
39
+
40
+ translations = {
41
+ generate_short_hash(entry.msgid): entry.msgid
42
+ for entry in po
43
+ }
44
+
45
+ self.stdout.write(self.style.SUCCESS(f" āœ” Hashed translations generated (total: {len(translations)}))"))
46
+
47
+ # Step 2: Get translations from API
48
+ self.stdout.write(self.style.WARNING(f" Step 2: get translations for '{target_lang}'..."))
49
+ data_result_translations = get_translations_from_api(translations, default_language, target_lang, f"{default_language}-django.po")
50
+ self.stdout.write(self.style.SUCCESS(f" āœ” Translations get (received: {len(data_result_translations)}))"))
51
+
52
+ # Step 5: Recreate translations
53
+ recreate_json = make_recreate_json(original_translations, data_result_translations)
54
+
55
+ # Paso 3: Generate new translations file
56
+ self.stdout.write(self.style.WARNING(f" Step 3: Generating new translations file for '{target_lang}'..."))
57
+ generate_new_file_po(po, target_lang, recreate_json, locale_dir)
58
+ self.stdout.write(self.style.SUCCESS(f" āœ” New .po file generated for '{target_lang}'"))
59
+
60
+ # Finish
61
+ self.stdout.write(self.style.SUCCESS(f"\nšŸŽ‰ Successfully completed translation generation for '{target_lang}' āœ…\n"))
@@ -0,0 +1,64 @@
1
+ from django.core.management.base import BaseCommand
2
+ from django.conf import settings
3
+ import subprocess
4
+ from django.core.management import call_command
5
+ import polib
6
+ import json
7
+ import os
8
+ import requests
9
+ from translate_projects_django.manage_translations import generate_translations
10
+
11
+ class Command(BaseCommand):
12
+ help = "Translate the project for a specific language or all languages."
13
+
14
+ def add_arguments(self, parser):
15
+ # Add an optional argument for the locale
16
+ parser.add_argument(
17
+ '--locale',
18
+ type=str,
19
+ help='Specific language to translate, e.g., "ko" for Korean.'
20
+ )
21
+
22
+ def handle(self, *args, **kwargs):
23
+ # Get the i18n configurations
24
+ languages = settings.LANGUAGES
25
+ default_language = settings.LANGUAGE_CODE
26
+ locale_dir = settings.LOCALE_PATHS[0]
27
+
28
+ # Get the locale argument
29
+ locale_param = kwargs.get('locale')
30
+
31
+ # Generate translations for the default language ('es' in this case)
32
+ generate_translations(self, 'es', locale_dir)
33
+
34
+ if locale_param:
35
+ # Check if the provided locale is valid
36
+ if locale_param not in dict(languages):
37
+ self.stdout.write(self.style.ERROR(f"The language '{locale_param}' is not valid."))
38
+ self.stdout.write(self.style.ERROR(f"Available languages: {', '.join([lang[0] for lang in languages])}"))
39
+ self.stdout.write(self.style.ERROR(f"You can add the language in the settings.py file under LANGUAGES=[...]"))
40
+ return
41
+
42
+ # Warn if the locale is the same as the default language
43
+ if locale_param == default_language:
44
+ self.stdout.write(self.style.WARNING(f"The language '{locale_param}' is not translated, as the default language '{default_language}' has already been translated."))
45
+ return
46
+
47
+ # Translate for the specific locale
48
+ self.stdout.write(self.style.SUCCESS(f"\nšŸš€ Translating for language: {locale_param}"))
49
+ generate_translations(self, locale_param, locale_dir)
50
+ self.stdout.write(self.style.SUCCESS(f"\nšŸŽ‰ Translation generation for '{locale_param}' completed successfully āœ…\n"))
51
+
52
+ # Compile messages
53
+ self.stdout.write(self.style.WARNING(f"\nšŸ›  Compiling messages..."))
54
+ call_command('compilemessages')
55
+ return
56
+
57
+ # If no specific locale is passed, generate translations for all languages except the default language
58
+ for code, name in languages:
59
+ if code != default_language:
60
+ generate_translations(self, code, locale_dir)
61
+
62
+ # Compile messages after translations are generated for all languages
63
+ self.stdout.write(self.style.WARNING(f"\nšŸ›  Compiling messages for all languages..."))
64
+ call_command('compilemessages')
@@ -0,0 +1,31 @@
1
+ import polib
2
+ import os
3
+
4
+ def generate_new_file_po(current_po, language, recreate_json, locale_dir):
5
+ # Create a new .po file
6
+ new_po = polib.POFile()
7
+
8
+ # Add metadata to the .po file
9
+ new_po.metadata = {
10
+ 'Project-Id-Version': '1.0',
11
+ 'Content-Type': 'text/plain; charset=utf-8',
12
+ 'Content-Transfer-Encoding': '8bit',
13
+ 'Language': language,
14
+ }
15
+
16
+ # Copy entries from the original file
17
+ new_po.extend(current_po)
18
+
19
+ # Update msgstr of entries with the data from recreate_json
20
+ for entry in current_po:
21
+ if entry.msgid in recreate_json:
22
+ entry.msgstr = recreate_json[entry.msgid]
23
+
24
+ # Create the necessary directories for the new file
25
+ new_po_file_path = os.path.join(locale_dir, language, 'LC_MESSAGES', 'django.po')
26
+ os.makedirs(os.path.dirname(new_po_file_path), exist_ok=True)
27
+
28
+ # Save the .po file with the updates
29
+ new_po.save(new_po_file_path)
30
+
31
+ return new_po
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.1
2
+ Name: translate-projects-django
3
+ Version: 0.0.2
4
+ Summary: Translate your django templates easily
5
+ Home-page: https://github.com/translate-projects/translate-projects-django
6
+ Author: Neider Ruiz
7
+ Author-email: contact@neiderruiz.com
8
+ License: MIT
9
+ Platform: UNKNOWN
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.7
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Framework :: Django
19
+ Classifier: Framework :: Django :: 3.0
20
+ Classifier: Framework :: Django :: 3.1
21
+ Classifier: Framework :: Django :: 3.2
22
+ Requires-Python: >=3.7
23
+ Description-Content-Type: text/markdown
24
+
25
+ # Translate Projects Django
26
+
27
+ Effortlessly manage and automate the translation of your Django-based projects with Translate Projects Django. This powerful package streamlines the process of translating your Django templates and messages, making it easier than ever to reach a global audience. Whether you're working on an open-source project or a private enterprise application, Translate Projects Django ensures that your application is ready for internationalization with minimal effort.
28
+
29
+ [See documentation](https://translateprojects.neiderruiz.com/docs/python/django)
30
+
31
+ ## Features
32
+
33
+ - **Easy Integration**: Seamlessly integrate with your existing Docusaurus project.
34
+ - **Multi-language Support**: Translate your documentation into multiple languages effortlessly.
35
+ - **Customizable**: Tailor the translation process to fit your project's needs.
36
+ - **Open Source**: Free to use and open to contributions.
37
+
38
+
39
+ ## Support
40
+
41
+ If you have any questions, need help, or want to contribute, feel free to reach out:
42
+
43
+ - **Website**: [neiderruiz.com](https://neiderruiz.com)
44
+ - **GitHub**: [@neiderruiz](https://github.com/neiderruiz)
45
+ - **Twitter**: [@neiderruiz](https://x.com/neiderruiz_)
46
+ - **YouTube**: [Neider Ruiz](https://youtube.com/@neiderruiz)
47
+
48
+
49
+ ## Support the Project
50
+
51
+ If you find this project useful, consider buying me a coffee to help me keep improving it. Your support means a lot! ā˜•
52
+
53
+ [![Buy me a coffee](https://img.buymeacoffee.com/button-api/?text=Buy%20me%20a%20coffee&emoji=ā˜•&slug=neiderruiz&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff)](https://www.buymeacoffee.com/neiderruiz)
54
+
@@ -0,0 +1,19 @@
1
+ MANIFEST.in
2
+ README.md
3
+ pyproject.toml
4
+ setup.py
5
+ translate_projects_django/__init__.py
6
+ translate_projects_django/api_utils.py
7
+ translate_projects_django/apps.py
8
+ translate_projects_django/hash_utils.py
9
+ translate_projects_django/manage_translations.py
10
+ translate_projects_django/translation_utils.py
11
+ translate_projects_django.egg-info/PKG-INFO
12
+ translate_projects_django.egg-info/SOURCES.txt
13
+ translate_projects_django.egg-info/dependency_links.txt
14
+ translate_projects_django.egg-info/requires.txt
15
+ translate_projects_django.egg-info/top_level.txt
16
+ translate_projects_django/management/__init__.py
17
+ translate_projects_django/management/commands/__init__.py
18
+ translate_projects_django/management/commands/translate.py
19
+ translate_projects_django/migrations/__init__.py
@@ -0,0 +1,2 @@
1
+ Django>=3.0
2
+ polib>=1.2.0
@@ -0,0 +1 @@
1
+ translate_projects_django