Skip to content

Commit

Permalink
Remove requirement for curly braces when defining key combos in trans…
Browse files Browse the repository at this point in the history
…lation values
  • Loading branch information
paulfioravanti committed Aug 1, 2024
1 parent 013d426 commit 18ecc1a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ create a platform-specific translation in your steno dictionaries.
Specify a translation for all possible (and unknown) platforms:

```json
"KP*EU": "{:PLATFORM:WINDOWS:\{#CONTROL(C)\}:MAC:\{#SUPER(C)\}:LINUX:\{#CONTROL(C)\}:OTHER:\{#CONTROL(C)\}}"
"KP*EU": "{:PLATFORM:WINDOWS:#CONTROL(C):MAC:#SUPER(C):LINUX:#CONTROL(C):OTHER:#CONTROL(C)}"
```

Specify a translation for only some platforms, and provide a default fallback
translation for any other platform:

```json
"KP*EU": "{:PLATFORM:MAC:\{#SUPER(C)\}:OTHER:\{#CONTROL(C)\}}"
"KP*EU": "{:PLATFORM:MAC:#SUPER(C):OTHER:#CONTROL(C)}"
```

Specify a translation for only some platforms, but without a fallback for other
platforms (will show an error if current platform is not found, but if you are
confident you know what platforms you work with, this should be fine):

```json
"KP*EU": "{:PLATFORM:WINDOWS:\{#CONTROL(C)\}:MAC:\{#SUPER(C)\}}"
"KP*EU": "{:PLATFORM:WINDOWS:#CONTROL(C):MAC:#SUPER(C)}"
```

Specify only a default fallback for other platforms (pointless, but supported):

```json
"KP*EU": "{:PLATFORM:OTHER:\{#CONTROL(C)\}}"
"KP*EU": "{:PLATFORM:OTHER:#CONTROL(C)}"
```

Note that the translation values are not limited to keyboard shortcuts, but can
Expand All @@ -80,10 +80,10 @@ contain any text to be output:
## Configuration

When a platform-specific translation is successfully determined from an outline,
the result is stored in the [Plover configuration directory][] on your machine
in a file called `platform_specific_translation.json`. This is done in order to
prevent determination actions from being done multiple times for the same
outline, and hence speed up translation lookups.
the result is stored in the local [Plover configuration directory][] on your
machine in a file called `platform_specific_translation.json`. This is done in
order to prevent determination actions from being done multiple times for the
same outline, and hence speed up lookups for already known translations.

You should not need to manually add any entries to the configuration, but if you
find any obsolete entries, feel free to delete them.
Expand Down
12 changes: 10 additions & 2 deletions plover_platform_specific_translation/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
- https://plover.readthedocs.io/en/latest/plugin-dev/extensions.html
- https://plover.readthedocs.io/en/latest/plugin-dev/meta.html
"""
import re
from pathlib import Path
from typing import Pattern

from plover.engine import StenoEngine
from plover.formatting import _Action, _Context
Expand All @@ -17,7 +19,8 @@
from . import translation


_CONFIG_FILEPATH = Path(CONFIG_DIR) / "platform_specific_translation.json"
_COMBO_TYPE: Pattern[str] = re.compile(r"#.*")
_CONFIG_FILEPATH: Path = Path(CONFIG_DIR) / "platform_specific_translation.json"

class PlatformSpecificTranslation:
"""
Expand Down Expand Up @@ -78,7 +81,12 @@ def _platform_specific_translation(
config.save(_CONFIG_FILEPATH, self._platform_translations)

action = ctx.new_action()
action.text = platform_translation

if _COMBO_TYPE.match(platform_translation):
action.combo = platform_translation.replace("#", "")
else:
action.text = platform_translation

return action

def _machine_state_changed(
Expand Down
12 changes: 6 additions & 6 deletions test/config/files/valid_platform_translations.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"platform_specific_translations": {
"{WINDOWS:{#CONTROL(C)}:MAC:{#SUPER(C)}:LINUX:{#CONTROL(C)}:OTHER:{#CONTROL(C)}}": "{#SUPER(C)}",
"{MAC:{#SUPER(C)}:OTHER:{#CONTROL(C)}}": "{#SUPER(C)}",
"{WINDOWS:{#CONTROL(C)}:MAC:{#SUPER(C)}}": "{#SUPER(C)}",
"{OTHER:{#CONTROL(C)}}": "{#CONTROL(C)",
"{WINDOWS:Hello:MAC:Hi:LINUX:Good day:OTHER:Whassup}": "Hi",
"{Windows:Goodbye:Mac:Bye:Linux:See you:Other:Sayonara}": "Bye"
"WINDOWS:#CONTROL(C):MAC:#SUPER(C):LINUX:#CONTROL(C):OTHER:#CONTROL(C)": "#SUPER(C)",
"MAC:#SUPER(C):OTHER:#CONTROL(C)": "#SUPER(C)",
"WINDOWS:#CONTROL(C):MAC:#SUPER(C)": "#SUPER(C)",
"OTHER:#CONTROL(C)": "#CONTROL(C)",
"WINDOWS:Hello:MAC:Hi:LINUX:Good day:OTHER:Whassup": "Hi",
"Windows:Goodbye:Mac:Bye:Linux:See you:Other:Sayonara": "Bye"
}
}
2 changes: 1 addition & 1 deletion test/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_loading_valid_config(valid_platform_translations_config_path):
config_platform_translations = (
config.load(valid_platform_translations_config_path)
)
assert config_platform_translations["{WINDOWS:Hello:MAC:Hi:LINUX:Good day:OTHER:Whassup}"] == "Hi"
assert config_platform_translations["WINDOWS:Hello:MAC:Hi:LINUX:Good day:OTHER:Whassup"] == "Hi"

def test_saving_config(valid_platform_translations_config_path):
platform_translations = {"foo": "bar"}
Expand Down

0 comments on commit 18ecc1a

Please sign in to comment.