Exemplo n.º 1
0
class Language(models.Model):
    code = models.CharField(max_length=63,
                            choices=get_languages_for_locale('en'))
    userprofile = models.ForeignKey(UserProfile)

    class Meta:
        ordering = ['code']
        unique_together = ('code', 'userprofile')

    def __unicode__(self):
        return self.code

    def get_english(self):
        return self.get_code_display()

    def get_native(self):
        if not getattr(self, '_native', None):
            languages = get_languages_for_locale(self.code)
            for code, language in languages:
                if code == self.code:
                    self._native = language
                    break
        return self._native

    def unique_error_message(self, model_class, unique_check):
        if (model_class == type(self)
                and unique_check == ('code', 'userprofile')):
            return _('This language has already been selected.')
        return super(Language,
                     self).unique_error_message(model_class, unique_check)
Exemplo n.º 2
0
 def get_native(self):
     if not getattr(self, '_native', None):
         languages = get_languages_for_locale(self.code)
         for code, language in languages:
             if code == self.code:
                 self._native = language
                 break
     return self._native
Exemplo n.º 3
0
 def get_native(self):
     if not getattr(self, '_native', None):
         languages = get_languages_for_locale(self.code)
         for code, language in languages:
             if code == self.code:
                 self._native = language
                 break
     return self._native
Exemplo n.º 4
0
def langcode_to_name(code, locale=None):
    """Return the language name for the code in locale.

    If locale is None return in current activated language.
    """

    if not locale:
        locale = get_language()
    translated_languages = get_languages_for_locale(locale)
    try:
        lang = dict(translated_languages)[code]
    except KeyError:
        return code
    return lang
Exemplo n.º 5
0
def langcode_to_name(code, locale=None):
    """Return the language name for the code in locale.

    If locale is None return in current activated language.
    """

    if not locale:
        locale = get_language()
    translated_languages = get_languages_for_locale(locale)
    try:
        lang = dict(translated_languages)[code]
    except KeyError:
        return code
    return lang
Exemplo n.º 6
0
class Language(models.Model):
    code = models.CharField(max_length=63, choices=get_languages_for_locale('en'))
    userprofile = models.ForeignKey(UserProfile)

    class Meta:
        ordering = ['code']
        unique_together = ('code', 'userprofile')

    def __unicode__(self):
        return self.code

    def unique_error_message(self, model_class, unique_check):
        if (model_class == type(self) and unique_check == ('code', 'userprofile')):
            return _('This language has already been selected.')
        return super(Language, self).unique_error_message(model_class, unique_check)
Exemplo n.º 7
0
 def add_fields(self, form, index):
     super(BaseLanguageFormSet, self).add_fields(form, index)
     choices = [('', '---------')] + get_languages_for_locale(self.locale)
     form.fields['code'].choices = choices
Exemplo n.º 8
0
 def add_fields(self, form, index):
     super(BaseLanguageFormSet, self).add_fields(form, index)
     choices = [('', '---------')] + get_languages_for_locale(self.locale)
     form.fields['code'].choices = choices
Exemplo n.º 9
0
 def test_valid_locale(self):
     get_languages_for_locale('en')
     self.assertIn('en', AVAILABLE_LANGUAGES.keys())
Exemplo n.º 10
0
 def test_invalid_locale(self):
     """Test with invalid locale, must default to english translations."""
     languages = get_languages_for_locale('foobar')
     english_languages = get_languages_for_locale('en')
     eq_(english_languages, languages)
Exemplo n.º 11
0
 def add_fields(self, form, index):
     super(BaseLanguageFormSet, self).add_fields(form, index)
     choices = [("", "---------")] + get_languages_for_locale(self.locale)
     form.fields["code"].choices = choices
Exemplo n.º 12
0
 def test_valid_locale_not_cached(self):
     # check that key does not exist
     self.assertNotIn('el', AVAILABLE_LANGUAGES.keys())
     get_languages_for_locale('el')
     self.assertIn('el', AVAILABLE_LANGUAGES.keys())
Exemplo n.º 13
0
 def test_valid_locale(self):
     get_languages_for_locale("en")
     self.assertIn("en", list(AVAILABLE_LANGUAGES.keys()))