def set_news_locale(news, context): """ Fill model content according to language. This function is called after an News model is filled with data from the database, but before is used in all other code. Use the locale of the current user/client to determine which language to display on the whole website. If the users locale is unavailable, select the alternative language, suffixing the title of the news with the displayed language. """ locale = get_locale() nl_available = news.nl_title and news.nl_content en_available = news.en_title and news.en_content if locale == 'nl' and nl_available: news.title = news.nl_title news.content = news.nl_content elif locale == 'en' and en_available: news.title = news.en_title news.content = news.en_content elif nl_available: news.title = news.nl_title + " (" + _('Dutch') + ")" news.content = news.nl_content elif en_available: news.title = news.en_title + " (" + _('English') + ")" news.content = news.en_content else: news.title = 'N/A' news.content = 'N/A'
def set_page_revision_locale(page_rev, context): """ Load the correct info in the model. This function is called after an PageRevision model is filled with data from the database, but before is used in all other code. Use the locale of the current user/client to determine which language to display on the whole website. If the users locale is unavailable, select the alternative language, suffixing the title of the activity with the displayed language. """ locale = get_locale() nl_available = page_rev.nl_title and page_rev.nl_content en_available = page_rev.en_title and page_rev.en_content if locale == 'nl' and nl_available: page_rev.title = page_rev.nl_title page_rev.content = page_rev.nl_content elif locale == 'en' and en_available: page_rev.title = page_rev.en_title page_rev.content = page_rev.en_content elif nl_available: page_rev.title = page_rev.nl_title + " (" + _('Dutch') + ")" page_rev.content = page_rev.nl_content elif en_available: page_rev.title = page_rev.en_title + " (" + _('English') + ")" page_rev.content = page_rev.en_content else: page_rev.title = 'N/A' page_rev.content = 'N/A'
def set_committee_revision_locale(com_rev, context): """ Fill model content according to language. This function is called after an CommitteeRevision model is filled with data from the database, but before is used in all other code. Use the locale of the current user/client to determine which language to display on the whole website. If the users locale is unavailable, select the alternative language, suffixing the title of the activity with the displayed language. """ locale = get_locale() nl_available = com_rev.nl_title and com_rev.nl_description en_available = com_rev.en_title and com_rev.en_description if locale == 'nl' and nl_available: com_rev.title = com_rev.nl_title com_rev.description = com_rev.nl_description elif locale == 'en' and en_available: com_rev.title = com_rev.en_title com_rev.description = com_rev.en_description elif nl_available: com_rev.title = com_rev.nl_title + " (" + _('Dutch') + ")" com_rev.description = com_rev.nl_description elif en_available: com_rev.title = com_rev.en_title + " (" + _('English') + ")" com_rev.description = com_rev.en_description else: com_rev.title = 'N/A' com_rev.description = 'N/A'
def test_get_locale(self, request): request.accept_languages.best_match.return_value = "en" request.cookies = {} reload(app) result = app.get_locale() self.assertEqual("en", result) request.accept_languages.best_match.assert_called_with(["pt", "en"])
def set_activity_locale(activity, context): """ Fill model content according to language. This function is called after an Activity model is filled with data from the database, but before is used in all other code. Use the locale of the current user/client to determine which language to display on the whole website. If the users locale is unavailable, select the alternative language, suffixing the title of the activity with the displayed language. """ locale = get_locale() nl_available = activity.nl_name and activity.nl_description en_available = activity.en_name and activity.en_description if locale == 'nl' and nl_available: activity.name = activity.nl_name activity.description = activity.nl_description elif locale == 'en' and en_available: activity.name = activity.en_name activity.description = activity.en_description elif nl_available: activity.name = activity.nl_name + " (" + _('Dutch') + ")" activity.description = activity.nl_description elif en_available: activity.name = activity.en_name + " (" + _('English') + ")" activity.description = activity.en_description else: activity.name = 'N/A' activity.description = 'N/A'
def home(): session['locale'] = get_locale() if g.user.max_date > datetime.now(): time = (g.user.max_date - datetime.now()).seconds else: time = 0 return render_template('index.html', title='Home', user=g.user, timeleft=time)
def get_time(self): """ Get a proper representation of all datetime date. Based on the time the event is gonna take/when it is going to start/when it's gonna end. """ today = datetime.now() if self.start_time.month == self.end_time.month and \ self.start_time.day == self.end_time.day: if self.start_time.year == today.year: return format_datetime(self.start_time, 'EEEE d MMM H:mm - ', locale=get_locale()).capitalize() + \ format_datetime(self.end_time, 'H:mm', locale=get_locale()) else: return format_datetime(self.start_time, 'EEEE d MMM YYYY, H:mm - ', locale=get_locale()).capitalize() + \ format_datetime(self.end_time, 'H:mm', locale=get_locale()) else: if self.start_time.year == today.year: return format_datetime(self.start_time, 'EEE d MMM (H:mm) - ', locale=get_locale()).capitalize() + \ format_datetime(self.end_time, 'EEE d MMM (H:mm)', locale=get_locale()).capitalize() else: return "%s - %s" % ( self.start_time.strftime(constants.ACT_DT_FORMAT), self.end_time.strftime(constants.ACT_DT_FORMAT))
def get_time(self): """ Get a proper representation of all datetime date. Based on the time the event is gonna take/when it is going to start/when it's gonna end. """ today = datetime.now() if self.start_time.month == self.end_time.month and \ self.start_time.day == self.end_time.day: if self.start_time.year == today.year: return format_datetime(self.start_time, 'EEEE d MMM, H:mm - ', locale=get_locale()).capitalize() + \ format_datetime(self.end_time, 'H:mm', locale=get_locale()) else: return format_datetime(self.start_time, 'EEEE d MMM YYYY, H:mm - ', locale=get_locale()).capitalize() + \ format_datetime(self.end_time, 'H:mm', locale=get_locale()) else: if self.start_time.year == today.year: return format_datetime(self.start_time, 'EEE d MMM (H:mm) - ', locale=get_locale()).capitalize() + \ format_datetime(self.end_time, 'EEE d MMM (H:mm)', locale=get_locale()).capitalize() else: return self.start_time.strftime("%a. %d %b %Y (%H:%M) - ") + \ self.end_time.strftime("%a. %d %b %Y (%H:%M)")
def get_localized_name(self, locale=None): if not locale: locale = get_locale() if locale == 'nl' and self.nl_name: return self.nl_name elif locale == 'en' and self.en_name: return self.en_name elif self.nl_name: return self.nl_name + " (Dutch)" elif self.en_name: return self.en_name + " (Engels)" else: return 'N/A'
def index(translation_id=None): """The main page.""" if request.host == 'translator.suminb.com': return redirect('http://better-translator.com') # NOTE: Do not use HTTP GET parameters 'sl', 'tl', 'm' and 't'. These are # reserved for special purposes. # FIXME: The following values must exist in other pages as well user_agent = request.headers.get('User-Agent', []) is_android = 'Android' in user_agent is_iphone = 'iPhone' in user_agent is_msie = 'MSIE' in user_agent context = dict( version=__version__, locale=get_locale(), is_android=is_android, is_iphone=is_iphone, is_mobile=is_android or is_iphone, is_msie=is_msie, language_options=language_options_html(), debug=os.environ.get('DEBUG', None), ) tresponse = None translation_id = translation_id or request.args.get('tr', None) #if translation_id != None: # tresponse = TranslationResponse.fetch(id_b62=translation_id) if translation_id is not None and tresponse is None: return redirect(url_for('main.index')) if tresponse is not None: translation = tresponse.serialize() translation['original_text'] = tresponse.request.original_text #translation['translated_text_dictlink'] = link_dictionary( #translation['translated_text'], translation['source'], translation['target']) context['og_description'] = tresponse.request.original_text context['translation'] = json.dumps(translation) else: context['og_description'] = _('app-description-text') return render_template('index.html', **context)
def sign_up_manual(): # Redirect the user to the index page if he or she has been authenticated # already. if current_user.is_authenticated: return redirect(url_for('home.home')) form = SignUpForm(request.form) # Add education. educations = Education.query.all() form.education_id.choices = [(e.id, e.name) for e in educations] if form.validate_on_submit(): try: user = user_service.register_new_user( email=form.email.data, password=form.password.data, first_name=form.first_name.data, last_name=form.last_name.data, student_id=form.student_id.data, education_id=form.education_id.data, birth_date=form.birth_date.data, study_start=form.study_start.data, receive_information=form.receive_information.data, phone_nr=form.phone_nr.data, address=form.address.data, zip_=form.zip.data, city=form.city.data, country=form.country.data, locale=get_locale()) login_user(user) flash(_('Welcome %(name)s! Your profile has been succesfully ' 'created and you have been logged in!', name=current_user.first_name), 'success') return redirect(url_for('home.home')) except BusinessRuleException: flash(_('A user with this e-mail address already exists'), 'danger') return render_template('user/sign_up.htm', form=form)
def get_translations(): # If there is no context: return None ctx = _request_ctx_stack.top if not ctx: return None # If context exists and contains a cashed value, return cached value if hasattr(ctx, 'flask_user_translations'): return ctx.flask_user_translations # If App has not initialized Flask-Babel: return None app_has_initalized_flask_babel = 'babel' in current_app.extensions if not app_has_initalized_flask_babel: # pragma no cover ctx.flask_user_translations = None return ctx.flask_user_translations # Prepare search properties import os import gettext as python_gettext from flask_babel import get_locale, support domain = 'messages' locales = [app.get_locale()] languages = [str(locale) for locale in locales] # See if translations exists in Application dir app_dir = os.path.join(current_app.root_path, 'translations') filename = python_gettext.find(domain, app_dir, languages) if filename: ctx.flask_user_translations = support.Translations.load(app_dir, locales, domain=domain) # See if translations exists in Flask-User dir else: flask_user_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'translations') ctx.flask_user_translations = support.Translations.load(flask_user_dir, locales, domain=domain) from flask.ext import babel # TODO: first babel.get_translations return babel.get_translations().merge(ctx.flask_user_translations)
def get_localized_basename(self, locale=None): if not locale: locale = get_locale() fallback = "Document {}".format(self.id) if locale == 'nl': if self.nl_name: return self.nl_name elif self.en_name: return self.en_name else: return fallback elif locale == 'en' and self.en_name: if self.en_name: return self.nl_name elif self.nl_name: return self.nl_name else: return fallback
def get_localized_name_desc(self, locale=None): if not locale: locale = get_locale() nl_available = self.nl_name and self.nl_description en_available = self.en_name and self.en_description if locale == 'nl' and nl_available: name = self.nl_name description = self.nl_description elif locale == 'en' and en_available: name = self.en_name description = self.en_description elif nl_available: name = self.nl_name + " (Dutch)" description = self.nl_description elif en_available: name = self.en_name + " (Engels)" description = self.en_description else: name = 'N/A' description = 'N/A' return name, description
def get_localized_title_content(self, locale=None): if not locale: locale = get_locale() nl_available = self.nl_title and self.nl_content en_available = self.en_title and self.en_content if locale == 'nl' and nl_available: title = self.nl_title content = self.nl_content elif locale == 'en' and en_available: title = self.en_title content = self.en_content elif nl_available: title = self.nl_title + " (Dutch)" content = self.nl_content elif en_available: title = self.en_title + " (Engels)" content = self.en_content else: title = 'N/A' content = 'N/A' return title, content
def set_navigation_entry_locale(nav_entry, context): """ Fill model content according to language. This function is called after an NavigationEntry model is filled with data from the database, but before is used in all other code. Use the locale of the current user/client to determine which language to display on the whole website. If the users locale is unavailable, select the alternative language, suffixing the title of the activity with the displayed language. """ locale = get_locale() if locale == 'nl' and nav_entry.nl_title: nav_entry.title = nav_entry.nl_title elif locale == 'en' and nav_entry.en_title: nav_entry.title = nav_entry.en_title elif nav_entry.nl_title: nav_entry.title = nav_entry.nl_title elif nav_entry.en_title: nav_entry.title = nav_entry.en_title else: nav_entry.title = 'N/A'
def sign_up_saml_response(): redir_url = saml_service.get_redirect_url(url_for('home.home')) # Redirect the user to the index page if he or she has been authenticated # already. if current_user.is_authenticated: # End the sign up session when it is still there somehow if saml_service.sign_up_session_active(): saml_service.end_sign_up_session() return redirect(redir_url) if saml_service.sign_up_session_active(): # Delete the old sign up session when # the user re-authenticates if saml_service.user_is_authenticated(): saml_service.end_sign_up_session() # Otherwise, refresh the timeout timestamp of the session else: saml_service.update_sign_up_session_timestamp() form = SignUpForm(request.form) # Add education. educations = Education.query.all() form.education_id.choices = [(e.id, e.name) for e in educations] if not saml_service.sign_up_session_active(): if not saml_service.user_is_authenticated(): flash(_('Authentication failed. Please try again.'), 'danger') return redirect(redir_url) if not saml_service.user_is_student(): flash(_('You must authenticate with a student ' 'UvA account to register.'), 'danger') return redirect(redir_url) if saml_service.uid_is_linked_to_other_user(): flash(_('There is already an account linked to this UvA account. ' 'If you are sure that this is a mistake please send ' 'an email to the board.'), 'danger') return redirect(redir_url) # Start a new sign up session and pre-fill the form saml_service.start_sign_up_session() saml_service.fill_sign_up_form_with_saml_attributes( form) # When we encounter a GET request but a session is already active, # this means that the user did a refresh without submitting the form. # We redirect him/her to the SAML sign up, since otherwise all # pre-filled data would be gone. elif request.method == 'GET': return redirect(url_for('saml.sign_up')) else: # Make sure that it is not possible to change the student id form.student_id.data = \ saml_service.get_sign_up_session_linking_student_id() if form.validate_on_submit(): try: user = user_service.register_new_user( email=form.email.data, password=form.password.data, first_name=form.first_name.data, last_name=form.last_name.data, student_id=form.student_id.data, education_id=form.education_id.data, birth_date=form.birth_date.data, study_start=form.study_start.data, receive_information=form.receive_information.data, phone_nr=form.phone_nr.data, address=form.address.data, zip_=form.zip.data, city=form.city.data, country=form.country.data, locale=get_locale(), link_student_id=True) login_user(user) saml_service.end_sign_up_session() flash(_('Welcome %(name)s! Your profile has been succesfully ' 'created and you have been logged in!', name=current_user.first_name), 'success') return redirect(redir_url) except BusinessRuleException: flash(_('A user with this e-mail address already exists'), 'danger') return render_template('user/sign_up.htm', form=form, disable_student_id=True)
def disclaimers(): context = dict( version=__version__, locale=get_locale(), ) return render_template('disclaimers.html', **context)
def credits(): context = dict( version=__version__, locale=get_locale(), ) return render_template('credits.html', **context)
def test_get_locale_from_cookie(self, request): request.cookies = {"language": "pt"} reload(app) result = app.get_locale() self.assertEqual("pt", result)
def index(): return redirect('/' + get_locale())
def url_view(url): from app import get_locale page = DataGetter.get_page_by_url('/' + url, get_locale()) return render_template('gentelella/guest/page.html', page=page)
def url_view(self, url): from app import get_locale page = DataGetter.get_page_by_url('/' + url, get_locale()) return self.render('/gentelella/guest/page.html', page=page)
def get_timedelta_to_start(self): """Leturn locale based description of time left to start.""" return format_timedelta(datetime.now() - self.start_time, locale=get_locale())
def before_request(): if current_user.is_authenticated: current_user.last_seen = datetime.utcnow() db.session.commit() g.search_form = SearchForm() g.locale = str(get_locale())
def get_timedelta_from_end(self): """Locale based description of time in the past.""" return format_timedelta(datetime.now() - self.end_time, locale=get_locale())
def url_view(url): from app import get_locale page = DataGetter.get_page_by_url('/' + url, get_locale()) if page == None: return abort(404) return render_template('gentelella/guest/page.html', page=page)
def get_timedelta_to_end(self): """Locale based description of time until the end of the activity.""" return format_timedelta(self.end_time - datetime.now(), locale=get_locale())