def update_tables_21000(): text = u""" <p>%s</p> """ % _('Updating existing database tables...') logging.info("Updating existing database tables") from south.db import db #raise ImportError table_name = Store._meta.db_table field = Store._meta.get_field('state') db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name,)) field = Store._meta.get_field('translation_project') field.null = True db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name + '_id',)) table_name = Project._meta.db_table field = Project._meta.get_field('directory') field.null = True db.add_column(table_name, field.name, field) field = Project._meta.get_field('source_language') try: en = Language.objects.get(code='en') except Language.DoesNotExist: # we can't allow translation project detection to kick in yet so let's create en manually en = Language(code='en', fullname='English', nplurals=2, pluralequation="(n != 1)") en.directory = Directory.objects.root.get_or_make_subdir(en.code) en.save_base(raw=True) field.default = en.id db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name + '_id',)) # We shouldn't do save_pootle_version(21000) yet - more to do below return text
def test_add_project_language(self): """Tests that we can add a language to a project, then access its page when there are no files.""" fish = Language(code="fish", fullname="fish") fish.save() response = self.client.get("/projects/tutorial/admin.html") self.assertContains(response, "fish") project = Project.objects.get(code="tutorial") add_dict = {"language": fish.id, "project": project.id} response = self.client.post("/projects/tutorial/admin.html", formset_dict([add_dict])) self.assertContains(response, "/fish/tutorial/") response = self.client.get("/fish/") self.assertContains(response, '<a href="/fish/">fish</a>') self.assertContains(response, '<a href="/fish/tutorial/">Tutorial</a>') self.assertContains(response, "1 project, 0% translated")
def method_wrapper(self, request, *args, **kwargs): User = get_user_model() request.profile = User.get(self.request.user) try: request.permissions = get_matching_permissions( request.profile, self.permission_context) or [] except Http404 as e: # Test if lang code is not canonical but valid lang = Language.get_canonical(kwargs['language_code']) if lang is not None and lang.code != kwargs['language_code']: kwargs["language_code"] = lang.code return redirect( resolve(request.path).view_name, permanent=True, **kwargs) elif kwargs["dir_path"] or kwargs.get("filename", None): try: TranslationProject.objects.get( project__code=kwargs["project_code"], language__code=kwargs["language_code"]) # the TP exists so redirect to it return redirect( reverse( 'pootle-tp-browse', kwargs={ k: v for k, v in kwargs.items() if k in [ "language_code", "project_code"]})) except TranslationProject.DoesNotExist: pass # if we get here - the TP does not exist user_choice = self.request.COOKIES.get( 'user-choice', None) if user_choice: url = None if user_choice == 'language': url = reverse( 'pootle-language-browse', args=[kwargs["language_code"]]) elif user_choice == "project": url = reverse( 'pootle-project-browse', args=[kwargs["project_code"], '', '']) if url: response = redirect(url) response.delete_cookie('user-choice') return response raise e return f(self, request, *args, **kwargs)
def update_tables_21000(): text = u""" <p>%s</p> """ % _('Updating existing database tables...') logging.info("Updating existing database tables") from south.db import db table_name = Store._meta.db_table field = Store._meta.get_field('state') db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name, )) field = Store._meta.get_field('translation_project') field.null = True db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name + '_id', )) table_name = Project._meta.db_table field = Project._meta.get_field('directory') field.null = True db.add_column(table_name, field.name, field) field = Project._meta.get_field('source_language') try: en = Language.objects.get(code='en') except Language.DoesNotExist: # We can't allow translation project detection to kick in yet so let's # create en manually en = Language(code='en', fullname='English', nplurals=2, pluralequation="(n != 1)") en.directory = Directory.objects.root.get_or_make_subdir(en.code) en.save_base(raw=True) field.default = en.id db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name + '_id', )) # We shouldn't do save_pootle_version(21000) yet - more to do below return text
def method_wrapper(self, request, *args, **kwargs): try: request.permissions = get_matching_permissions( request.user, self.permission_context) or [] except Http404 as e: # Test if lang code is not canonical but valid lang = Language.get_canonical(kwargs['language_code']) if lang is not None and lang.code != kwargs['language_code']: kwargs["language_code"] = lang.code return redirect( resolve(request.path).view_name, permanent=True, **kwargs) elif kwargs["dir_path"] or kwargs.get("filename", None): try: TranslationProject.objects.get( project__code=kwargs["project_code"], language__code=kwargs["language_code"]) # the TP exists so redirect to it return redirect( reverse( 'pootle-tp-browse', kwargs={ k: v for k, v in kwargs.items() if k in [ "language_code", "project_code"]})) except TranslationProject.DoesNotExist: pass # if we get here - the TP does not exist user_choice = self.request.COOKIES.get( 'user-choice', None) if user_choice: url = None if user_choice == 'language': url = reverse( 'pootle-language-browse', args=[kwargs["language_code"]]) elif user_choice == "project": url = reverse( 'pootle-project-browse', args=[kwargs["project_code"], '', '']) if url: response = redirect(url) response.delete_cookie('user-choice') return response raise e return f(self, request, *args, **kwargs)
def test_add_project_language(self): """Tests that we can add a language to a project, then access its page when there are no files.""" fish = Language(code="fish", fullname="fish") fish.save() response = self.client.get("/projects/tutorial/admin.html") self.assertContains(response, "fish") project = Project.objects.get(code='tutorial') add_dict = { "language": fish.id, "project": project.id, } response = self.client.post("/projects/tutorial/admin.html", formset_dict([add_dict])) self.assertContains(response, '/fish/tutorial/') response = self.client.get("/fish/") self.assertContains(response, '<a href="/fish/">fish</a>') self.assertContains(response, '<a href="/fish/tutorial/">Tutorial</a>') self.assertContains(response, "1 project, 0% translated")
def update_tables_21000(): logging.info("Updating existing database tables") from south.db import db table_name = Store._meta.db_table field = Store._meta.get_field('state') db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name,)) field = Store._meta.get_field('translation_project') field.null = True db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name + '_id',)) table_name = Project._meta.db_table field = Project._meta.get_field('directory') field.null = True db.add_column(table_name, field.name, field) field = Project._meta.get_field('source_language') try: en = Language.objects.get(code='en') except Language.DoesNotExist: from pootle_app.models import Directory # We can't allow translation project detection to kick in yet so let's # create en manually en = Language(code='en', fullname='English', nplurals=2, pluralequation="(n != 1)") en.directory = Directory.objects.root.get_or_make_subdir(en.code) en.save_base(raw=True) field.default = en.id db.add_column(table_name, field.name, field) db.create_index(table_name, (field.name + '_id',)) save_legacy_pootle_version(21000)
def create_default_languages(self): """Create the default languages.""" from translate.lang import data, factory # import languages from toolkit languages = [] directories = [] existing_lang_codes = Language.objects.values_list("code", flat=True) for code in data.languages.keys(): if code in existing_lang_codes: continue directory = Directory(parent=Directory.objects.root, pootle_path="/%s/" % code, name=code) directories.append(directory) if directories: Directory.objects.bulk_create(directories) directories = { directory.name: directory for directory in Directory.objects.filter( parent=Directory.objects.root) } for code in data.languages.keys(): if code in existing_lang_codes: continue try: tk_lang = factory.getlanguage(code) except AttributeError: continue criteria = { "code": code, "fullname": tk_lang.fullname, "nplurals": tk_lang.nplurals, "pluralequation": tk_lang.pluralequation, } try: criteria["specialchars"] = tk_lang.specialchars except AttributeError: pass criteria["directory"] = directories[code] languages.append(Language(**criteria)) if languages: Language.objects.bulk_create(languages)
def import_languages(parsed_data): data = parsed_data.__root__._assignments # Is this really the right way? prefix = 'Pootle.languages.' # Filter out unrelated keys keys = [key for key in data if key.startswith(prefix)] # Clean up 'sv_SE.pluralequation' into 'sv_SE' langs = set([key[len(prefix):].split('.')[0] for key in keys]) for lang in map(lambda s: unicode(s, 'utf-8'), langs): # id, for free # code: try: db_lang = Language.objects.get(code=lang) logging.info('Already found a language named %s.\n'\ 'Data for this language are not imported.', lang) continue except Language.DoesNotExist: db_lang = Language(code=lang) # fullname db_lang.fullname = _get_attribute(data, lang, 'fullname') # nplurals db_lang.nplurals = try_type(int, _get_attribute(data, lang, 'nplurals', unicode_me=False, default=1)) # pluralequation db_lang.pluralequation = _get_attribute(data, lang, 'pluralequation', unicode_me=False) # specialchars db_lang.specialchars = _get_attribute(data, lang, 'specialchars') logging.info("Creating language %s", db_lang) db_lang.save()
def import_languages(parsed_data): data = parsed_data.__root__._assignments # Is this really the right way? prefix = 'Pootle.languages.' # Filter out unrelated keys keys = [key for key in data if key.startswith(prefix)] # Clean up 'sv_SE.pluralequation' into 'sv_SE' langs = set([key[len(prefix):].split('.')[0] for key in keys]) for lang in map(lambda s: unicode(s, 'utf-8'), langs): # id, for free # code: try: db_lang = Language.objects.get(code=lang) logging.log(logging.INFO, 'Already found a language named %s.\n'\ 'Data for this language are not imported.', lang) continue except Language.DoesNotExist: db_lang = Language(code=lang) # fullname db_lang.fullname = _get_attribute(data, lang, 'fullname') # nplurals db_lang.nplurals = try_type(int, _get_attribute(data, lang, 'nplurals', unicode_me = False, default=1)) # pluralequation db_lang.pluralequation = _get_attribute(data, lang, 'pluralequation', unicode_me = False) # specialchars db_lang.specialchars = _get_attribute(data, lang, 'specialchars') db_lang.save()
def create_default_languages(): """Create the default languages. We afford this priviledge to languages with reasonably complete interface translations for Pootle.""" from pootle_language.models import Language af = Language(code="af") af.fullname = u"Afrikaans" af.specialchars = u"ëïêôûáéíóúý" af.nplurals = '2' af.pluralequation = "(n != 1)" af.save() # Akan ak = Language(code='ak') ak.fullname = u'Akan' ak.pluralequation = u'(n > 1)' ak.specialchars = "ɛɔƐƆ" ak.nplurals = u'2' ak.save() # Haitian Creole ht = Language(code="ht") ht.fullname = u'Haitian; Haitian Creole' ht.nplurals = '2' ht.pluralequation = '(n != 1)' ht.save() # Sesotho sa Leboa # Northern Sotho nso = Language(code="nso") nso.fullname = u'Pedi; Sepedi; Northern Sotho' nso.nplurals = '2' nso.pluralequation = '(n != 1)' nso.specialchars = "šŠ" nso.save() # Tshivenḓa # Venda ve = Language(code="ve") ve.fullname = u'Venda' ve.nplurals = '2' ve.pluralequation = '(n != 1)' ve.specialchars = "ḓṋḽṱ ḒṊḼṰ ṅṄ" ve.save() # Wolof wo = Language(code="wo") wo.fullname = u'Wolof' wo.nplurals = '2' wo.pluralequation = '(n != 1)' wo.save() # 简体中文 # Simplified Chinese (China mainland used below, but also used in Singapore and Malaysia) zh_CN = Language(code="zh_CN") zh_CN.fullname = u'Chinese (China)' zh_CN.nplurals = '1' zh_CN.pluralequation = '0' zh_CN.specialchars = u"←→↔×÷©…—‘’“”【】《》" zh_CN.save() # 繁體中文 # Traditional Chinese (Hong Kong used below, but also used in Taiwan and Macau) zh_HK = Language(code="zh_HK") zh_HK.fullname = u'Chinese (Hong Kong)' zh_HK.nplurals = '1' zh_HK.pluralequation = '0' zh_HK.specialchars = u"←→↔×÷©…—‘’“”「」『』【】《》" zh_HK.save() # 繁體中文 # Traditional Chinese (Taiwan used below, but also used in Hong Kong and Macau) zh_TW = Language(code="zh_TW") zh_TW.fullname = u'Chinese (Taiwan)' zh_TW.nplurals = '1' zh_TW.pluralequation = '0' zh_TW.specialchars = u"←→↔×÷©…—‘’“”「」『』【】《》" zh_TW.save() ca_valencia = Language(code='ca@valencia') ca_valencia.fullname = u'Catalan (Valencia)' ca_valencia.nplurals = '2' ca_valencia.pluralequation = '(n != 1)' ca_valencia.save() son = Language(code='son') son.fullname = u'Songhai languages' son.nplurals = '1' son.pluralequation = '0' son.specialchars = u'ɲŋšžãõẽĩƝŊŠŽÃÕẼĨ' son.save() lg = Language(code='lg') lg.fullname = u'Ganda' lg.save() gd = Language(code='gd') gd.fullname = u'Gaelic; Scottish Gaelic' gd.nplurals = '4' gd.pluralequation = '(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3' gd.specialchars = u'àòùèìÀÈÌÒÙ' gd.save() # import languages from toolkit from translate.lang import data for code, props in data.languages.items(): try: lang, created = Language.objects.get_or_create(code=code, fullname=props[0], nplurals=props[1], pluralequation=props[2]) except: pass
def create_default_languages(): """Create the default languages. We afford this priviledge to languages with reasonably complete interface translations for Pootle.""" from pootle_language.models import Language af = Language(code="af") af.fullname = u"Afrikaans" af.specialchars = u"ëïêôûáéíóúý" af.nplurals = '2' af.pluralequation = "(n != 1)" af.save() # Akan ak = Language(code='ak') ak.fullname = u'Akan' ak.pluralequation = u'(n > 1)' ak.specialchars = "ɛɔƐƆ" ak.nplurals = u'2' ak.save() # Haitian Creole ht = Language(code="ht") ht.fullname = u'Haitian; Haitian Creole' ht.nplurals = '2' ht.pluralequation = '(n != 1)' ht.save() # Sesotho sa Leboa # Northern Sotho nso = Language(code="nso") nso.fullname = u'Pedi; Sepedi; Northern Sotho' nso.nplurals = '2' nso.pluralequation = '(n > 1)' nso.specialchars = "šŠ" nso.save() # Tshivenḓa # Venda ve = Language(code="ve") ve.fullname = u'Venda' ve.nplurals = '2' ve.pluralequation = '(n != 1)' ve.specialchars = "ḓṋḽṱ ḒṊḼṰ ṅṄ" ve.save() # Wolof wo = Language(code="wo") wo.fullname = u'Wolof' wo.nplurals = '2' wo.pluralequation = '(n != 1)' wo.save() # 简体中文 # Simplified Chinese (China mainland used below, but also used in Singapore and Malaysia) zh_CN = Language(code="zh_CN") zh_CN.fullname = u'Chinese (China)' zh_CN.nplurals = '1' zh_CN.pluralequation = '0' zh_CN.specialchars = u"←→↔×÷©…—‘’“”【】《》" zh_CN.save() # 繁體中文 # Traditional Chinese (Hong Kong used below, but also used in Taiwan and Macau) zh_HK = Language(code="zh_HK") zh_HK.fullname = u'Chinese (Hong Kong)' zh_HK.nplurals = '1' zh_HK.pluralequation = '0' zh_HK.specialchars = u"←→↔×÷©…—‘’“”「」『』【】《》" zh_HK.save() # 繁體中文 # Traditional Chinese (Taiwan used below, but also used in Hong Kong and Macau) zh_TW = Language(code="zh_TW") zh_TW.fullname = u'Chinese (Taiwan)' zh_TW.nplurals = '1' zh_TW.pluralequation = '0' zh_TW.specialchars = u"←→↔×÷©…—‘’“”「」『』【】《》" zh_TW.save() ca_valencia = Language(code='ca@valencia') ca_valencia.fullname = u'Catalan (Valencia)' ca_valencia.nplurals = '2' ca_valencia.pluralequation = '(n != 1)' ca_valencia.save() son = Language(code='son') son.fullname = u'Songhai languages' son.nplurals = '1' son.pluralequation = '0' son.specialchars = u'ɲŋšžãõẽĩƝŊŠŽÃÕẼĨ' son.save() lg = Language(code='lg') lg.fullname = u'Ganda' lg.save() # import languages from toolkit from translate.lang import data for code, props in data.languages.items(): try: lang, created = Language.objects.get_or_create( code=code, fullname=props[0], nplurals=props[1], pluralequation=props[2]) except: pass