def test_locale_to_lower_lower(): """ Test cc.i18n.util.locale_to_lower_lower() """ assert util.locale_to_lower_lower('en') == 'en' assert util.locale_to_lower_lower('en_US') == 'en-us' assert util.locale_to_lower_lower('en-us') == 'en-us' # crazy renditions. Useful? assert util.locale_to_lower_lower('en-US') == 'en-us' assert util.locale_to_lower_lower('en_us') == 'en-us'
def test_locale_to_lower_lower(): """ Test cc.i18n.util.locale_to_lower_lower() """ assert util.locale_to_lower_lower("en") == "en" assert util.locale_to_lower_lower("en_US") == "en-us" assert util.locale_to_lower_lower("en-us") == "en-us" # crazy renditions. Useful? assert util.locale_to_lower_lower("en-US") == "en-us" assert util.locale_to_lower_lower("en_us") == "en-us"
def get_xmp_info(request_form, license, locale): ugettext = ugettext_for_locale(locale) # assemble the necessary information for the XMP file before rendering year = ('field_year' in request_form and request_form['field_year']) or "" creator = ('field_creator' in request_form and request_form['field_creator']) or None work_type = workType( ('field_format' in request_form and request_form['field_format']) or "") work_url = ('field_url' in request_form and request_form['field_url']) or None # determine the license notice if ('publicdomain' in license.uri): notice = "This %s is dedicated to the public domain." % (work_type) copyrighted = False else: if creator: notice = "Copyright %s %s. " % ( year, creator, ) else: notice = "" i18n_work = ugettext('work') work_notice_template = ugettext( u'This %(work_type)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') work_notice = work_notice_template % { 'license_name': license.title(locale_to_lower_lower(locale)), 'license_url': license.uri, 'work_type': i18n_work } notice = notice + work_notice copyrighted = True return { 'copyrighted': copyrighted, 'notice': notice, 'license_url': license.uri, 'license': license, 'work_url': work_url }
def get_xmp_info(request_form, license, locale): ugettext = ugettext_for_locale(locale) # assemble the necessary information for the XMP file before rendering year = ('field_year' in request_form and request_form['field_year']) or "" creator = ('field_creator' in request_form and request_form['field_creator']) or None work_type = workType(('field_format' in request_form and request_form['field_format']) or "") work_url = ('field_url' in request_form and request_form['field_url']) or None # determine the license notice if ('publicdomain' in license.uri): notice = "This %s is dedicated to the public domain." % (work_type) copyrighted = False else: if creator: notice = "Copyright %s %s. " % (year, creator,) else: notice = "" i18n_work = ugettext('work') work_notice_template = ugettext( u'This %(work_type)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') work_notice = work_notice_template % { 'license_name': license.title(locale_to_lower_lower(locale)), 'license_url': license.uri, 'work_type': i18n_work} notice = notice + work_notice copyrighted = True return { 'copyrighted': copyrighted, 'notice':notice, 'license_url':license.uri, 'license':license, 'work_url':work_url}
def translate_graph(graph): """ Look for title assertions with i18n as the lang, use their object as the msgid to find additionaly title translations Args: graph: rdflib processed graph for us to walk through i18n_dir: directory of PO files. Default directory is that which is supplied with this package. """ lang_dirs = os.listdir( os.path.abspath(pkg_resources.resource_filename('cc.i18n', 'i18n'))) for subject, predicate, obj in graph.triples((None, None, None)): if not hasattr(obj, 'language') or obj.language != 'i18n': continue else: str_id = unicode(obj) if not str_id: return None old_objects = {} # remove any previous instane of this language's # translations. for s, p, old_obj in graph.triples((subject, predicate, None)): if lang_dirs.count(old_obj.language): old_objects[old_obj.language] = old_obj for lang in lang_dirs: rdf_lang = locale_to_lower_lower(lang) if old_objects.has_key(rdf_lang): graph.remove((subject, predicate, old_objects[rdf_lang])) translated = util.inverse_translate(str_id, lang) graph.add((subject, predicate, Literal(translated, lang=rdf_lang)))
def translate_graph(graph): """ Look for title assertions with i18n as the lang, use their object as the msgid to find additionaly title translations Args: graph: rdflib processed graph for us to walk through i18n_dir: directory of PO files. Default directory is that which is supplied with this package. """ lang_dirs = os.listdir(os.path.abspath(pkg_resources.resource_filename("cc.i18n", "i18n"))) for subject, predicate, obj in graph.triples((None, None, None)): if not hasattr(obj, "language") or obj.language != "i18n": continue else: str_id = unicode(obj) if not str_id: return None old_objects = {} # remove any previous instane of this language's # translations. for s, p, old_obj in graph.triples((subject, predicate, None)): if lang_dirs.count(old_obj.language): old_objects[old_obj.language] = old_obj for lang in lang_dirs: rdf_lang = locale_to_lower_lower(lang) if old_objects.has_key(rdf_lang): graph.remove((subject, predicate, old_objects[rdf_lang])) translated = util.inverse_translate(str_id, lang) graph.add((subject, predicate, Literal(translated, lang=rdf_lang)))
def format(self, license, work_dict=None, locale='en'): """Return an HTML + RDFa string serialization for the license, optionally incorporating the work metadata and locale.""" gettext = ugettext_for_locale(locale) work_dict = work_dict or {} image_header = IMAGE_HEADER_TEMPLATE % { 'license_url': license.uri, 'util.Creative_Commons_License': util.escape(gettext(u'Creative Commons License')), 'license_logo': license.logo } dctype = None if work_dict.get('format'): dctype = _translate_dctype(work_dict['format'].lower()) body_vars = { 'license_url': license.uri, 'license_name': util.escape(license.title(locale_to_lower_lower(locale))) } if ((work_dict.get('attribution_url') or work_dict.get('attribution_name')) and work_dict.get('worktitle')): body_template = gettext( u'%(work_title)s by %(work_author)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') body_vars.update({ 'work_title': process_work_title(dctype, work_dict['worktitle']), 'work_author': process_work_author(work_dict.get('attribution_url'), work_dict.get('attribution_name')) }) elif work_dict.get('attribution_url') \ or work_dict.get('attribution_name'): body_template = gettext( u'This %(work_type)s by %(work_author)s is licensed under ' u'a <a rel="license" href="%(license_url)s">Creative ' u'Commons %(license_name)s License</a>.') body_vars.update({ 'work_type': process_work_type(gettext, dctype), 'work_author': process_work_author(work_dict.get('attribution_url'), work_dict.get('attribution_name')) }) elif work_dict.get('worktitle'): body_template = gettext( u'%(work_title)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') body_vars.update({ 'work_title': process_work_title(dctype, work_dict['worktitle']) }) else: body_template = gettext( u'This %(work_type)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') body_vars.update({'work_type': process_work_type(gettext, dctype)}) message = image_header + body_template % body_vars if work_dict.get('source_work'): source_work_template = gettext( u'Based on a work at %(source_link)s.') source_domain = urlparse(work_dict['source_work'])[1] if not source_domain.strip(): source_domain = work_dict['source_work'] source_work = source_work_template % { 'source_link': SOURCE_LINK_TEMPLATE % { 'source_work': util.escape(work_dict['source_work']), 'source_domain': util.escape(source_domain) } } message = message + "<br />" + source_work if work_dict.get('more_permissions_url'): more_perms_template = gettext( u'Permissions beyond the scope of this license may be ' u'available at %(more_perms_link)s.') more_perms = more_perms_template % { 'more_perms_link': MORE_PERMS_LINK_TEMPATE % { 'more_permissions_url': util.escape(work_dict['more_permissions_url']) } } message = message + "<br />" + more_perms return message
def format(self, license, work_dict=None, locale='en'): """Return an HTML + RDFa string serialization for the license, optionally incorporating the work metadata and locale.""" gettext = ugettext_for_locale(locale) work_dict = work_dict or {} localized_deed = license.uri default_lang = license.jurisdiction.default_language if not default_lang: default_lang = "en" if locale != default_lang: localized_deed += "deed." + locale image_header = IMAGE_HEADER_TEMPLATE % { 'license_url': localized_deed, 'util.Creative_Commons_License': util.escape( gettext(u'Creative Commons License')), 'license_logo': license.logo} dctype = None if work_dict.get('format'): dctype = _translate_dctype(work_dict['format'].lower()) body_vars = { 'license_url': localized_deed, 'license_name': util.escape( license.title(locale_to_lower_lower(locale)))} if ((work_dict.get('attribution_url') or work_dict.get('attribution_name')) and work_dict.get('worktitle')): body_template = gettext( u'%(work_title)s by %(work_author)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') body_vars.update( {'work_title': process_work_title( dctype, work_dict['worktitle']), 'work_author': process_work_author( work_dict.get('attribution_url'), work_dict.get('attribution_name'))}) elif work_dict.get('attribution_url') \ or work_dict.get('attribution_name'): body_template = gettext( u'This %(work_type)s by %(work_author)s is licensed under ' u'a <a rel="license" href="%(license_url)s">Creative ' u'Commons %(license_name)s License</a>.') body_vars.update( {'work_type': process_work_type(gettext, dctype), 'work_author': process_work_author( work_dict.get('attribution_url'), work_dict.get('attribution_name'))}) elif work_dict.get('worktitle'): body_template = gettext( u'%(work_title)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') body_vars.update( {'work_title': process_work_title( dctype, work_dict['worktitle'])}) else: body_template = gettext( u'This %(work_type)s is licensed under a ' u'<a rel="license" href="%(license_url)s">Creative Commons ' u'%(license_name)s License</a>.') body_vars.update( {'work_type': process_work_type(gettext, dctype)}) message = image_header + body_template % body_vars if work_dict.get('source_work'): source_work_template = gettext( u'Based on a work at %(source_link)s.') source_domain = urlparse(work_dict['source_work'])[1] if not source_domain.strip(): source_domain = work_dict['source_work'] source_work = source_work_template % { 'source_link': SOURCE_LINK_TEMPLATE % { 'source_work': util.escape(work_dict['source_work']), 'source_domain': util.escape(source_domain)}} message = message + "<br />" + source_work if work_dict.get('more_permissions_url'): more_perms_template = gettext( u'Permissions beyond the scope of this license may be ' u'available at %(more_perms_link)s.') more_perms = more_perms_template % { 'more_perms_link': MORE_PERMS_LINK_TEMPATE % { 'more_permissions_url': util.escape( work_dict['more_permissions_url'])}} message = message + "<br />" + more_perms return message
def license_deed_view(request): """ The main and major deed generating view. """ ########################## # Try and get the license. ########################## license = by_code(request.matchdict['code'], jurisdiction=request.matchdict.get('jurisdiction'), version=request.matchdict.get('version')) if not license: license_versions = util.catch_license_versions_from_request(request) if license_versions: # If we can't get it, but others of that code exist, give # a special 404. return license_catcher(request) else: # Otherwise, give the normal 404. return exc.HTTPNotFound() #################### # Everything else ;) #################### # "color" of the license; the color reflects the relative amount # of freedom. if license.license_code in ('devnations', 'sampling'): color = 'red' elif license.license_code.find('sampling') > -1 or \ license.license_code.find('nc') > -1 or \ license.license_code.find('nd') > -1: color = 'yellow' else: color = 'green' # Get the language this view will be displayed in. # - First checks to see if the routing matchdict specifies the language # - Or, next gets the jurisdictions' default language if the jurisdiction # specifies one # - Otherwise it's english! if request.matchdict.has_key('target_lang'): target_lang = request.matchdict.get('target_lang') elif license.jurisdiction.default_language: target_lang = locale_to_lower_upper( license.jurisdiction.default_language) else: target_lang = 'en' # True if the legalcode for this license is available in # multiple languages (or a single language with a language code different # than that of the jurisdiction). # # Stored in the RDF, we'll just check license.legalcodes() :) legalcodes = license.legalcodes(target_lang) if len(legalcodes) > 1 \ or list(legalcodes)[0][2] is not None: multi_language = True legalcodes = sorted(legalcodes, key=lambda lc: lc[2]) else: multi_language = False # Use the lower-dash style for all RDF-related locale stuff rdf_style_target_lang = locale_to_lower_lower(target_lang) license_title = None try: license_title = license.title(rdf_style_target_lang) except KeyError: # don't have one for that language, use default license_title = license.title() conditions = {} for code in license.license_code.split('-'): conditions[code] = 1 # Find out all the active languages active_languages = get_well_translated_langs() negotiated_locale = negotiate_locale(target_lang) # If negotiating the locale says that this isn't a valid language, # let's fall back to something that is. if target_lang != negotiated_locale: base_url = REMOVE_DEED_URL_RE.match(request.path_info).groups()[0] redirect_to = base_url + 'deed.' + negotiated_locale return exc.HTTPFound(location=redirect_to) if DEED_TEMPLATE_MAPPING.has_key(license.license_code): main_template = DEED_TEMPLATE_MAPPING[license.license_code] else: main_template = 'licenses/standard_deed.html' get_this = "/choose/results-one?license_code=%s&jurisdiction=%s&version=%s&lang=%s" % ( urllib.quote(license.license_code), license.jurisdiction.code, license.version, target_lang) context = { 'request': request, 'license_code': license.license_code, 'license_code_quoted': urllib.quote(license.license_code), 'license_title': license_title, 'license': license, 'multi_language': multi_language, 'legalcodes': legalcodes, 'color': color, 'conditions': conditions, 'active_languages': active_languages, 'target_lang': target_lang, 'jurisdiction': license.jurisdiction.code, 'get_this': get_this, } context.update(util.rtl_context_stuff(target_lang)) return Response( util.render_template(request, target_lang, main_template, context))
def license_deed_view(request): """ The main and major deed generating view. """ ########################## # Try and get the license. ########################## license = by_code( request.matchdict['code'], jurisdiction=request.matchdict.get('jurisdiction'), version=request.matchdict.get('version')) if not license: license_versions = util.catch_license_versions_from_request(request) if license_versions: # If we can't get it, but others of that code exist, give # a special 404. return license_catcher(request) else: # Otherwise, give the normal 404. return exc.HTTPNotFound() #################### # Everything else ;) #################### # "color" of the license; the color reflects the relative amount # of freedom. if license.license_code in ('devnations', 'sampling'): color = 'red' elif license.license_code.find('sampling') > -1 or \ license.license_code.find('nc') > -1 or \ license.license_code.find('nd') > -1: color = 'yellow' else: color = 'green' # Get the language this view will be displayed in. # - First checks to see if the routing matchdict specifies the language # - Or, next gets the jurisdictions' default language if the jurisdiction # specifies one # - Otherwise it's english! if request.matchdict.has_key('target_lang'): target_lang = request.matchdict.get('target_lang') elif license.jurisdiction.default_language: target_lang = locale_to_lower_upper( license.jurisdiction.default_language) else: target_lang = 'en' # True if the legalcode for this license is available in # multiple languages (or a single language with a language code different # than that of the jurisdiction). # # Stored in the RDF, we'll just check license.legalcodes() :) legalcodes = license.legalcodes(target_lang) if len(legalcodes) > 1 \ or list(legalcodes)[0][2] is not None: multi_language = True legalcodes = sorted(legalcodes, key=lambda lc: lc[2]) else: multi_language = False # Use the lower-dash style for all RDF-related locale stuff rdf_style_target_lang = locale_to_lower_lower(target_lang) license_title = None try: license_title = license.title(rdf_style_target_lang) except KeyError: # don't have one for that language, use default license_title = license.title() conditions = {} for code in license.license_code.split('-'): conditions[code] = 1 # Find out all the active languages active_languages = get_well_translated_langs() negotiated_locale = negotiate_locale(target_lang) # If negotiating the locale says that this isn't a valid language, # let's fall back to something that is. if target_lang != negotiated_locale: base_url = REMOVE_DEED_URL_RE.match(request.path_info).groups()[0] redirect_to = base_url + 'deed.' + negotiated_locale return exc.HTTPFound(location=redirect_to) if DEED_TEMPLATE_MAPPING.has_key(license.license_code): main_template = DEED_TEMPLATE_MAPPING[license.license_code] else: main_template = 'licenses/standard_deed.html' get_this = "/choose/results-one?license_code=%s&jurisdiction=%s&version=%s&lang=%s" % (urllib.quote(license.license_code), license.jurisdiction.code, license.version, target_lang) context = { 'request': request, 'license_code': license.license_code, 'license_code_quoted': urllib.quote(license.license_code), 'license_title': license_title, 'license': license, 'multi_language': multi_language, 'legalcodes': legalcodes, 'color': color, 'conditions': conditions, 'active_languages': active_languages, 'target_lang': target_lang, 'jurisdiction':license.jurisdiction.code, 'get_this': get_this, } context.update(util.rtl_context_stuff(target_lang)) return Response( util.render_template( request, target_lang, main_template, context))