├── .gitignore ├── AUTHORS ├── CHANGES.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── categories_i18n ├── __init__.py ├── abstract_models.py ├── admin.py ├── apps.py ├── locale │ ├── zh-hans │ │ └── LC_MESSAGES │ │ │ └── django.po │ └── zh_Hans ├── managers.py ├── migrations │ ├── 0001_initial.py │ └── __init__.py ├── models.py ├── templates │ └── categories_i18n │ │ ├── base.html │ │ ├── category_detail.html │ │ └── category_list.html ├── urls.py └── views.py ├── example ├── .gitignore ├── README.rst ├── __init__.py ├── manage.py ├── requirements.txt ├── settings.py ├── templates │ └── base.html └── urls.py ├── pyproject.toml ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.pyo 3 | *.mo 4 | *.db 5 | *.egg-info/ 6 | *.egg/ 7 | .coverage 8 | .project 9 | .idea/ 10 | .pydevproject 11 | .idea/workspace.xml 12 | .tox/ 13 | .DS_Store 14 | build/ 15 | dist/ 16 | docs/_build/ 17 | htmlcov/ 18 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Original authors: 2 | 3 | * Diederik van der Boor 4 | -------------------------------------------------------------------------------- /CHANGES.rst: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | Version 2.0 (2021-11-17) 5 | ------------------------ 6 | 7 | * Added Django 4.0 support 8 | * Dropped Python 2.7 support. 9 | * Dropped Django 1.11 support. 10 | 11 | 12 | Version 1.1.1 (2020-01-05) 13 | -------------------------- 14 | 15 | * Fixed Django 3.0 support by replacing ``python_2_unicode_compatible`` import. 16 | 17 | 18 | Version 1.1 (2018-01-22) 19 | ------------------------ 20 | 21 | * Added Django 2.0 support. 22 | * Added complete Simplified Chinese Translations 23 | * Set ``mptt_indent_field`` explicitly for proper MPTT list columns 24 | * Dropped south and Django 1.4 / 1.5 / 1.6 compatibility. 25 | 26 | 27 | Version 1.0 (2014-08-31) 28 | ------------------------ 29 | 30 | * First release 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS 2 | include README.rst 3 | include LICENSE 4 | global-exclude .DS_Store 5 | global-exclude Thumbs.db 6 | global-exclude Desktop.ini 7 | global-exclude *.swp 8 | global-exclude *~ 9 | global-exclude *.bak 10 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | django-categories-i18n 2 | ====================== 3 | 4 | A simple categories model that supports translations. 5 | Improvements are welcome! 6 | 7 | 8 | Installation 9 | ============ 10 | 11 | First install the module, preferably in a virtual environment. 12 | It can be installed from PyPI: 13 | 14 | .. code-block:: bash 15 | 16 | pip install django-categories-i18n 17 | 18 | Add ``categories_i18n`` to your ``INSTALLED_APPS``: 19 | 20 | .. code-block:: python 21 | 22 | INSTALLED_APPS += ( 23 | 'categories_i18n', 24 | ) 25 | 26 | 27 | Usage 28 | ===== 29 | 30 | * Link to the ``categories_i18n.models.Category`` model in your application. 31 | 32 | 33 | Improving this package 34 | ====================== 35 | 36 | This module is designed to be usable for other projects too. 37 | In case there is anything you didn't like about it, 38 | or think it's not flexible enough, please let us know. 39 | We'd love to improve it! Pull requests are welcome too. :-) 40 | -------------------------------------------------------------------------------- /categories_i18n/__init__.py: -------------------------------------------------------------------------------- 1 | # follow PEP440 2 | __version__ = "2.0" 3 | 4 | default_app_config = "categories_i18n.apps.CategoryAppConfig" 5 | -------------------------------------------------------------------------------- /categories_i18n/abstract_models.py: -------------------------------------------------------------------------------- 1 | from django.contrib.sites.models import Site 2 | from django.db import models 3 | from django.urls import reverse 4 | from django.utils.translation import gettext_lazy as _ 5 | from mptt.fields import TreeForeignKey 6 | from mptt.models import MPTTModel 7 | from parler.fields import TranslatedField 8 | from parler.models import TranslatableModel, TranslatedFieldsModel 9 | from parler.utils.context import switch_language 10 | 11 | from .managers import CategoryManager 12 | 13 | 14 | def _get_current_site(): 15 | return Site.objects.get_current().pk 16 | 17 | 18 | class AbstractCategory(MPTTModel, TranslatableModel): 19 | """ 20 | Base class for categories. 21 | """ 22 | 23 | title = TranslatedField(any_language=True) 24 | slug = TranslatedField() # Explicitly added, but not needed 25 | 26 | parent = TreeForeignKey( 27 | "self", 28 | on_delete=models.CASCADE, 29 | blank=True, 30 | null=True, 31 | related_name="children", 32 | verbose_name=_("Parent"), 33 | ) 34 | site = models.ForeignKey( 35 | Site, 36 | on_delete=models.CASCADE, 37 | editable=False, 38 | blank=True, 39 | null=True, 40 | default=_get_current_site, 41 | ) 42 | 43 | objects = CategoryManager() 44 | 45 | class Meta: 46 | abstract = True 47 | ordering = ("tree_id", "lft") 48 | 49 | def __str__(self): 50 | return self.safe_translation_getter("title", any_language=True) 51 | 52 | def get_absolute_url(self): 53 | # Make sure i18n_patterns() generate the proper URL, when used. 54 | with switch_language(self): 55 | return reverse("category_detail", kwargs={"slug": self.slug}) 56 | 57 | 58 | class AbstractCategoryTranslation(TranslatedFieldsModel): 59 | """ 60 | Base class for translated fields. 61 | """ 62 | 63 | title = models.CharField(_("title"), max_length=255) 64 | slug = models.SlugField( 65 | _("slug"), 66 | max_length=100, 67 | help_text=_("The slug is used in the URL of the page"), 68 | ) 69 | 70 | #: The ForeignKey to the shared model. Needs to be defined in the concrete class. 71 | master = None 72 | 73 | class Meta: 74 | abstract = True 75 | unique_together = [("language_code", "slug")] 76 | -------------------------------------------------------------------------------- /categories_i18n/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from mptt.admin import MPTTModelAdmin 3 | from mptt.forms import MPTTAdminForm 4 | from parler.admin import TranslatableAdmin 5 | from parler.forms import TranslatableModelForm 6 | 7 | from .models import Category 8 | 9 | 10 | class CategoryAdminForm(MPTTAdminForm, TranslatableModelForm): 11 | """ 12 | Form for categories, both MPTT + translatable. 13 | """ 14 | 15 | pass 16 | 17 | 18 | class CategoryAdmin(MPTTModelAdmin, TranslatableAdmin): 19 | """ 20 | Admin page for categories. 21 | """ 22 | 23 | list_display = ("title", "slug") 24 | mptt_indent_field = "title" # be explicit for MPTT 25 | search_fields = ("translations__title", "translations__slug") 26 | form = CategoryAdminForm 27 | 28 | fieldsets = [(None, {"fields": ("title", "slug", "parent")})] 29 | 30 | def get_prepopulated_fields(self, request, obj=None): 31 | # Needed for django-parler 32 | return {"slug": ("title",)} 33 | 34 | 35 | admin.site.register(Category, CategoryAdmin) 36 | -------------------------------------------------------------------------------- /categories_i18n/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | from django.utils.translation import gettext_lazy as _ 3 | 4 | 5 | class CategoryAppConfig(AppConfig): 6 | """ 7 | Application config for a better Django admin label. 8 | """ 9 | 10 | name = "categories_i18n" 11 | verbose_name = _("Categories") 12 | -------------------------------------------------------------------------------- /categories_i18n/locale/zh-hans/LC_MESSAGES/django.po: -------------------------------------------------------------------------------- 1 | # Simplified Chinese Translations for Django Categories 2 | # Copyright (C) 2015 3 | # This file is distributed under the same license as the django-categories-i18n package. 4 | # Kuiyu CHANG , 2015 5 | # 6 | #, fuzzy 7 | msgid "" 8 | msgstr "" 9 | "Project-Id-Version: 1.0\n" 10 | "Report-Msgid-Bugs-To: \n" 11 | "POT-Creation-Date: 2015-10-29 11:39+0800\n" 12 | "PO-Revision-Date: 2015-10-29 11:39+0800\n" 13 | "Last-Translator: Kuiyu CHANG \n" 14 | "Language-Team: Mosuma Corporation \n" 15 | "Language: \n" 16 | "MIME-Version: 1.0\n" 17 | "Content-Type: text/plain; charset=UTF-8\n" 18 | "Content-Transfer-Encoding: 8bit\n" 19 | 20 | #: abstract_models.py:27 21 | msgid "Parent" 22 | msgstr "父母" 23 | 24 | #: abstract_models.py:50 25 | msgid "title" 26 | msgstr "标题" 27 | 28 | #: abstract_models.py:51 29 | msgid "slug" 30 | msgstr "缩写" 31 | 32 | #: abstract_models.py:51 33 | msgid "The slug is used in the URL of the page" 34 | msgstr "此缩写将构成网址部分" 35 | 36 | #: apps.py:10 models.py:12 37 | msgid "Categories" 38 | msgstr "类别" 39 | 40 | #: models.py:11 41 | msgid "Category" 42 | msgstr "类别" 43 | 44 | #: models.py:25 45 | msgid "Category Translation" 46 | msgstr "类别翻译" 47 | 48 | #: models.py:26 49 | msgid "Category Translations" 50 | msgstr "类别翻译" 51 | -------------------------------------------------------------------------------- /categories_i18n/locale/zh_Hans: -------------------------------------------------------------------------------- 1 | zh-hans -------------------------------------------------------------------------------- /categories_i18n/managers.py: -------------------------------------------------------------------------------- 1 | """ 2 | The manager classes. 3 | """ 4 | import django 5 | from django.db.models.query import QuerySet 6 | from mptt.managers import TreeManager 7 | from mptt.querysets import TreeQuerySet 8 | from parler.managers import TranslatableManager, TranslatableQuerySet 9 | 10 | 11 | class CategoryQuerySet(TranslatableQuerySet, TreeQuerySet): 12 | """ 13 | The Queryset methods for the Category model. 14 | """ 15 | 16 | def as_manager(cls): 17 | # Make sure the Django way of creating managers works. 18 | manager = CategoryManager.from_queryset(cls)() 19 | manager._built_with_as_manager = True 20 | return manager 21 | 22 | as_manager.queryset_only = True 23 | as_manager = classmethod(as_manager) 24 | 25 | 26 | class CategoryManager(TreeManager, TranslatableManager): 27 | """ 28 | Base manager class for the categories. 29 | """ 30 | 31 | _queryset_class = CategoryQuerySet 32 | -------------------------------------------------------------------------------- /categories_i18n/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | import mptt.fields 2 | from django.db import migrations, models 3 | 4 | import categories_i18n.abstract_models 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ("sites", "0001_initial"), 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name="Category", 16 | fields=[ 17 | ( 18 | "id", 19 | models.AutoField( 20 | verbose_name="ID", 21 | serialize=False, 22 | auto_created=True, 23 | primary_key=True, 24 | ), 25 | ), 26 | ("lft", models.PositiveIntegerField(editable=False, db_index=True)), 27 | ("rght", models.PositiveIntegerField(editable=False, db_index=True)), 28 | ("tree_id", models.PositiveIntegerField(editable=False, db_index=True)), 29 | ("level", models.PositiveIntegerField(editable=False, db_index=True)), 30 | ( 31 | "parent", 32 | mptt.fields.TreeForeignKey( 33 | related_name="children", 34 | verbose_name="Parent", 35 | blank=True, 36 | to="categories_i18n.Category", 37 | on_delete=models.CASCADE, 38 | null=True, 39 | ), 40 | ), 41 | ( 42 | "site", 43 | models.ForeignKey( 44 | default=categories_i18n.abstract_models._get_current_site, 45 | blank=True, 46 | editable=False, 47 | to="sites.Site", 48 | on_delete=models.CASCADE, 49 | null=True, 50 | ), 51 | ), 52 | ], 53 | options={ 54 | "ordering": ("tree_id", "lft"), 55 | "verbose_name": "Category", 56 | "verbose_name_plural": "Categories", 57 | }, 58 | ), 59 | migrations.CreateModel( 60 | name="CategoryTranslation", 61 | fields=[ 62 | ( 63 | "id", 64 | models.AutoField( 65 | verbose_name="ID", 66 | serialize=False, 67 | auto_created=True, 68 | primary_key=True, 69 | ), 70 | ), 71 | ( 72 | "language_code", 73 | models.CharField(max_length=15, verbose_name="Language", db_index=True), 74 | ), 75 | ("title", models.CharField(max_length=255, verbose_name="title")), 76 | ( 77 | "slug", 78 | models.SlugField( 79 | help_text="The slug is used in the URL of the page", 80 | max_length=100, 81 | verbose_name="slug", 82 | ), 83 | ), 84 | ( 85 | "master", 86 | models.ForeignKey( 87 | related_name="translations", 88 | to="categories_i18n.Category", 89 | on_delete=models.CASCADE, 90 | ), 91 | ), 92 | ], 93 | options={ 94 | "verbose_name": "Category Translation", 95 | "verbose_name_plural": "Category Translations", 96 | }, 97 | ), 98 | ] 99 | -------------------------------------------------------------------------------- /categories_i18n/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/django-parler/django-categories-i18n/1226fd9f8110bbe99ecb670cf936fb8d67a78f80/categories_i18n/migrations/__init__.py -------------------------------------------------------------------------------- /categories_i18n/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.utils.translation import gettext_lazy as _ 3 | 4 | from categories_i18n.abstract_models import AbstractCategory, AbstractCategoryTranslation 5 | 6 | 7 | class Category(AbstractCategory): 8 | """ 9 | The category model. 10 | """ 11 | 12 | class Meta: 13 | verbose_name = _("Category") 14 | verbose_name_plural = _("Categories") 15 | ordering = ("tree_id", "lft") 16 | 17 | 18 | class CategoryTranslation(AbstractCategoryTranslation): 19 | """ 20 | The translated fields of the category. 21 | By using this model instead of the ``parler.models.TranslatedFields(..)`` construct, 22 | the translatable fields can be defined in an abstract model. 23 | """ 24 | 25 | master = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="translations") 26 | 27 | class Meta: 28 | verbose_name = _("Category Translation") 29 | verbose_name_plural = _("Category Translations") 30 | -------------------------------------------------------------------------------- /categories_i18n/templates/categories_i18n/base.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %}{# Define "base.html" in your site! #} 2 | -------------------------------------------------------------------------------- /categories_i18n/templates/categories_i18n/category_detail.html: -------------------------------------------------------------------------------- 1 | {% extends "categories_i18n/base.html" %} 2 | {% comment %} 3 | 4 | This is a very basic template. 5 | You can replace this in your own projects, to have a better layout. 6 | 7 | {% endcomment %} 8 | 9 | {% block title %}{{ object.title }}{% endblock %} 10 | 11 | {% block content %} 12 | All Categories -> {{ object }} 13 | {% endblock %} 14 | -------------------------------------------------------------------------------- /categories_i18n/templates/categories_i18n/category_list.html: -------------------------------------------------------------------------------- 1 | {% extends "categories_i18n/base.html" %} 2 | {% comment %} 3 | 4 | This is a very basic template. 5 | You can replace this in your own projects, to have a better layout. 6 | 7 | {% endcomment %} 8 | 9 | {% block content %} 10 | {% if object_list %} 11 |
    12 | {% for object in object_list %} 13 |
  • {{ object }}
  • 14 | {% endfor %} 15 |
16 | {% endif %} 17 | {% endblock %} 18 | 19 | -------------------------------------------------------------------------------- /categories_i18n/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path, re_path 2 | 3 | from . import views 4 | 5 | urlpatterns = [ 6 | path("", views.CategoryListView.as_view(), name="category_list"), 7 | re_path( 8 | r"^(?P[-_\w]+)/$", 9 | views.CategoryDetailView.as_view(), 10 | name="category_detail", 11 | ), 12 | ] 13 | -------------------------------------------------------------------------------- /categories_i18n/views.py: -------------------------------------------------------------------------------- 1 | from django.views.generic import DetailView, ListView 2 | from parler.views import TranslatableSlugMixin 3 | 4 | from .models import Category 5 | 6 | 7 | class CategoryListView(ListView): 8 | """ 9 | Category list view. 10 | """ 11 | 12 | model = Category 13 | 14 | 15 | class CategoryDetailView(TranslatableSlugMixin, DetailView): 16 | """ 17 | Category detail view 18 | """ 19 | 20 | model = Category 21 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | media/ 2 | static/ 3 | *.sqlite3 4 | -------------------------------------------------------------------------------- /example/README.rst: -------------------------------------------------------------------------------- 1 | Example 2 | ======= 3 | 4 | To run the example application, make sure you have the required 5 | packages installed. You can do this using following commands : 6 | 7 | .. code-block:: bash 8 | 9 | mkvirtualenv example 10 | pip install -r example/requirements.txt 11 | 12 | This assumes you already have ``virtualenv`` and ``virtualenvwrapper`` 13 | installed and configured. 14 | 15 | Next, you can setup the django instance using : 16 | 17 | .. code-block:: bash 18 | 19 | python example/manage.py syncdb --noinput 20 | python example/manage.py createsuperuser --username=admin --email=admin@example.com 21 | 22 | And run it : 23 | 24 | .. code-block:: bash 25 | 26 | python example/manage.py runserver 27 | 28 | Good luck! 29 | -------------------------------------------------------------------------------- /example/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/django-parler/django-categories-i18n/1226fd9f8110bbe99ecb670cf936fb8d67a78f80/example/__init__.py -------------------------------------------------------------------------------- /example/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | # Allow starting the app without installing the module. 11 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) 12 | 13 | execute_from_command_line(sys.argv) 14 | -------------------------------------------------------------------------------- /example/requirements.txt: -------------------------------------------------------------------------------- 1 | Django >= 1.6 2 | South >= 1.0 3 | django-parler >= 1.5 4 | django-mptt >= 0.6 5 | -------------------------------------------------------------------------------- /example/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for app project. 3 | 4 | For more information on this file, see 5 | https://docs.djangoproject.com/en/1.7/topics/settings/ 6 | 7 | For the full list of settings and their values, see 8 | https://docs.djangoproject.com/en/1.7/ref/settings/ 9 | """ 10 | 11 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 12 | import os 13 | import re 14 | 15 | BASE_DIR = os.path.dirname(__file__) 16 | 17 | 18 | # Quick-start development settings - unsuitable for production 19 | # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ 20 | 21 | # SECURITY WARNING: keep the secret key used in production secret! 22 | SECRET_KEY = "YOUR_SECRET_KEY" 23 | 24 | # SECURITY WARNING: don't run with debug turned on in production! 25 | DEBUG = True 26 | 27 | TEMPLATE_DEBUG = True 28 | 29 | SERVE_MEDIA = True 30 | 31 | ALLOWED_HOSTS = [] 32 | 33 | # Application definition 34 | 35 | INSTALLED_APPS = [ 36 | "django.contrib.auth", 37 | "django.contrib.sites", 38 | "django.contrib.sessions", 39 | "django.contrib.staticfiles", 40 | "django.contrib.contenttypes", 41 | "django.contrib.messages", 42 | "django.contrib.admin", 43 | "mptt", 44 | "parler", 45 | "categories_i18n", 46 | ] 47 | 48 | MIDDLEWARE_CLASSES = MIDDLEWARE = [ 49 | "django.contrib.sessions.middleware.SessionMiddleware", 50 | "django.middleware.common.CommonMiddleware", 51 | "django.middleware.csrf.CsrfViewMiddleware", 52 | "django.contrib.auth.middleware.AuthenticationMiddleware", 53 | "django.contrib.messages.middleware.MessageMiddleware", 54 | "django.middleware.clickjacking.XFrameOptionsMiddleware", 55 | ] 56 | 57 | TEMPLATE_DIRS = (os.path.join(BASE_DIR, "templates"),) 58 | 59 | TEMPLATE_CONTEXT_PROCESSORS = [ 60 | "django.core.context_processors.tz", 61 | "django.core.context_processors.i18n", 62 | "django.core.context_processors.debug", 63 | "django.core.context_processors.static", 64 | "django.core.context_processors.request", 65 | "django.contrib.auth.context_processors.auth", 66 | "django.contrib.messages.context_processors.messages", 67 | ] 68 | 69 | ROOT_URLCONF = "example.urls" 70 | 71 | SITE_ID = 1 72 | 73 | # Database 74 | # https://docs.djangoproject.com/en/1.7/ref/settings/#databases 75 | 76 | DATABASES = { 77 | "default": { 78 | "ENGINE": "django.db.backends.sqlite3", 79 | "NAME": os.path.join(BASE_DIR, "db.sqlite3"), 80 | } 81 | } 82 | 83 | # Internationalization 84 | # https://docs.djangoproject.com/en/1.7/topics/i18n/ 85 | 86 | LANGUAGE_CODE = "en-us" 87 | 88 | TIME_ZONE = "UTC" 89 | 90 | USE_I18N = True 91 | 92 | USE_L10N = True 93 | 94 | USE_TZ = True 95 | 96 | # Static files (CSS, JavaScript, Images) 97 | # https://docs.djangoproject.com/en/1.8/howto/static-files/ 98 | 99 | # Absolute filesystem path to the directory that will hold user-uploaded files. 100 | MEDIA_ROOT = os.path.join(BASE_DIR, "media") 101 | 102 | # URL that handles the media served from MEDIA_ROOT. Make sure to use a 103 | # trailing slash. 104 | MEDIA_URL = "/media/" 105 | 106 | # Absolute path to the directory static files should be collected to. 107 | STATIC_ROOT = os.path.join(BASE_DIR, "static") 108 | 109 | STATIC_URL = "/static/" 110 | -------------------------------------------------------------------------------- /example/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {% block title %}-{% endblock %} 6 | 7 | 8 | 9 | {% block content %}{% endblock %} 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /example/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import include, path 3 | 4 | admin.autodiscover() 5 | 6 | urlpatterns = [ 7 | path("admin/", include(admin.site.urls)), 8 | path("categories/", include("categories_i18n.urls")), 9 | ] 10 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.isort] 2 | profile = "black" 3 | line_length = 99 4 | 5 | [tool.black] 6 | line-length = 99 7 | exclude = ''' 8 | /( 9 | \.git 10 | | \.venv 11 | | \.tox 12 | | dist 13 | )/ 14 | ''' 15 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | # create "py2.py3-none-any.whl" package 3 | universal = 1 4 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import codecs 3 | import os 4 | import re 5 | import sys 6 | from os import path 7 | 8 | from setuptools import find_packages, setup 9 | 10 | 11 | def read(*parts): 12 | file_path = path.join(path.dirname(__file__), *parts) 13 | return codecs.open(file_path, encoding="utf-8").read() 14 | 15 | 16 | def find_version(*parts): 17 | version_file = read(*parts) 18 | version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M) 19 | if version_match: 20 | return str(version_match.group(1)) 21 | raise RuntimeError("Unable to find version string.") 22 | 23 | 24 | setup( 25 | name="django-categories-i18n", 26 | version=find_version("categories_i18n", "__init__.py"), 27 | license="Apache 2.0", 28 | install_requires=[ 29 | "django-mptt>=0.9.1", 30 | "django-parler>=2.0.1", 31 | ], 32 | requires=[ 33 | "Django (>=2.2)", 34 | ], 35 | description="Translatable Categories model", 36 | long_description=read("README.rst"), 37 | author="Diederik van der Boor", 38 | author_email="opensource@edoburu.nl", 39 | url="https://github.com/django-parler/django-categories-i18n", 40 | download_url="https://github.com/django-parler/django-categories-i18n/zipball/master", 41 | packages=find_packages(exclude=("example*",)), 42 | include_package_data=True, 43 | zip_safe=False, 44 | classifiers=[ 45 | "Development Status :: 5 - Production/Stable", 46 | "Environment :: Web Environment", 47 | "Intended Audience :: Developers", 48 | "License :: OSI Approved :: Apache Software License", 49 | "Operating System :: OS Independent", 50 | "Programming Language :: Python", 51 | "Programming Language :: Python :: 3.5", 52 | "Programming Language :: Python :: 3.6", 53 | "Programming Language :: Python :: 3.7", 54 | "Programming Language :: Python :: 3.8", 55 | "Programming Language :: Python :: 3.9", 56 | "Programming Language :: Python :: 3.10", 57 | "Framework :: Django", 58 | "Framework :: Django :: 2.0", 59 | "Framework :: Django :: 2.1", 60 | "Framework :: Django :: 2.2", 61 | "Framework :: Django :: 3.0", 62 | "Framework :: Django :: 3.1", 63 | "Framework :: Django :: 3.2", 64 | "Framework :: Django :: 4.0", 65 | "Topic :: Internet :: WWW/HTTP", 66 | "Topic :: Software Development :: Libraries :: Python Modules", 67 | ], 68 | ) 69 | --------------------------------------------------------------------------------