Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable case-changing of language codes #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,43 @@ integrates with the Smartling translation platform.
}
```

If your project locales do not match those in Smartling (e.g. `ro` in your project, `ro-RO` in Smartling),
then you can provide a Wagtail locale id to Smartling locale id mapping via the `LOCALE_TO_SMARTLING_LOCALE` setting:
If your project locales do not match those in Smartling (e.g. `ro` in your project, `ro-RO` in Smartling),
then you can provide a Wagtail locale id to Smartling locale id mapping via the `LOCALE_TO_SMARTLING_LOCALE` setting:

```python
WAGTAIL_LOCALIZE_SMARTLING = {
"LOCALE_TO_SMARTLING_LOCALE": {
"ro": "ro-RO"
```python
WAGTAIL_LOCALIZE_SMARTLING = {
"LOCALE_TO_SMARTLING_LOCALE": {
"ro": "ro-RO"
}
}
}
```
```

... or you can specify a callable or a dotted path to a callable in the `LOCALE_MAPPING_CALLBACK` setting:
... or you can specify a callable or a dotted path to a callable in the `LOCALE_MAPPING_CALLBACK` setting:

```python
def map_project_locale_to_smartling(locale: str) -> str:
if locale == "ro":
return "ro-RO"
return locale
```python
def map_project_locale_to_smartling(locale: str) -> str:
if locale == "ro":
return "ro-RO"
return locale


WAGTAIL_LOCALIZE_SMARTLING = {
# ...
"LOCALE_MAPPING_CALLBACK": "settings.map_project_locale_to_smartling"
}
```
WAGTAIL_LOCALIZE_SMARTLING = {
# ...
"LOCALE_MAPPING_CALLBACK": "settings.map_project_locale_to_smartling"
}
```

Note that by default, when syncing translations, the project will attempt to reformat a mixed-case, Smartling-style language code (e.g. `zh-CN`) into a core Django all-lowercase style (`zh-cn`). Depending on how lang codes are set up in your project, this behaviour may not be appropriate. You can disable it via the `REFORMAT_LANGUAGE_CODES` setting, which defaults to `True`:

```python
WAGTAIL_LOCALIZE_SMARTLING = {
# ...
"REFORMAT_LANGUAGE_CODES": False
}

The callback receives a `WAGTAIL_CONTENT_LANGUAGES` local code string and is expected to return
a valid mapped locale id (or the original locale id).
```

4. Run migrations:

Expand Down
6 changes: 6 additions & 0 deletions src/wagtail_localize_smartling/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SmartlingSettings:
SMARTLING_LOCALE_TO_LOCALE: "dict[str, str]" = dataclasses.field(
default_factory=dict
)
REFORMAT_LANGUAGE_CODES: bool = True


def _init_settings() -> SmartlingSettings:
Expand Down Expand Up @@ -126,6 +127,11 @@ def _init_settings() -> SmartlingSettings:
v: k for k, v in LOCALE_TO_SMARTLING_LOCALE.items()
}

if "REFORMAT_LANGUAGE_CODES" in settings_dict:
settings_kwargs["REFORMAT_LANGUAGE_CODES"] = bool(
settings_dict["REFORMAT_LANGUAGE_CODES"]
)

return SmartlingSettings(**settings_kwargs)


Expand Down
10 changes: 10 additions & 0 deletions src/wagtail_localize_smartling/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ def format_smartling_locale_id(locale_id: str) -> str:
(e.g. "en-us") whereas Smartling uses lower case for the language code and
upper case for the region, if any (e.g. "en-US").

This behaviour is not applied if the REFORMAT_LANGUAGE_CODES setting is False

Also, this applies any mapping defined by the LOCALE_MAPPING_CALLBACK or
LOCALE_TO_SMARTLING_LOCALE settings.
"""
# Apply any mapping defined in settings
locale_id = smartling_settings.LOCALE_TO_SMARTLING_LOCALE.get(locale_id, locale_id)

if smartling_settings.REFORMAT_LANGUAGE_CODES is False:
return locale_id

# Reformat to match Smartling's format/casing
original_parts = locale_id.split("-")
if len(original_parts) == 1:
Expand All @@ -41,10 +46,15 @@ def format_smartling_locale_id(locale_id: str) -> str:
def format_wagtail_locale_id(locale_id: str) -> str:
"""
The opposite of format_smartling_locale_id, return everything lower case.

This behaviour is not applied if the REFORMAT_LANGUAGE_CODES setting is False
"""
# Apply any mapping defined in settings
locale_id = smartling_settings.SMARTLING_LOCALE_TO_LOCALE.get(locale_id, locale_id)

if smartling_settings.REFORMAT_LANGUAGE_CODES is False:
return locale_id

return locale_id.lower()


Expand Down