Skip to content

Commit 15c4183

Browse files
committed
Add documentation and few fixed as a result of writing the docs
1 parent a7ef08c commit 15c4183

21 files changed

+588
-103
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"gutignore",
2121
"lanczos",
2222
"maskable",
23+
"msgctxt",
2324
"neighbor",
2425
"npcs",
2526
"onready",

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ Ensure the following are installed:
1313

1414
- [Godot](https://godotengine.org/)
1515

16+
## Conventions
17+
18+
### GDScript
19+
20+
Parley does not enforce a strict style guide but where possible, it is
21+
recommended to follow the Godot GDScript style guide as described
22+
[here](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_styleguide.html).
23+
1624
## Documentation
1725

1826
Documentation is a vital part of Parley and should always be included for any

addons/parley/models/parley_context.gd

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ class_name ParleyContext extends Object
22

33

44
#region DEFS
5+
const Settings = preload("../settings.gd")
6+
const Constants = preload("../constants.gd")
7+
8+
59
## The current Dialogue Sequence AST of the Parley Context.
610
var dialogue_sequence_ast: ParleyDialogueSequenceAst = ParleyDialogueSequenceAst.new(): set = _set_dialogue_sequence_ast
711

@@ -28,9 +32,10 @@ signal dialogue_ended
2832

2933

3034
#region LIFECYCLE
31-
func _init(p_dialogue_sequence_ast: ParleyDialogueSequenceAst = ParleyDialogueSequenceAst.new(), data: Dictionary = {}) -> void:
35+
func _init(p_dialogue_sequence_ast: ParleyDialogueSequenceAst = ParleyDialogueSequenceAst.new(), data: Dictionary = {}, p_translation_mode: TranslationMode = TranslationMode.Auto) -> void:
3236
dialogue_sequence_ast = p_dialogue_sequence_ast
3337
p_data = data
38+
translation_mode = p_translation_mode
3439
#endregion
3540

3641

@@ -46,8 +51,11 @@ func _set_dialogue_sequence_ast(new_dialogue_sequence_ast: ParleyDialogueSequenc
4651

4752

4853
#region FACTORY
49-
static func create(p_dialogue_sequence_ast: ParleyDialogueSequenceAst, data: Dictionary = {}) -> ParleyContext:
50-
return ParleyContext.new(p_dialogue_sequence_ast, data)
54+
@warning_ignore("INT_AS_ENUM_WITHOUT_MATCH") # This is used as a default and handled within the function so this is fine to not match
55+
static func create(p_dialogue_sequence_ast: ParleyDialogueSequenceAst, data: Dictionary = {}, p_translation_mode: TranslationMode = -1 as TranslationMode) -> ParleyContext:
56+
var settings_translation_mode: TranslationMode = TranslationMode[Settings.get_setting(Constants.TRANSLATION_MODE)]
57+
var ctx_translation_mode: TranslationMode = p_translation_mode if p_translation_mode != -1 else settings_translation_mode
58+
return ParleyContext.new(p_dialogue_sequence_ast, data, ctx_translation_mode)
5159
#endregion
5260

5361

addons/parley/parley_export.gd

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,17 @@ static func _export(file_type: ParleyExportModal.FileType, export_type: ParleyEx
143143
continue
144144
if key_values.size() < 2:
145145
continue
146-
var value: String = key_values[1]
146+
var entry_locale_value: String = key_values[1]
147147

148148
# Handle existing lines
149149
var existing_line_index: int = existing_lines_map.get(entry_key, -1)
150150
if existing_line_index >= 0 and existing_line_index < lines_to_store.size():
151-
var column_index: int = headers.find(locale)
151+
var project_locale_column_index: int = headers.find(locale)
152152
var existing_line: PackedStringArray = lines_to_store[existing_line_index]
153-
var override_value: bool = false
154-
if column_index >= 0:
155-
var existing_value: String = existing_line[column_index]
156-
override_value = existing_value != value and value != entry_key
153+
var should_override_value: bool = false
154+
if project_locale_column_index >= 0:
155+
var existing_value: String = existing_line[project_locale_column_index]
156+
should_override_value = existing_value != entry_locale_value
157157

158158
var existing_line_to_store: PackedStringArray = PackedStringArray([key_values[0]])
159159
var index: int = 1
@@ -164,7 +164,7 @@ static func _export(file_type: ParleyExportModal.FileType, export_type: ParleyEx
164164
# TODO: support _notes
165165
var _result: int = existing_line_to_store.append(column_value)
166166
else:
167-
var _result: int = existing_line_to_store.append(value if override_value else column_value)
167+
var _result: int = existing_line_to_store.append(entry_locale_value if should_override_value else column_value)
168168
index += 1
169169
lines_to_store[existing_line_index] = existing_line_to_store
170170
continue
@@ -177,7 +177,7 @@ static func _export(file_type: ParleyExportModal.FileType, export_type: ParleyEx
177177
# TODO: support _notes
178178
var _result: int = new_line_to_store.append("")
179179
else:
180-
var _result: int = new_line_to_store.append(value)
180+
var _result: int = new_line_to_store.append(entry_locale_value)
181181
lines_to_store.append(new_line_to_store)
182182
_:
183183
var message: String = "Unable to export: Unknown file type (file_type:%s, export_type:%s)" % [file_type_name, export_type_name]

addons/parley/parley_import.gd

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@ static func import_dialogue_text_translation(file_type: ParleyImportModal.FileTy
1616
return [ERR_INVALID_DATA if file_result == OK else file_result, message]
1717

1818
var index: int = 0
19-
var key: StringName = ParleyUtils.translation.get_csv_key_name()
19+
var key_name: StringName = ParleyUtils.translation.get_csv_key_name()
2020
var locale: StringName = ParleyUtils.translation.get_locale()
21-
var headers: PackedStringArray = PackedStringArray([key, locale])
21+
var headers: PackedStringArray = PackedStringArray([key_name, locale])
2222
const key_index: int = 0
23-
var translation_index: int = 1
23+
var project_locale_column_index: int = headers.find(locale)
2424
var lines_map: Dictionary[String, String] = {}
2525
while !file.eof_reached():
2626
var line: PackedStringArray = file.get_csv_line()
2727
if line.size() >= 2 and line[0] != "":
2828
if index == 0:
2929
headers = line
30-
var found_translation_index: int = headers.find(locale)
30+
project_locale_column_index = headers.find(locale)
3131
# We assume that key index is always 0
32-
if found_translation_index > key_index:
32+
if project_locale_column_index > key_index:
3333
pass
3434
else:
3535
var message: String = "Unable to import: cannot find valid translation index: (headers:%s, line:%s, locale:%s)" % [headers, line, locale]
3636
push_error(ParleyUtils.log.error_msg(message))
3737
file.close()
3838
return [ERR_INVALID_DATA, message]
39-
elif line.size() >= translation_index:
40-
var translation_field: String = line[translation_index]
39+
elif project_locale_column_index >= 0 and project_locale_column_index < line.size():
40+
var translation_field: String = line[project_locale_column_index]
4141
lines_map[line[key_index]] = translation_field
4242
else:
43-
var message: String = "Unable to import: cannot find valid translation field: (headers:%s, line:%s, locale:%s)" % [headers, line, locale]
43+
var message: String = "Unable to import: cannot find valid translation field matching the project locale: (headers:%s, line:%s, locale:%s)" % [headers, line, locale]
4444
push_error(ParleyUtils.log.error_msg(message))
4545
file.close()
4646
return [ERR_INVALID_DATA, message]
@@ -49,10 +49,17 @@ static func import_dialogue_text_translation(file_type: ParleyImportModal.FileTy
4949

5050
var updated: bool = false
5151
for node_ast: ParleyNodeAst in dialogue_sequence_ast.nodes:
52+
if not [ParleyDialogueSequenceAst.Type.DIALOGUE, ParleyDialogueSequenceAst.Type.DIALOGUE_OPTION].has(node_ast.type):
53+
continue
54+
5255
var field: StringName = &"text"
5356
var text_translation_key: String = ParleyUtils.translation.get_msg_ctx(node_ast, field)
54-
var updated_text: String = lines_map.get(text_translation_key, "")
55-
if text_translation_key and updated_text and is_instance_of(updated_text, TYPE_STRING):
57+
var text: String = node_ast.get("text") if node_ast.get("text") else ""
58+
var key: String = text_translation_key if text_translation_key else text
59+
var updated_text: String = lines_map.get(key, "")
60+
if updated_text and is_instance_of(updated_text, TYPE_STRING) and text != updated_text:
61+
if not text_translation_key:
62+
ParleyUtils.translation.set_msg_ctx(node_ast, field, key)
5663
node_ast.set(field, updated_text)
5764
updated = true
5865

addons/parley/settings.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static var DEFAULT_SETTINGS: Dictionary = {
1515
ParleyConstants.ACTION_STORE_PATH: "res://actions/action_store.tres",
1616
ParleyConstants.FACT_STORE_PATH: "res://facts/fact_store.tres",
1717
# Internationalisation
18-
ParleyConstants.TRANSLATION_MODE: ParleyContext.TranslationMode.keys()[ParleyContext.TranslationMode.PO],
18+
ParleyConstants.TRANSLATION_MODE: ParleyContext.TranslationMode.keys()[ParleyContext.TranslationMode.Auto],
1919
ParleyConstants.TRANSLATION_CSV_HEADER_KEY: &"keys",
2020
# Test Dialogue Sequence
2121
# We can't preload this because of circular deps so let's

addons/parley/utils/parley_util.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class translation:
108108
return ""
109109

110110

111+
## Set the translation context for a node field.
112+
## If it can't be found, do nothing.
113+
static func set_msg_ctx(node: ParleyNodeAst, field: String, value: String, suffix: StringName = &"_translation_key") -> void:
114+
if not is_instance_of(node.get(field + suffix), TYPE_STRING):
115+
return
116+
return node.set(field + suffix, value)
117+
118+
111119
## Translate the input string
112120
static func translate(input: StringName) -> String:
113121
var instance: Object = new()

0 commit comments

Comments
 (0)