def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['DISCOURSE_URL'] = settings.DISCOURSE_URL.rstrip('/') context['rating_form'] = AppRatingForm( initial={'language_code': get_language()}) ratings = AppRating.objects.filter(app=context['app']) rating_languages = flatmap( lambda r: r.get_available_languages(), ratings) # make sure current session language is in the list even if there are # no comments. rating_languages = list(rating_languages) if get_language() not in rating_languages: rating_languages.append(get_language()) context['languages'] = set(sorted(rating_languages)) context['fallbackLang'] = 'en' if 'en' in context['languages'] else '' context['user_has_rated_app'] = False if self.request.user.is_authenticated: try: app_rating = AppRating.objects.get(user=self.request.user, app=context['app']) # if parler fallsback to a fallback language # it doesn't set the language as current language # and we can't select the correct language in the # frontend. So we try and find a languge that is # available language_code = app_rating.get_current_language() if not app_rating.has_translation(language_code): for fallback in app_rating.get_fallback_languages(): if app_rating.has_translation(fallback): app_rating.set_current_language(fallback) # when accessing an empty comment django-parler tries to # fall back to the default language. However for comments # the default (English) does not always exist. Unfortunately # it throws the same exception as non existing models, # so we need to access it beforehand try: comment = app_rating.comment except AppRating.DoesNotExist: comment = '' context['rating_form'] = AppRatingForm({ 'rating': app_rating.rating, 'comment': comment, 'language_code': app_rating.get_current_language(), }) context['user_has_rated_app'] = True except AppRating.DoesNotExist: pass context['categories'] = Category.objects.prefetch_related( 'translations').all() context['latest_releases_by_platform_v'] = \ self.object.latest_releases_by_platform_v() return context
def handle(self, *args, **options): target_file = join(settings.BASE_DIR, self.translation_file) translations = flatmap(lambda x: self._create_translations(x[0], x[1]), self.translated_fields) content = '\n'.join(translations) with open(target_file, 'w') as f: f.write(content) msg = 'Exported translations to %s' % target_file self.stdout.write(self.style.SUCCESS(msg))
def _create_translations(self, model, fields): objs = model.objects.language(self.source_lang).all() strings = flatmap(lambda o: [getattr(o, f) for f in fields], objs) strings = map(escape_tpl_string, strings) return list(map(lambda s: '{%% trans "%s" %%}' % s, strings))