Skip to content

Commit

Permalink
Introduce platform setting to choose used locales
Browse files Browse the repository at this point in the history
Closes #75
  • Loading branch information
bkis committed Dec 18, 2023
1 parent 695f069 commit e9b68ab
Show file tree
Hide file tree
Showing 27 changed files with 265 additions and 159 deletions.
67 changes: 51 additions & 16 deletions Tekst-API/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3887,7 +3887,7 @@
"locale": {
"allOf": [
{
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
}
],
"description": "Locale indicating the translation language of this segment"
Expand Down Expand Up @@ -3943,7 +3943,7 @@
"title": "Title"
},
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
}
},
"type": "object",
Expand Down Expand Up @@ -3988,7 +3988,7 @@
"locale": {
"allOf": [
{
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
}
],
"description": "Locale indicating the translation language of this segment"
Expand Down Expand Up @@ -4055,7 +4055,7 @@
"locale": {
"anyOf": [
{
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
{
"type": "null"
Expand Down Expand Up @@ -4963,10 +4963,9 @@
"type": "object",
"title": "HTTPValidationError"
},
"Locale": {
"LocaleKey": {
"type": "string",
"enum": [
"*",
"deDE",
"enUS"
]
Expand Down Expand Up @@ -6025,7 +6024,7 @@
"PlatformDescriptionTranslation": {
"properties": {
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
"translation": {
"type": "string",
Expand All @@ -6044,7 +6043,7 @@
"PlatformNavInfoEntryTranslation": {
"properties": {
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
"translation": {
"type": "string",
Expand Down Expand Up @@ -6236,6 +6235,20 @@
"title": "Showfooterinfo",
"description": "Show platform title and description in footer",
"default": true
},
"availableLocales": {
"items": {
"$ref": "#/components/schemas/LocaleKey"
},
"type": "array",
"maxItems": 2,
"minItems": 1,
"title": "Availablelocales",
"default": [
"deDE",
"enUS"
],
"descriptions": "Locales available for use in platform client"
}
},
"additionalProperties": true,
Expand Down Expand Up @@ -6385,6 +6398,20 @@
"title": "Showfooterinfo",
"description": "Show platform title and description in footer",
"default": true
},
"availableLocales": {
"items": {
"$ref": "#/components/schemas/LocaleKey"
},
"type": "array",
"maxItems": 2,
"minItems": 1,
"title": "Availablelocales",
"default": [
"deDE",
"enUS"
],
"descriptions": "Locales available for use in platform client"
}
},
"type": "object",
Expand Down Expand Up @@ -6439,7 +6466,7 @@
"ResourceCategoryTranslation": {
"properties": {
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
"translation": {
"type": "string",
Expand All @@ -6458,7 +6485,7 @@
"ResourceCommentTranslation": {
"properties": {
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
"translation": {
"type": "string",
Expand All @@ -6477,7 +6504,7 @@
"ResourceDescriptionTranslation": {
"properties": {
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
"translation": {
"type": "string",
Expand Down Expand Up @@ -6603,7 +6630,7 @@
"TextLevelTranslation": {
"properties": {
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
"translation": {
"type": "string",
Expand Down Expand Up @@ -6746,7 +6773,7 @@
"TextSubtitleTranslation": {
"properties": {
"locale": {
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/TranslationLocaleKey"
},
"translation": {
"type": "string",
Expand Down Expand Up @@ -6852,6 +6879,14 @@
"type": "object",
"title": "TextUpdate"
},
"TranslationLocaleKey": {
"type": "string",
"enum": [
"deDE",
"enUS",
"*"
]
},
"UserCreate": {
"properties": {
"email": {
Expand Down Expand Up @@ -6914,7 +6949,7 @@
"locale": {
"anyOf": [
{
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/LocaleKey"
},
{
"type": "null"
Expand Down Expand Up @@ -6992,7 +7027,7 @@
"locale": {
"anyOf": [
{
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/LocaleKey"
},
{
"type": "null"
Expand Down Expand Up @@ -7182,7 +7217,7 @@
"locale": {
"anyOf": [
{
"$ref": "#/components/schemas/Locale"
"$ref": "#/components/schemas/LocaleKey"
},
{
"type": "null"
Expand Down
12 changes: 8 additions & 4 deletions Tekst-API/tekst/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ class Metadate(TypedDict):
]


# type alias for available locale identifiers
Locale = TypeAliasType("Locale", Literal["*", "deDE", "enUS"])
# type alias for available locale/language setting identifiers
_platform_locales = ("deDE", "enUS")
LocaleKey = TypeAliasType("LocaleKey", Literal[_platform_locales])
TranslationLocaleKey = TypeAliasType(
"TranslationLocaleKey", Literal[_platform_locales + ("*",)]
)

# Pydantic HttpUrl with string serialization
CustomHttpUrl = Annotated[
Expand All @@ -50,11 +54,11 @@ class Metadate(TypedDict):

# translations
class TranslationBase(TypedDict):
locale: Locale
locale: TranslationLocaleKey


T = TypeVar("T", bound=TranslationBase)
Translations = conlist(T, max_length=len(get_args(Locale.__value__)))
Translations = conlist(T, max_length=len(get_args(TranslationLocaleKey.__value__)))


class ModelTransformerMixin:
Expand Down
11 changes: 8 additions & 3 deletions Tekst-API/tekst/models/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from beanie import PydanticObjectId
from pydantic import BaseModel, Field, StringConstraints, model_validator

from tekst.models.common import DocumentBase, Locale, ModelBase, ModelFactoryMixin
from tekst.models.common import (
DocumentBase,
ModelBase,
ModelFactoryMixin,
TranslationLocaleKey,
)


class ClientSegment(ModelBase, ModelFactoryMixin):
Expand Down Expand Up @@ -32,7 +37,7 @@ class ClientSegment(ModelBase, ModelFactoryMixin):
Literal["wysiwyg", "html"], Field(description="Last used editor mode")
] = "wysiwyg"
locale: Annotated[
Locale,
TranslationLocaleKey,
Field(description="Locale indicating the translation language of this segment"),
]
title: Annotated[
Expand Down Expand Up @@ -64,7 +69,7 @@ class ClientSegmentHead(BaseModel):
id: PydanticObjectId
key: str
title: str | None = None
locale: Locale
locale: TranslationLocaleKey

class Settings:
projection = {"id": "$_id", "key": 1, "title": 1, "locale": 1}
11 changes: 10 additions & 1 deletion Tekst-API/tekst/models/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated
from typing import Annotated, get_args

from beanie import PydanticObjectId
from pydantic import EmailStr, Field, StringConstraints
Expand All @@ -8,6 +8,7 @@
from tekst.models.common import (
CustomHttpUrl,
DocumentBase,
LocaleKey,
ModelBase,
ModelFactoryMixin,
TranslationBase,
Expand Down Expand Up @@ -110,6 +111,14 @@ class PlatformSettings(ModelBase, ModelFactoryMixin):
show_footer_info: Annotated[
bool, Field(description="Show platform title and description in footer")
] = True
available_locales: Annotated[
list[LocaleKey],
Field(
descriptions="Locales available for use in platform client",
max_length=len(get_args(LocaleKey.__value__)),
min_length=1,
),
] = list(get_args(LocaleKey.__value__))


class PlatformSettingsDocument(PlatformSettings, DocumentBase):
Expand Down
4 changes: 2 additions & 2 deletions Tekst-API/tekst/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pymongo import IndexModel

from tekst.config import TekstConfig, get_config
from tekst.models.common import Locale, ModelBase, ModelFactoryMixin
from tekst.models.common import LocaleKey, ModelBase, ModelFactoryMixin


_cfg: TekstConfig = get_config()
Expand Down Expand Up @@ -48,7 +48,7 @@ class User(ModelBase, ModelFactoryMixin):
]
name: Annotated[str, StringConstraints(min_length=1, max_length=64)]
affiliation: Annotated[str, StringConstraints(min_length=1, max_length=64)]
locale: Locale | None = None
locale: LocaleKey | None = None
public_fields: Annotated[
list[PublicUserField], Field(description="Data fields set public by this user")
] = []
Expand Down
5 changes: 3 additions & 2 deletions Tekst-Web/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ export const prioritizedMetadataKeys = ['author', 'year', 'language'];
export type ErrorModel = components['schemas']['ErrorModel'];
export type Metadate = components['schemas']['Metadate'];
export type Metadata = Metadate[];
export type Locale = components['schemas']['Locale'];
export type LocaleKey = components['schemas']['LocaleKey'];
export type TranslationLocaleKey = components['schemas']['TranslationLocaleKey'];
export type Translation = {
locale: components['schemas']['Locale'];
locale: TranslationLocaleKey;
translation: string;
};

Expand Down
Loading

0 comments on commit e9b68ab

Please sign in to comment.