From a430198cbb0754c262760d3a55fd7ba6d175c507 Mon Sep 17 00:00:00 2001 From: Matthew Summers Date: Fri, 19 Nov 2010 15:05:22 -0600 Subject: New feature to provide translations in ADS_STRUCT new VARS in settings example_ads has changed, its shortened with fewer examples so note this --- example_ads.py | 100 +++++++++-------------------------------- gentoo_ads/ads/views.py | 26 +++++++++-- gentoo_ads/example_settings.py | 8 +++- 3 files changed, 51 insertions(+), 83 deletions(-) diff --git a/example_ads.py b/example_ads.py index 25aa3eb..15947c8 100644 --- a/example_ads.py +++ b/example_ads.py @@ -10,11 +10,18 @@ # 'name': 'imgs/some/path/some/where/my_image.jpg' # Note that the MEDIA_URL has a trailing slash !important + +## try for lang/country first, then just lang, then fallback to en + ads = [ { 'type':'html', 'name': 'internal_name_1', - 'title': 'human title 1', + 'title': + { + 'en':'human title 1', + 'it':'italian human title 1', + }, 'tier': 1, 'weight': 15, #bogomips 'url': 'http://www1.example.com', @@ -29,14 +36,25 @@ ads = [ 'tier': 1, 'weight': 25, 'url': 'http://www2.example.com', - 'text': 'On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain.', + 'text': + { + 'en':'On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain.', + 'de':'german text translation', + 'it':'italian trans of num 2' + }, 'height': 120, 'width': 125, }, { 'type':'img', 'name': 'internal_name_3', - 'title': 'human title 3', + 'title': + { + 'en':'human title 3', + 'de':'german 3', + 'it':'italian 3', + 'de-DE':'de-DE german 3', + }, 'tier': 1, 'weight': 5, 'url': 'http://www3.example.com', @@ -55,80 +73,4 @@ ads = [ 'height': 120, 'width': 125, }, - { - 'type':'img', - 'name': 'internal_name_5', - 'title': 'human title 5', - 'tier': 2, - 'weight': 19, - 'url': 'http://www5.example.com', - 'img': 'path/to/img5.png', - 'height': 120, - 'width': 125, - }, - { - 'type':'img', - 'name': 'internal_name_6', - 'title': 'human title 6', - 'tier': 3, - 'weight': 150, - 'url': 'http://www6.example.com', - 'img': 'path/to/img6.png', - 'height': 120, - 'width': 125, - }, - { - 'type':'img', - 'name': 'internal_name_7', - 'title': 'human title 7', - 'tier': 3, - 'weight': 170, - 'url': 'http://www7.example.com', - 'img': 'path/to/img7.png', - 'height': 120, - 'width': 125, - }, - { - 'type':'img', - 'name': 'internal_name_8', - 'title': 'human title 8', - 'tier': 3, - 'weight': 11, - 'url': 'http://www8.example.com', - 'img': 'path/to/img8.png', - 'height': 120, - 'width': 125, - }, - { - 'name': 'internal_name_9', - 'title': 'human title 9', - 'tier': 3, - 'weight': 1950, - 'url': 'http://www9.example.com', - 'img': 'path/to/img9.png', - 'height': 120, - 'width': 125, - }, - { - 'type':'img', - 'name': 'internal_name_10', - 'title': 'human title 10', - 'tier': 3, - 'weight': 122, - 'url': 'http://www10.example.com', - 'img': 'path/to/img10.png', - 'height': 120, - 'width': 125, - }, - { - 'type':'img', - 'name': 'internal_name_11', - 'title': 'human title 11', - 'tier': 3, - 'weight': 17, - 'url': 'http://www11.example.com', - 'img': 'path/to/img11 .png', - 'height': 120, - 'width': 125, - }, ] diff --git a/gentoo_ads/ads/views.py b/gentoo_ads/ads/views.py index 3ec28d9..1fb1bcd 100644 --- a/gentoo_ads/ads/views.py +++ b/gentoo_ads/ads/views.py @@ -8,7 +8,26 @@ import logging def serve_ads(request): sample = _weighted_random_sample(settings.ADS_STRUCT) - return render_to_response('ads.html', {'ads': sample, 'MEDIA_URL': settings.MEDIA_URL,}) + ads = [_trans_ad(a, request.META['HTTP_ACCEPT_LANGUAGE'].split(',')[0]) for a in sample] + return render_to_response('ads.html', {'ads': ads, 'MEDIA_URL': settings.MEDIA_URL,}) + +def _trans_ad(ad, lang): + + for k in settings.TRANS_KEYS: + if k not in ad: + continue + if isinstance(ad[k], dict): + if lang in ad[k]: + ad[k] = ad[k][lang] + + elif lang.split('-')[0] in ad[k]: + s = lang.split('-')[0] + ad[k] = ad[k][s] + + else: + ad[k] = ad[k][settings.DEFAULT_ADS_LANG] + + return ad def _weighted_random_sample(ads): ads_out = [] @@ -24,10 +43,11 @@ def _weighted_random_sample(ads): cur_total += ad['weight'] if r < cur_total: - ads_out.append(ad) + ads_out.append(ad.copy()) ads = ads[:ad_i] + ads[ad_i + 1:] break ads_log_data_message = ','.join([a['name'] for a in ads_out]) logging.info(ads_log_data_message) - return ads_out \ No newline at end of file + return ads_out + diff --git a/gentoo_ads/example_settings.py b/gentoo_ads/example_settings.py index 47bda69..d2ef621 100644 --- a/gentoo_ads/example_settings.py +++ b/gentoo_ads/example_settings.py @@ -27,7 +27,7 @@ CONFIG_PATH = '/some/path/towards/the/file/gentoo-ads/example_ads.py' ads_module = load_source('ads_module', CONFIG_PATH,) ## sets the number of ads to be displayed -ADS_LENGTH = 6 +ADS_LENGTH = 3 ## why is this not above ADS_LENGTH? ## list of dictionaries we use in the view, i,e, the advertisements. @@ -37,6 +37,12 @@ ADS_STRUCT = ads_module.ads ## also eliminating the `import os` above ADS_IMAGES_DIR = os.path.join(os.path.dirname(CONFIG_PATH), 'images') +## These are the translateable keys in the ads_struct. +TRANS_KEYS = ('html', 'title', 'text') + +#default language for ads +DEFAULT_ADS_LANG = 'en' + DEBUG = True TEMPLATE_DEBUG = DEBUG -- cgit v1.2.3-65-gdbad