Package: django-restful-translator
django_restful_translatordjango_restful_translatoris a Django library designed to enhance the internationalization of Django models by providing tools for managing and serving model translations. It integrates seamlessly with the Django REST Framework, offering model translation without altering original model values, and supports automatic translation through external services.Functionalitydjango_restful_translatorprovides:Model translations: TheTranslatableModelmixin adds translation capabilities to your models, allowing you to specify which fields should be translatable, while leaving the original field values intact.Translation management: Admin inlines for managing translations from the Django Admin site.REST API support: Provided serializers (TranslatableDBSerializer,TranslatableDBDictSerializer,TranslatableGettextSerializer,TranslatableGettextDictSerializer) ensure translated fields are properly serialized in API responses.Translation synchronization: Commandsdrt_makemessagesanddrt_update_databaseexport translations to.pofiles and import them back into the database, keeping translations synchronized across different environments.Automatic Translation: The library supports automatic translation of model fields through popular translation providers like Google Translate v2, v3, AWS Translate and Deepl. The feature is configurable and can be run via an admin command.AdvantagesPreserves original values:django_restful_translatorstores all translations in a separate model, ensuring that your original model's values remain unchanged.Ease of use: It integrates seamlessly with the Django Admin interface for easy management of translations.Flexibility: TheTranslatableModelmixin can be added to any Django model with specified fields to translate.Efficiency: By storing translations in the database and only translating fields when necessary,django_restful_translatorminimizes unnecessary work.Integration with Django REST framework: If you're using Django REST framework,django_restful_translatoris designed to work with it out of the box.InstallationUse the package managerpipto install django_restful_translator.pipinstalldjango-restful-translatorConfigurationIn your settings file, add the middleware for handling language preferences:MIDDLEWARE=[...'django.middleware.locale.LocaleMiddleware',...]In your settings file, adddjango_restful_translatorto instaled apps:INSTALLED_APPS=[...'django_restful_translator',...]Set up your languages and locale paths. Make sure to include both the standard locale directory and the one for django_restful_translator:LOCALE_PATHS=(BASE_DIR/"locale",BASE_DIR/"drt_locale",)LANGUAGES=(("en","English"),("sv","Swedish"),)LANGUAGE_CODE="en"MigrationsAfter setting up, you need to create the necessary database tables. You can do this by running migrations:pythonmanage.pymigrateUsageImport theTranslatableModelin your models.py and use it as a base class for any model you want to be translatable.fromdjango_restful_translator.modelsimportTranslatableModelclassYourModel(TranslatableModel):...Add your translatable fields to thetranslatable_fieldsattribute.classYourModel(TranslatableModel):title=models.CharField(max_length=100)description=models.TextField()translatable_fields=['title','description']Use the provided serializers in your API.fromdjango_restful_translator.drf.serializersimportTranslatableDBSerializerclassYourModelSerializer(TranslatableDBSerializer):...To enable managing translations in the Django admin, addTranslationInlineto your model admin.fromdjango_restful_translator.adminimportTranslationInlineclassYourModelAdmin(admin.ModelAdmin):inlines=[TranslationInline,]When querying your translatable models, you can optimize your database queries by usingprefetch_relatedto prefetch translations.YourModel.objects.all().prefetch_related('translations')Run the provided management commands to generate.pofiles and update the database with translations.pythonmanage.pydrt_makemessagespythonmanage.pydrt_update_databaseConverting Existing.poFiles to DRT Format.
Utilize the management commanddrt_convert_localesto transform existing.pofiles into a format suitable for use with the Django Restful Translator (DRT). The command accepts the following arguments:--locale: The directory where the existing.pofiles are located and will be read from.--remove-used: When this option is used, the entries that are converted and added to the new.pofile formatted for DRT will be removed from the original.pofile, keeping it clean from already processed entries.To convert.pofiles without modifying the original file:pythonmanage.pydrt_convert_locales--localelocaleTo remove entries in the original.pofile that are used/converted in the new DRT format:pythonmanage.pydrt_convert_locales--localelocale--remove-usedAutomatic Translation Feature GuideOverviewOur library provides an easy way to automatically translate fields in your Django models using popular translation providers such as Google Translate v2, v3, AWS Translate and DeepL. This guide will help you configure and use this feature.RequirementsMake sure the necessary credentials for your chosen translation provider are available in your Django settings file.For Google Translate v2:You can use theGOOGLE_APPLICATION_CREDENTIALSenvironment variable to provide the location of a credential JSON file.https://cloud.google.com/docs/authentication/application-default-credentials#GACFor Google Translate v3:You can use theGOOGLE_APPLICATION_CREDENTIALSenvironment variable to provide the location of a credential JSON file.https://cloud.google.com/docs/authentication/application-default-credentials#GACConfiguring Django SettingsTo integrate Google Translate API v3 with your Django application, you need to add the following configuration settings in your settings.py file. These settings will specify your Google Cloud project name and the location for the Translate API:# settings.pyGOOGLE_CLOUD_PROJECT='your google cloud project id'GOOGLE_CLOUD_LOCATION='your google cloud location'For AWS Translate:# settings.pyAWS_ACCESS_KEY_ID='your aws access key here'AWS_SECRET_ACCESS_KEY='your aws secret access key here'AWS_REGION_NAME='your aws region name here'For DeepL Translate:# settings.pyDEEPL_AUTH_KEY='your DeepL auth key here'Running the Admin CommandYou can use the provided admin command to translate your model fields. The command accepts the following arguments:--language: The language code to which you want to translate.--target_language: The provider target language if it differs from the setting language--provider: The translation provider to use:google_v2,google_v3,aws,deepl.--all: (Optional) Use this flag if you want to overwrite existing translations.--workers: (Optional) Number of worker threads to use for concurrent processing. Default is 4.--without_batch: (Optional) One provider request per one unit of text.Run the admin command as follows:pythonmanage.pydrt_translate_models--language=es--provider=google_v3This will translate all translatable fields to Spanish using Google Translate.To overwrite existing translations:pythonmanage.pydrt_translate_models--language=es--provider=google_v3--allVerifying TranslationsAfter running the command, your translated text should now be available and stored in your database. You can verify this through your Django admin panel or by querying the models directly.ContributingPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.LicenseMIT