introduce locale (#165)

* introduced locale option

* added tests
This commit is contained in:
Simone Robutti 2022-04-11 18:13:35 +02:00 committed by GitHub
parent 4e40a1979e
commit 94c85d8b48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 50 additions and 18 deletions

View File

@ -20,6 +20,8 @@ base_validators = [
# url of the main Mobilizon instance to download events from # url of the main Mobilizon instance to download events from
Validator("source.mobilizon.url", must_exist=True, is_type_of=str), Validator("source.mobilizon.url", must_exist=True, is_type_of=str),
Validator("source.mobilizon.group", must_exist=True, is_type_of=str), Validator("source.mobilizon.group", must_exist=True, is_type_of=str),
Validator("db_path", must_exist=True, is_type_of=str),
Validator("locale", must_exist=True, is_type_of=str, default="en-us"),
] ]
activeness_validators = [ activeness_validators = [

View File

@ -6,6 +6,8 @@ from uuid import UUID
import arrow import arrow
from jinja2 import Template from jinja2 import Template
from mobilizon_reshare.config.config import get_settings
class EventPublicationStatus(IntEnum): class EventPublicationStatus(IntEnum):
WAITING = 1 WAITING = 1
@ -43,7 +45,8 @@ class MobilizonEvent:
] ]
def _fill_template(self, pattern: Template) -> str: def _fill_template(self, pattern: Template) -> str:
return pattern.render(**asdict(self)) config = get_settings()
return pattern.render(locale=config["locale"], **asdict(self))
def format(self, pattern: Template) -> str: def format(self, pattern: Template) -> str:
return self._fill_template(pattern) return self._fill_template(pattern)

View File

@ -1,6 +1,6 @@
{{ name }} {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,6 +1,6 @@
{{ name }} {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,6 +1,6 @@
{{ name }} {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,5 +1,5 @@
{{ name }} {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %}📍 {{ location }}{% endif %} {% if location %}📍 {{ location }}{% endif %}
🔗 {{mobilizon_link}} 🔗 {{mobilizon_link}}

View File

@ -1,6 +1,6 @@
<strong>{{ name }}</strong> <strong>{{ name }}</strong>
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %}📍 {{ location }}{% endif %} {% if location %}📍 {{ location }}{% endif %}
{{ description }} {{ description }}

View File

@ -1,5 +1,5 @@
<strong>{{ name }}</strong> <strong>{{ name }}</strong>
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %}📍 {{ location }}{% endif %} {% if location %}📍 {{ location }}{% endif %}
🔗 <a href="{{mobilizon_link}}">Link</a> 🔗 <a href="{{mobilizon_link}}">Link</a>

View File

@ -1,6 +1,6 @@
{{ name }} {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,5 +1,5 @@
{{ name }} {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}
{% endif %} {% endif %}

View File

@ -1,6 +1,6 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,6 +1,6 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,10 +1,8 @@
[default] [default]
debug = true
default = true
local_state_dir = "/var/mobilizon_reshare" local_state_dir = "/var/mobilizon_reshare"
log_dir = "/var/log/mobilizon_reshare"
db_name = "events.db" db_name = "events.db"
db_path = "@format {this.local_state_dir}/{this.db_name}" db_path = "@format {this.local_state_dir}/{this.db_name}"
locale= "en-us"
[default.source.mobilizon] [default.source.mobilizon]
url="https://some_mobilizon" url="https://some_mobilizon"
@ -34,7 +32,7 @@ stream = "ext://sys.stderr"
level = "DEBUG" level = "DEBUG"
class = "logging.handlers.RotatingFileHandler" class = "logging.handlers.RotatingFileHandler"
formatter = "standard" formatter = "standard"
filename = "@format {this.log_dir}/mobilizon_reshare.log" filename = "/var/log/mobilizon_reshare/mobilizon_reshare.log"
maxBytes = 52428800 maxBytes = 52428800
backupCount = 500 backupCount = 500
encoding = "utf8" encoding = "utf8"

View File

@ -1,13 +1,33 @@
import pytest import pytest
from jinja2 import Template from jinja2 import Template
from mobilizon_reshare.config.config import get_settings
@pytest.fixture()
def set_locale(locale):
settings = get_settings()
old_locale = settings["locale"]
yield get_settings().update({"locale": locale})
settings.update({"locale": old_locale})
@pytest.fixture() @pytest.fixture()
def simple_template(): def simple_template():
return Template( return Template(
( (
"{{name}}|{{description}}|{{location}}|{{begin_datetime.strftime('%Y-%m-%d, %H:%M')}}" "{{name}}|{{description}}|{{location}}|{{begin_datetime.strftime('%Y-%m-%d, %H:%M')}}"
"|{{last_update_time.strftime('%Y-%m-%d, %H:%M')}}" "|{{end_datetime.strftime('%Y-%m-%d, %H:%M')}}"
)
)
@pytest.fixture()
def template_with_locale():
return Template(
(
"{{name}}|{{description}}|{{location}}|{{begin_datetime.format('DD MMMM, HH:mm', locale=locale)}}"
"|{{end_datetime.format('DD MMMM, HH:mm', locale=locale)}}"
) )
) )
@ -15,12 +35,21 @@ def simple_template():
def test_fill_template(event, simple_template): def test_fill_template(event, simple_template):
assert ( assert (
event._fill_template(simple_template) event._fill_template(simple_template)
== "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 11:30" == "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 12:30"
) )
def test_format(event, simple_template): def test_format(event, simple_template):
assert ( assert (
event.format(simple_template) event.format(simple_template)
== "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 11:30" == "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 12:30"
)
@pytest.mark.parametrize("locale", ["it-it"])
def test_locale(event, template_with_locale, set_locale):
assert (
event.format(template_with_locale)
== "test event|description of the event|location|01 gennaio, 11:30|01 gennaio, 12:30"
) )