How to translate single page in django? - django

I have only one page i want to translate and im using downloaded application (with locale folder in it).
I want to get output from this app with translated values on one single page.
How can i tell the the template or view to show translated values?

I'm assuming that you want the page to be in a different language than what you have set for your website.
Here is two lines of the code of the middleware which manages languages in our dual-language website but I guess in your case, it can be placed at the beginning of your view function:
django.utils.translation.activate(desired_lang)
request.LANGUAGE_CODE = django.utils.translation.get_language()

Related

Changing language with React Native and django-modeltranslation

I would like to add option to mobile app to change language. Im using django on backend and RN as mobile. So I installed django-modeltranslation and added my model I want to translate. On mobile I display available languages. When user clicks on specific language I would like to get translated data. I have huge problem to create logic how to do it. I'm not asking about code just some hints and idea
EDIT:
For example: I added translation from django-modeltranslation to my model (i.e GameTask with field title, description etc). In my settings.py I have declared languages ('en','de','uk',etc) and added translations in database (for every field of GameTask, I added title(en), title(de) etc). When I change language in settings.py, values on mobile are changing too (so working as intended). So im not storing any translated text in app files, just in database (except of static errors and informations). Now I just want to send info from mobile with chosen language and activate this language on backend to return content in specific language
If you want to completely make your application multi-language, you need two things.
Translation system for your app
Translation system for your api.
First, use a pre-built context api or create your own to support changing language in-app. Something like this: https://medium.com/#ally_20818/multi-language-text-with-react-native-react-context-b76d5677346d
When user changes the language, store the language name or key in async-storage or some other database.
Change the texts in the react-native side based on the selected language.
When you're making a api call, send the selected language too. Get the selected language on api side and return appropriate texts based on language.
UPDATE:
Since you're not storing any text on react-native side, you only need to add a picker (react-native-picker/picker is a native picker) and store the selected language key (en, de, uk etc in your case) in a database like react-native-async-storage. When you're making api requests with react-native, include an additional header or post data which includes selected language key. And you can get and use that key in your django back-end.

How to preview a Wagtail page with translated fields in different languages?

I have a multi-language Wagtail website with two languages (English and German) using duplicated fields in my page models, e.g. a text block has two fields, text_de and text_en, and I define text as a translated field following the example in the translating content documentation. (I.e. I am NOT duplicating the whole page tree.) Here's an example how that looks in my code:
[models.py]
class MyPage(Page):
...
text_de = models.CharField(max_length=1024)
text_en = models.CharField(max_length=1024)
text = TranslatedField('text_en', 'text_de')
Everything works perfectly fine, in templates I can just use {{ text }} and depending on the active language (using i18n patterns and LocaleMiddleware) the correct version is displayed.
BUT: I have issues with getting a page preview in both languages.
When an editor creates a draft page in Wagtail admin and clicks on 'preview', then a page preview is shown in the language used within the Wagtail admin, i.e. in the language defined by the current editor's language preferences in his or her account settings.
How could the editor also preview the page in another language (without switching the language in his or her account settings back and forth)?
Is there maybe a way to construct a view that sets a different language before creating the page preview? Or is there another way to solve this?
I've tried to find out when Wagtail/Django decides which language it should serve and found the method get_url_parts of wagtail/admin/core/models, Page class. The page_path returned from this function is suffixed with '/de' or '/en', depending on the editor's account settings.
I can permanently change the language in which a page is displayed using translation.activate(). E.g. if I add translation.activate('en') to the get_context method of the MyPage class, then a MyPage-page and its previews are always shown in English. That's not really helpful.
I tried to construct a view, that first sets the language and then redirects to the preview like this:
[views.py]
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils import translation
def preview_language(request, pk=None, language='en'):
if language == 'de':
translation.activate(language)
else:
translation.activate('en')
return HttpResponseRedirect(reverse('wagtailadmin_pages:preview_on_edit', args=(pk,)))
But the language is set back to the editor's language from the account settings when the redirect is performed. And I am not sure that this is actually the way to go ...
Does anyone have a (different?) idea how to enable editors to easily preview a page with translated fields in different languages?
I am currently using Wagtail 2.4, Django 2.1, Python 3.5.
Any help is greatly appreciated! :-)
Wagtail's "preview modes" feature would probably help here: https://docs.wagtail.io/en/stable/reference/pages/model_reference.html#wagtail.core.models.Page.preview_modes
You could define English and German as two preview modes on your page model, and override the serve_preview method to activate the appropriate translation based on the received mode_name argument.

Flexible block positioning in Django?

In Drupal you could choose in which "region" of your site you want your block displayed. You did not have to modify any php/html code in order to achieve this.
Can such a thing be achieved with Django, and if yes, how?
By block I understand a piece of html output that doesn't have it's own URL and gets displayed along side the main data. (for example a search box or a poll)
Hm you probably want to create context processor and just output from it where you want it in template?
If you want reordering of content blocks in html output inside admin then you need something to generate that output like cms. You could try something like django-fluent-contents for this without requiring big cms.
Django and Drupal shouldn't be compared like this: Drupal is a CMS, Django is a web framework.
If you want to get a somewhat similar experience, I would look at using django-cms. With this, you can create numerous templates and set placeholders within these templates (these are regions of the page like 'sidebar', 'footer', 'content area' etc.). When you go to create a new page in django-cms, you select which template you want to use (maybe a two column layout or a three column layout with a header - depending on what you have created) and then you choose what content (or plugins) you want to place within the placeholders you have created in the template. So this is a somewhat similar experience to Drupal's regions.

How can I change the current language that users see for a Sitecore site?

Let's say I have a web site that is translated in to two languages - English and Spanish. I want to give the users a toggle switch at the top of the page that allows them to switch back and forth between the two languages. How would I do that? I don't want to set up different websites. I only want one website. I just want the user to be able to click on a button or link or something and go from English to Spanish. Now as they move around the site they are viewing the entire site in Spanish. Then if they click on the button or link again they would go back to English. Is there something I have to do in the code behind to tell Sitecore to "use" a different language?
You can switch the context language using "sc_lang" query string parameter. For example:
http://youwebsite.com?sc_lang=en
Typically in Sitecore you will want to represent the language using the built-in URL prefixes, e.g. http://www.example.com/en or http://www.example.com/es. We often use a cookie that we read in the httpRequestBegin pipeline to enhance the Sitecore LanguageResolver. Your language "toggle" could appropriately set that cookie.
See this article by John West for more detail and sample code.
https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/repost-overriding-sitecore-39-s-logic-to-determine-the-context-language
You can write your own LanguageResolver Pipeline to do this.
Or you can extend your URL by Using the sc_lang querystring Parameter eg.: sc_lang=en-EN or sc_lang=de-DE. If you have configured your linkmanager fine this should work.

Django - Static content display based on URL

I'm working on a Django site with a basic three column design. Left column navigation, center column content and right column URL specific content blocks.
My question is about the best method of controlling the URL specific content blocks in the right column.
I am thinking of something along the lines of the Flatpages app that will make the content available to the template context if the URL matches a pre-determined pattern (perhaps regex?).
Does anyone know if such an app already exists?
If not, I am looking for some advice about the best way to implement it. Particularly in relation to the matching of patterns to the current URL. Is there any good way to re-use parts of the Django URL dispatcher for this use?
Django CMS is a good suggestion, it depends on how deep you want to go. If this is just the beginning of different sorts of dynamic content you want then you should go that way for sure.
A simple one-off solution would be something like this:
You would just need to write a view and add some variables on the end of the URL that would define what showed up there. Depending on how fancy you need to get, you could just create a simple models, and just map the view to the model key
www.example.com/content/sidecontent/jokes/
so if "jokes" was your block of variable sidecontent (one of many in your sides model instances) the urls.py entry for that would be
(r'^content/sidecontent/(?P<side>)/$,sides.views.showsides),
and then in your sides app you have a view with a
def showsides(request, side):
Sides.objects.get(pk=side)
etc...
For something like this I personally would use Django CMS. It's like flatpages on steroids.
Django CMS has a concept of Pages, Templates, and Plugins. Each page has an associated template. Templates have placeholders where you can insert different plugins. Plugins are like mini-applications that can have dynamic model-based content.
Although Django-CMS is an interesting suggestion, there are quite a few projects that do specifically what you've requested - render blocks of content based on a URL. The main one that I know about is django-flatblocks.