""" Usage examples for django-translatable-fields demonstrating all field types. This file shows practical examples of how to work with translatable fields in various scenarios, including data creation, language switching, and accessing translations programmatically. """ from django.utils.translation import activate, get_language from .models import Product, Category, BlogPost, Company def create_sample_data(): """ Create sample data with translations in multiple languages. """ print("Creating sample data with translations...") # Create a product with all field types product = Product.objects.create( name="Awesome Widget", description="This is an amazing product that does everything you need.", slug="awesome-widget", support_email="support@example.com", website="https://example.com/products/awesome-widget", price=99.99 ) # Add German translations product.name_de = "Fantastisches Widget" product.description_de = "Das ist ein fantastisches Produkt, das alles macht, was Sie brauchen." product.slug_de = "fantastisches-widget" product.support_email_de = "support@beispiel.de" product.website_de = "https://beispiel.de/produkte/fantastisches-widget" # Add French translations product.name_fr = "Widget Fantastique" product.description_fr = "C'est un produit fantastique qui fait tout ce dont vous avez besoin." product.slug_fr = "widget-fantastique" product.support_email_fr = "support@exemple.fr" product.website_fr = "https://exemple.fr/produits/widget-fantastique" product.save() # Create a blog post blog_post = BlogPost.objects.create( title="How to Use Awesome Widgets", content="This comprehensive guide will show you everything about widgets...", slug="how-to-use-awesome-widgets", author_email="author@example.com", external_link="https://external-resource.com", meta_description="Complete guide to using awesome widgets effectively", published=True ) # Add translations for blog post blog_post.title_de = "Wie man fantastische Widgets verwendet" blog_post.content_de = "Dieser umfassende Leitfaden zeigt Ihnen alles über Widgets..." blog_post.slug_de = "wie-man-fantastische-widgets-verwendet" blog_post.author_email_de = "autor@beispiel.de" blog_post.external_link_de = "https://externe-ressource.de" blog_post.meta_description_de = "Vollständiger Leitfaden zur effektiven Nutzung fantastischer Widgets" blog_post.save() # Create a company company = Company.objects.create( name="Tech Innovations Inc.", description="Leading technology company specializing in innovative solutions.", slug="tech-innovations", contact_email="contact@techinnovations.com", website="https://techinnovations.com", address="123 Tech Street, Silicon Valley, CA 94000", founded_year=2010, employee_count=150 ) # Add translations for company company.name_de = "Tech Innovationen GmbH" company.description_de = "Führendes Technologieunternehmen mit Spezialisierung auf innovative Lösungen." company.slug_de = "tech-innovationen" company.contact_email_de = "kontakt@techinnovationen.de" company.website_de = "https://techinnovationen.de" company.address_de = "Techstraße 123, 10115 Berlin, Deutschland" company.save() return product, blog_post, company def demonstrate_language_switching(): """ Demonstrate how fields automatically return values based on current language. """ print("\nDemonstrating language-aware field access...") # Get a product (assuming one exists) try: product = Product.objects.first() if not product: print("No products found. Create sample data first.") return print(f"Current language: {get_language()}") # English activate('en') print(f"English - Name: {product.name}") print(f"English - Website: {product.website}") print(f"English - Email: {product.support_email}") # German activate('de') print(f"German - Name: {product.name}") print(f"German - Website: {product.website}") print(f"German - Email: {product.support_email}") # French activate('fr') print(f"French - Name: {product.name}") print(f"French - Website: {product.website}") print(f"French - Email: {product.support_email}") # Reset to English activate('en') except Exception as e: print(f"Error: {e}") def demonstrate_direct_access(): """ Demonstrate direct access to specific language translations. """ print("\nDemonstrating direct language access...") try: product = Product.objects.first() if not product: print("No products found. Create sample data first.") return print("Direct access to specific languages:") print(f"English name: {product.name_en}") print(f"German name: {product.name_de}") print(f"French name: {product.name_fr}") print(f"English slug: {product.slug_en}") print(f"German slug: {product.slug_de}") print(f"French slug: {product.slug_fr}") except Exception as e: print(f"Error: {e}") def demonstrate_translation_methods(): """ Demonstrate helper methods for working with translations. """ print("\nDemonstrating translation helper methods...") try: product = Product.objects.first() if not product: print("No products found. Create sample data first.") return # Get all translations for a field name_translations = product.get_all_translations('name') print(f"All name translations: {name_translations}") website_translations = product.get_all_translations('website') print(f"All website translations: {website_translations}") # Get specific translation german_name = product.get_translation('name', 'de') print(f"German name via method: {german_name}") # Set translation programmatically product.set_translation('name', 'es', 'Widget Fantástico') product.save() spanish_name = product.get_translation('name', 'es') print(f"Spanish name (newly set): {spanish_name}") except Exception as e: print(f"Error: {e}") def demonstrate_all_field_types(): """ Demonstrate all available translatable field types. """ print("\nDemonstrating all translatable field types...") try: # Create an instance with all field types company = Company.objects.create( name="Demo Company", # CharField description="This is a demo company.", # TextField slug="demo-company", # SlugField contact_email="demo@company.com", # EmailField website="https://democompany.com", # URLField address="123 Demo Street, Demo City" # TextField ) print("Created company with all field types:") print(f"CharField (name): {company.name}") print(f"TextField (description): {company.description}") print(f"SlugField (slug): {company.slug}") print(f"EmailField (contact_email): {company.contact_email}") print(f"URLField (website): {company.website}") print(f"TextField (address): {company.address}") # Add translations for each field type company.name_de = "Demo Firma" company.description_de = "Das ist eine Demo-Firma." company.slug_de = "demo-firma" company.contact_email_de = "demo@firma.de" company.website_de = "https://demofirma.de" company.address_de = "Demostraße 123, Demo Stadt" company.save() activate('de') print("\nGerman translations:") print(f"CharField (name): {company.name}") print(f"TextField (description): {company.description}") print(f"SlugField (slug): {company.slug}") print(f"EmailField (contact_email): {company.contact_email}") print(f"URLField (website): {company.website}") print(f"TextField (address): {company.address}") activate('en') # Reset except Exception as e: print(f"Error: {e}") def run_all_examples(): """ Run all examples in sequence. """ print("=== Django Translatable Fields - Usage Examples ===") # Create sample data product, blog_post, company = create_sample_data() # Run demonstrations demonstrate_language_switching() demonstrate_direct_access() demonstrate_translation_methods() demonstrate_all_field_types() print("\n=== Examples completed successfully! ===") if __name__ == "__main__": # This can be run in Django shell with: # python manage.py shell # >>> from example.usage_examples import run_all_examples # >>> run_all_examples() run_all_examples()