Giter Site home page Giter Site logo

transifex-python's Introduction

Test suite codecov

Transifex Python Toolkit

Transifex Python Toolkit is a collection of tools that allow you to easily localize your Django and Python applications using Transifex. The toolkit features fetching translations over the air (OTA) to your apps.

This project adheres to the Contributor Covenant code of conduct. To contribute to Transifex Python Toolkit, please check out the contribution guidelines.

Upgrade to v2

If you are upgrading from the 1.x.x version, please read this migration guide, as there are breaking changes in place.

Quick starting guide

  1. Install toolkit in your code $ pip install transifex-python
  2. Add a provided TOKEN and SECRET in your config, connecting your application with a Transifex project
  3. Add internationalization commands in your code
  <!-- Django app template example -->

  {% load transifex %}
  <p>{% t "Hello!" %}</p>
  <p>{% t "I want to be translated." %}</p>
  # Django view sample
  from transifex.native.django import t

  output = {
      "msg1": t("Welcome aboard!"),
      "msg2": t("It's great to have you here!"),
  }
  return JsonResponse(output)
  1. Push strings to your connected Transifex project ./manage.py transifex push
  2. When translations are added in your Transifex project are automatically made available

To learn more about using Transifex Python toolkit check:

License

Licensed under Apache License 2.0, see LICENSE file.

transifex-python's People

Contributors

besfahbod avatar candrikos avatar codegaze avatar deathbird avatar kbairak avatar m-aciek avatar nbasili avatar wyngarde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

transifex-python's Issues

[Django] Can't translate models.TextChoices enum values

Hi!

I recently subscribed to Transifex, and I have a small issue when using the transifex-python library in my Django project.

Description

I have a models.TextChoices enum, and I want to translate the values thanks to Transifex Native.
Basically, I have that:

from django.db import models
from transifex.native.django import lazyt as _

class PossibleValues(models.TextChoices):
    VALUE_A = 'value_a', _('Value A')
    VALUE_B = 'value_b', _('Value B')
    VALUE_C = 'value_c', _('Value C')

When I run my code (starting the Django server for instance), I have this error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "lib/python3.8/site-packages/django/apps/config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "apps/app/choices.py", line 6, in <module>
    class PossibleValues(models.TextChoices):
  File "lib/python3.8/site-packages/django/db/models/enums.py", line 28, in __new__
    cls = super().__new__(metacls, classname, bases, classdict, **kwds)
  File "/usr/lib/python3.8/enum.py", line 243, in __new__
    enum_member = __new__(enum_class, *args)
TypeError: str() argument 2 must be str, not LazyString

Remarks

Remark 1: I previously used Django integrated i18n module (importing from django.utils.translation import gettext_lazy as _ instead) and it was working.

Remark 2: If I use from transifex.native.django import t as _ instead of the lazy version, I still have an error, even if it makes sense this time:

Traceback (most recent call last):
  [...]
  File "apps/app/choices.py", line 6, in <module>
    class PossibleValues(models.TextChoices):
  File "apps/app/choices.py", line 9, in PossibleValues
    VALUE_A = 'value_a', _('Value A')
  File "lib/python3.8/site-packages/transifex/native/django/utils/__init__.py", line 24, in translate
    return tx.translate(
  File "lib/python3.8/site-packages/transifex/native/core.py", line 100, in translate
    self._check_initialization()
  File "lib/python3.8/site-packages/transifex/native/core.py", line 234, in _check_initialization
    raise NotInitializedError(
transifex.native.core.NotInitializedError: TxNative is not initialized, make sure you call init() first.

So I HAVE to use the lazy method.

Versions

  • Python 3.8
  • Django 3.2
  • transifex-python 3.0.1
  • Linux 5.14 x86_64 / Ubuntu 20.04

Questions

It looks like having a LazyString instance as its value makes the models.TextChoices class unhappy (which is basically an enum.Enum subclass).

  • Is it a known issue?
  • Is there a way to translate models.TextChoices value with Transifex?
  • Is there a workaround?

Thank you!

Transifex native not working in combination with Django 3.x due to deprecated access

Steps to reproduce:

  • Install Django version 3.x or above
  • Install transifex python
  • Integrate using the steps in https://docs.transifex.com/django-sdk/quickstart-1
  • try to perform
    ./manage.py transifex push
  • The command returns error as the accessed information is deprecated since django 2.1+

Uses

  • Updating the accessed variables to allow usage with recent django versions would help in better adoption

Changes

  • transifex/native/django/management/commands/transifex.py
    from django.core.management import BaseCommand from django.core.management.base import CommandParser

  • transifex/native/django/templatetags/transifex.py
    old:
    from django.template.base import (BLOCK_TAG_END, BLOCK_TAG_START, COMMENT_TAG_END, COMMENT_TAG_START, TOKEN_BLOCK, TOKEN_COMMENT, TOKEN_TEXT, TOKEN_VAR, VARIABLE_TAG_END, VARIABLE_TAG_START) from django.template.defaulttags import token_kwargs

    new:
    import django.template.base as base
    from django.template.base import (BLOCK_TAG_END, BLOCK_TAG_START, COMMENT_TAG_END, COMMENT_TAG_START, VARIABLE_TAG_END, VARIABLE_TAG_START)
    eg: TOKEN_VAR accessed as
    base.TokenType.VAR
    similarly for TOKEN_BLOCK, TOKEN_COMMENT, TOKEN_TEXT, etc.

    • Similarly for token_kwargs, imported from base as base.token_kwargs

JsonApiException: (400, ["<JsonApiError: invalid - 'attributes' is a required property>"])

We're trying to get the translated po file via api v3.
We're following this documentation https://github.com/transifex/transifex-python/blob/devel/transifex/api/README.md#file-uploads-and-downloads
When calling: url = transifex_api.ResourceTranslationsAsyncDownload.download(resource=resource, language=language)

We get: JsonApiException: (400, ["<JsonApiError: invalid - 'attributes' is a required property>"])

Can't disable HTML escape when using lazy translation

Hi,

Just noticed that disabling HTML escape does not work with lazyt. It is basically due to a typo I think :)

Description

from transifex.native.django import lazyt

a = lazyt("I don't want to be HTML escaped!", _escape=False)

When no translation is available, with default policy returning the source string, we will have:

"I don't want to be HTML escaped!"

Expected result:

"I don't want to be HTML escaped!"

Hint

lazyt just calls lazy_translate which is implemented like that:

def lazy_translate(_string, _context=None, _escape=True, **params):
    return LazyString(
        translate,
        _string,
        _context=_context,
        escape=_escape,
        fallback_value=_string,
        **params
    )

Doing that, LazyString will just call the translate function with all provided **kwargs (except fallback_value).
The issue is that translate has this signature:

def translate(_string, _context=None, _escape=True, **params)

Note that parameter is called _escape where lasy_translate creates a LazyString with the escape kwarg (without the leading underscore _).
It results in the escape parameter passed to translate but just ignored, and _escape will fallback to its default value _escape=True.

Versions

Python 3.8
transifex-python 3.0.2
Linux 5.14 x86_64 / Ubuntu 20.04

Error when translation None is django template

In my django template i'm doing: {% t status.status %}

But sometimes status.status is None, and there is a nasty error that occurs. this should be ignored just like django {% trans status.status %} would do.

Transifex doesn't work with Django 4.0 or later.

Hi Transifex team,
We have a project on Django=4.1. added Transifex to my project, I received the error below. It seems the package does not support Django>=4.0 since based on the django 4.0 release note: "The undocumented TRANSLATOR_COMMENT_MARK constant is moved from django.template.base to django.utils.translation.template."

ImportError: cannot import name 'TRANSLATOR_COMMENT_MARK' from 'django.template.base' (/usr/local/lib/python3.9/site-packages/django/template/base.py)

Could you please consider my request to support Django>=4.0?

This is my traceback:

$ python src/manage.py transifex push
Traceback (most recent call last):
File "/opt/project/core/src/manage.py", line 97, in
main()
File "/opt/project/core/src/manage.py", line 93, in main
manager.main()
File "/opt/project/core/src/manage.py", line 87, in main
execute_from_command_line(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/init.py", line 446, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/init.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/init.py", line 279, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/local/lib/python3.9/site-packages/django/core/management/init.py", line 48, in load_command_class
module = import_module("%s.management.commands.%s" % (app_name, name))
File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in exec_module
File "", line 228, in _call_with_frames_removed
File "/usr/local/lib/python3.9/site-packages/transifex/native/django/management/commands/transifex.py", line 5, in
from transifex.native.django.management.utils.migrate import Migrate
File "/usr/local/lib/python3.9/site-packages/transifex/native/django/management/utils/migrate.py", line 11, in
from transifex.native.django.tools.migrations.templatetags import
File "/usr/local/lib/python3.9/site-packages/transifex/native/django/tools/migrations/templatetags.py", line 10, in
from django.template.base import TRANSLATOR_COMMENT_MARK, DebugLexer, Parser
ImportError: cannot import name 'TRANSLATOR_COMMENT_MARK' from 'django.template.base' (/usr/local/lib/python3.9/site-packages/django/template/base.py)

Thank you!

Support custom CDS handlers

Some parts of the TxNative class already allow the user to provide their own implementations of various components via dependency injection. The AbstractCache class being a good example of this. It would be good if users could also provide their own implementation of the CDSHandler class.

Users can already point the TxNative class at a different instance of the Transifex CDS via cds_host, and since the code for the CDS is available at https://github.com/transifex/transifex-delivery there's nothing stopping them creating their own CDS (using transifex-delivery as documentation on what endpoints need to be available and how they should behave) and pointing TxNative at this to provide their own implementation at the service level, rather than the application level. However, if your application handles content delivery, then you're forced to either split this out in a separate service (which might be a good thing, but equally might not be), or your application is forced to make requests to itself over localhost (which I really don't want to do - and I can't imagine anyone else would either).

Allowing users to provide their own CDSHandler would provide a lot more flexibility, allowing users to bolt on extra functionality - maybe the source strings are coming from different sources that don't have visibility of one another and you want to check for duplicates to remove wasted effort translating duplicate strings, or maybe you want to run some sort of analysis on the source strings before pushing them to the CDS. Maybe you want to use a local database for content delivery, meaning you don't need to push and pull the translations over HTTP at all. These are just some examples off the top of my head to try to justify the development effort required here.

If this is something that Transifex don't have resource to do, but are happy to adopt it should another contributor carry out the work, then I'm happy to fork and submit a PR for this if I can find some spare time to do so.

APIv3: Bulk actions for /resource_translations_async_downloads GET and POST?

Hi Transifex team,

We have a system where it creates local backs of all our translation material on a weekly basis. However, this script now takes many hours to complete, most of its time spent on creating resource_translations_async_downloads objects (POST) and then getting their status (GET).

Would it be possible to provide a Bulk Action endpoint for these two actions, considering that the they are Async APIs which makes them fairly lightweight already?

Not working with django model field verbose_name=lazyt("Title")

When i'm displaying a form and have set verbose_name=lazyt("Title") like this:

title = models.CharField(max_length=250, null=True, blank=True, unique=False, verbose_name=lazyt("Title"))

And i'm rendering a form:

class ContactForm(forms.ModelForm):
    """Form for the Contact model."""

    class Meta:
        model = Contact
        fields = [
            "contact_type",
            "company_name",
            "name",
        ]

The translations are not working/taken into account, it stays in English

CDS 404 Client Error

We have just installed transifex into our application and marking string for translations and pushing them to transifex to translate them works flawlessly.

However when I start my server, I get an error that it cannot retrieve translations from the CDS:

Error retrieving translations from CDS: `Not Found`
Error retrieving translations from CDS: UnknownError (`404 Client Error: Not Found for url: https://cds.svc.transifex.net/content/de`)
Error retrieving translations from CDS: `Not Found`
Error retrieving translations from CDS: UnknownError (`404 Client Error: Not Found for url: https://cds.svc.transifex.net/content/fr`)

I went through the documentation, but I was unable to find anything besides adding transifex to INSTALLED_APPS, and adding the token and password to the settings.py. Am I missing something?

Unable to pull from today

Hello,

From today when I do

tx pull -l fr --force --mode=reviewed

I get

tx INFO: Pulling translations for resource ngeo.ngeo-2_7 (source: .build/locale/ngeo.pot)
tx ERROR: invalid literal for int() with base 10: '100.0'

It looks like that there is an issue on the server side?

Python 3.12 support

Library installation fails on Python 3.12 due to incompatibility regarding configparser module. See python/cpython#89336.

        File "/private/var/folders/…/pip-install-…/transifex-python…/versioneer.py", line 1480, in get_version
          return get_versions()["version"]
                 ^^^^^^^^^^^^^^
        File "/private/var/folders/…/pip-install-…/transifex-python…/versioneer.py", line 1412, in get_versions
          cfg = get_config_from_root(root)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/private/var/…/pip-install-…/transifex-python…/versioneer.py", line 342, in get_config_from_root
          parser = configparser.SafeConfigParser()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      AttributeError: module 'configparser' has no attribute 'SafeConfigParser'. Did you mean: 'RawConfigParser'?

Tests is installed as package

When installing the package with pip, two packages are actually installed: transifex and tests (the latter containing the tests for the package). This might slows down the installation, takes up more space, and might create conflicts with any other tests modules.

The tests package can be prevented from being built and installed by a small change in setup.py (see PR #87)

transifex_api.ResourceTranslationsAsyncDownload.download fails with missing attributes error

Apologies if I am missing something obvious, but I was following examples given in https://github.com/transifex/transifex-python/blob/devel/transifex/api/README.md#file-uploads-and-downloads.

Steps to reproduce:

>>> from transifex.api import transifex_api
>>> transifex_api.setup(auth='my_token')
>>> org = transifex_api.Organization.get(slug='my_org')
>>> proj = org.fetch('projects').get(slug='my_project')
>>> lang = transifex_api.Language.get(code='am')
>>> resource = proj.fetch('resources').get(slug='my_resource')
>>> url = transifex_api.ResourceTranslationsAsyncDownload.download(resource=resource, language=lang)
Traceback (most recent call last):
  File "", line 1, in 
  File "c:\Python27\lib\site-packages\transifex\api\__init__.py", line 19, in download
    download = cls.create(*args, **kwargs)
  File "c:\Python27\lib\site-packages\transifex\api\jsonapi\resources.py", line 480, in create
    instance._save_new()
  File "c:\Python27\lib\site-packages\transifex\api\jsonapi\resources.py", line 415, in _save_new
    json={'data': payload})
  File "c:\Python27\lib\site-packages\transifex\api\jsonapi\apis.py", line 216, in request
    raise exc
transifex.api.jsonapi.exceptions.JsonApiException: (400, [u"<JsonApiError: invalid - 'attributes' is a required property>"])

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.