def test_basics(self): app = flask.Flask(__name__) b = babel.Babel(app, default_locale='de_DE') with app.test_request_context(): assert gettext(u'Hello %(name)s!', name='Peter') == 'Hallo Peter!' assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 3) == u'3 Äpfel' assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 1) == u'1 Apfel'
def add_product(product_id,counts): id = int(product_id) qty = int(counts) result = {} matching = [d for d in session['bag'] if d['pid'] == id] if matching: matching[0]['qty'] += qty session['last'] = matching[0] session['total']['price'] += qty*matching[0]['price'] session['total']['counts'] += qty else: product = Product.query.filter_by(id=product_id).first() session["bag"].append(dict({'pid': id, 'category_id':product.category_id, 'name': product.name, 'qty': qty, 'img': thumb.thumbnail(product.img,'200x200'), 'price': product.price})) session['last'] = dict({'pid': id, 'category_id':product.category_id, 'name': product.name, 'qty': qty, 'img': thumb.thumbnail(product.img,'200x200'), 'price': product.price}) session['total']['price'] += qty*product.price session['total']['counts'] += qty session['paidcats'] = get_categories_by_cart(session['bag']) session['total']['delivery'] = 0 for i in session['paidcats']: session['total']['delivery'] = session['total']['delivery']+i['cost'] result['result'] = 'success' result['last'] = session['last'] result['total'] = session['total'] result['success'] = '<a href="/pid'+str(id)+'">'+session['last']['name']+'</a> '+_('added to ')+'<a href="/cart">'+_('shopping cart')+'</a>' result['cartheader'] = ngettext('%(num)d item', '%(num)d items',session['total']['counts']) % {'num': session['total']['counts']}+' - '+str(session['total']['price']+session['total']['delivery'])+' '+_('rub.') return json.dumps(result)
def delete_product(product_id): id = int(product_id) result = {} matching = [d for d in session['bag'] if d['pid'] == id] if matching: qty = matching[0]['qty'] img = matching[0]['img'] name = matching[0]['name'] price = matching[0]['price'] session['bag'].remove(matching[0]) session['last'] = matching[0] session['total']['price'] -= qty*price session['total']['counts'] -= qty result['result'] = 'success' else: result['result'] = 'false' session['paidcats'] = get_categories_by_cart(session['bag']) session['total']['delivery'] = 0 for i in session['paidcats']: session['total']['delivery'] = session['total']['delivery']+i['cost'] result['last'] = session['last'] result['total'] = session['total'] result['success'] = '<a href="/pid'+str(id)+'">'+session['last']['name']+'</a> '+_('removed from ')+'<a href="/cart">'+_('shopping cart ')+'</a>' result['cartheader'] = ngettext('%(num)d item', '%(num)d items',session['total']['counts']) % {'num': session['total']['counts']}+' - '+str(session['total']['price']+session['total']['delivery'])+' '+_('rub.') return json.dumps(result)
def delete_product(product_id): id = int(product_id) result = {} matching = [d for d in session['bag'] if d['pid'] == id] if matching: qty = matching[0]['qty'] img = matching[0]['img'] name = matching[0]['name'] price = matching[0]['price'] session['bag'].remove(matching[0]) session['last'] = matching[0] session['total']['price'] -= qty * price session['total']['counts'] -= qty result['result'] = 'success' else: result['result'] = 'false' session['paidcats'] = get_categories_by_cart(session['bag']) session['total']['delivery'] = 0 for i in session['paidcats']: session['total']['delivery'] = session['total']['delivery'] + i['cost'] result['last'] = session['last'] result['total'] = session['total'] result['success'] = '<a href="/pid' + str(id) + '">' + session['last'][ 'name'] + '</a> ' + _('removed from ') + '<a href="/cart">' + _( 'shopping cart ') + '</a>' result['cartheader'] = ngettext( '%(num)d item', '%(num)d items', session['total']['counts']) % { 'num': session['total']['counts'] } + ' - ' + str(session['total']['price'] + session['total']['delivery']) + ' ' + _('rub.') return json.dumps(result)
def fruits_json(number=1): # duplicating the strings already set in the first view, to test how babel handles duplication # NOTE: this is a comment the translators will see; directing them to not translate the replacement string singular = gettext(u'Here is a basic string to translate') # however comments without the 'NOTE:' comment tag will not be processed into the POT file # notice that singular strings that use gettext() are aliased to _() for brevity singular_replacement = _(u'Here is a string that has a %(replacement)s string', replacement=u'replacement') # when we have a plural, we have to use ngettext() num_pears = random.randint(1, 10) plural = ngettext(u'Here is %(num)s pear', u'Here are %(num)s pears', num=num_pears) # we can also send a context to the translator to give them more info on what they are translating singular_context = pgettext(u'This text is part of a button used for exiting a pop-up', u'Cancel') plural_context = npgettext(u'This is part of a spinner on a fruit wheel', u'%(num)s orange', u'%(num)s oranges', num=(number or 1)) data = { 'fruits': [ { 'fruit': _(u'lime'), 'type': _(u'continental') }, { 'fruit': _(u'starfruit'), 'type': _(u'exotic') }, { 'fruit': _(u'strawberry'), 'type': _(u'native') } ], 'list': [ { 'string': singular }, { 'string': singular_replacement }, { 'string': plural }, { 'string': singular_context }, { 'string': plural_context } ] } # JSON Response return jsonify(data)
def fruits_json(number=1): # duplicating the strings already set in the first view, to test how babel handles duplication # NOTE: this is a comment the translators will see; directing them to not translate the replacement string singular = gettext(u'Here is a basic string to translate') # however comments without the 'NOTE:' comment tag will not be processed into the POT file # notice that singular strings that use gettext() are aliased to _() for brevity singular_replacement = _( u'Here is a string that has a %(replacement)s string', replacement=u'replacement') # when we have a plural, we have to use ngettext() num_pears = random.randint(1, 10) plural = ngettext(u'Here is %(num)s pear', u'Here are %(num)s pears', num=num_pears) # we can also send a context to the translator to give them more info on what they are translating singular_context = pgettext( u'This text is part of a button used for exiting a pop-up', u'Cancel') plural_context = npgettext(u'This is part of a spinner on a fruit wheel', u'%(num)s orange', u'%(num)s oranges', num=(number or 1)) data = { 'fruits': [{ 'fruit': _(u'lime'), 'type': _(u'continental') }, { 'fruit': _(u'starfruit'), 'type': _(u'exotic') }, { 'fruit': _(u'strawberry'), 'type': _(u'native') }], 'list': [{ 'string': singular }, { 'string': singular_replacement }, { 'string': plural }, { 'string': singular_context }, { 'string': plural_context }] } # JSON Response return jsonify(data)
def add_product(product_id, counts): id = int(product_id) qty = int(counts) result = {} matching = [d for d in session['bag'] if d['pid'] == id] if matching: matching[0]['qty'] += qty session['last'] = matching[0] session['total']['price'] += qty * matching[0]['price'] session['total']['counts'] += qty else: product = Product.query.filter_by(id=product_id).first() session["bag"].append( dict({ 'pid': id, 'category_id': product.category_id, 'name': product.name, 'qty': qty, 'img': thumb.thumbnail(product.img, '200x200'), 'price': product.price })) session['last'] = dict({ 'pid': id, 'category_id': product.category_id, 'name': product.name, 'qty': qty, 'img': thumb.thumbnail(product.img, '200x200'), 'price': product.price }) session['total']['price'] += qty * product.price session['total']['counts'] += qty session['paidcats'] = get_categories_by_cart(session['bag']) session['total']['delivery'] = 0 for i in session['paidcats']: session['total']['delivery'] = session['total']['delivery'] + i['cost'] result['result'] = 'success' result['last'] = session['last'] result['total'] = session['total'] result['success'] = '<a href="/pid' + str( id) + '">' + session['last']['name'] + '</a> ' + _( 'added to ') + '<a href="/cart">' + _('shopping cart') + '</a>' result['cartheader'] = ngettext( '%(num)d item', '%(num)d items', session['total']['counts']) % { 'num': session['total']['counts'] } + ' - ' + str(session['total']['price'] + session['total']['delivery']) + ' ' + _('rub.') return json.dumps(result)
def fruits(number=1): # some server side data (analogous to title strings in card_generators) # NOTE: this is a comment the translators will see; directing them to not translate the replacement string singular = gettext(u'Here is a basic string to translate') # however comments without the 'NOTE:' comment tag will not be processed into the POT file # notice that singular strings that use gettext() are aliased to _() for brevity singular_replacement = _( u'Here is a string that has a %(replacement)s string', replacement=u'replacement') # when we have a plural, we have to use ngettext() num_pears = random.randint(1, 10) plural = ngettext(u'Here is %(num)s pear', u'Here are %(num)s pears', num=num_pears) # we can also send a context to the translator to give them more info on what they are translating singular_context = pgettext( u'This text is part of a button used for exiting a pop-up', u'Cancel') plural_context = npgettext(u'This is part of a spinner on a fruit wheel', u'%(num)s orange', u'%(num)s oranges', num=(number or 1)) # NOTE: part a comment_test = _(u'first part') # NOTE: part b comment_test_2 = _(u'second part') context_test = pgettext(u'One', u'first part') context_test2 = pgettext(u'Two', u'second part') data = { 'fruits': [{ 'fruit': _(u'lime'), 'type': _(u'continental') }, { 'fruit': _(u'starfruit'), 'type': _(u'exotic') }, { 'fruit': _(u'strawberry'), 'type': _(u'native') }], 'list': [{ 'string': singular }, { 'string': singular_replacement }, { 'string': plural }, { 'string': singular_context }, { 'string': plural_context }] } return render_template('fruits.html', number=number, data=data)
def format_author_prop(author_list): return (ngettext('Author','Authors',len(author_list)),', '.join(author_list))
def fruits(number=1): # some server side data (analogous to title strings in card_generators) # NOTE: this is a comment the translators will see; directing them to not translate the replacement string singular = gettext(u'Here is a basic string to translate') # however comments without the 'NOTE:' comment tag will not be processed into the POT file # notice that singular strings that use gettext() are aliased to _() for brevity singular_replacement = _(u'Here is a string that has a %(replacement)s string', replacement=u'replacement') # when we have a plural, we have to use ngettext() num_pears = random.randint(1, 10) plural = ngettext(u'Here is %(num)s pear', u'Here are %(num)s pears', num=num_pears) # we can also send a context to the translator to give them more info on what they are translating singular_context = pgettext(u'This text is part of a button used for exiting a pop-up', u'Cancel') plural_context = npgettext(u'This is part of a spinner on a fruit wheel', u'%(num)s orange', u'%(num)s oranges', num=(number or 1)) # NOTE: part a comment_test = _(u'first part') # NOTE: part b comment_test_2 = _(u'second part') context_test = pgettext(u'One', u'first part') context_test2 = pgettext(u'Two', u'second part') data = { 'fruits': [ { 'fruit': _(u'lime'), 'type': _(u'continental') }, { 'fruit': _(u'starfruit'), 'type': _(u'exotic') }, { 'fruit': _(u'strawberry'), 'type': _(u'native') } ], 'list': [ { 'string': singular }, { 'string': singular_replacement }, { 'string': plural }, { 'string': singular_context }, { 'string': plural_context } ] } return render_template('fruits.html', number=number, data=data)
def _n(*args, **kwargs): return ngettext(*args, **kwargs)