-
Notifications
You must be signed in to change notification settings - Fork 18
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
Handle multi language input fields in web ui #141
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a54d8fe
Handle multi language input fields in web ui
eighttrigrams 4cec4df
Wrap I18N.getTranslation
eighttrigrams 75b94c4
Add note
eighttrigrams c2a72a4
Add todo
eighttrigrams 36cb948
Lay out i18n field conversion
eighttrigrams b2f0f1d
Simplify test
eighttrigrams 4191775
Simplify test
eighttrigrams aad5f6e
Extract module
eighttrigrams 2e3b656
Don't touch field if already a map
eighttrigrams ca9d9a3
Handle simpleInput
eighttrigrams 5f56fe7
Handle multi input
eighttrigrams 6e14652
Write more compact
eighttrigrams 01c4d92
Improve test readability
eighttrigrams 60149e1
Merge branch 'master' into handle-multi-language-input-fields-in-ui
eighttrigrams 1c6e208
Display multi language field for dimension, in web ui
eighttrigrams a8511d8
Display multi language field for dating, in web ui
eighttrigrams 5307495
Change key order
eighttrigrams c85a7c9
Convert legacy dating fields to multi language
eighttrigrams 230a7b3
Convert legacy dimension fields to multi language
eighttrigrams 887857b
Remove log msg
eighttrigrams d10a5f5
Add unspecifiedLanguage to fallback USER_INTERFACE_LANGUAGES
eighttrigrams b4037a9
Merge remote-tracking branch 'origin/master' into handle-multi-langua…
eighttrigrams 3c6b6b8
Use text where instead multiInput
eighttrigrams 56145a8
Handle multiInputFields
eighttrigrams bb2a4a4
Add in 'unspecifiedLanguage' in initializeLanguages()
eighttrigrams 2d3cd16
Simplify getTranslation function
eighttrigrams c7dafb7
Update enricher.ex
eighttrigrams ac96e11
Remove unnessecary brackets in concat
eighttrigrams 141f6e3
Merge branch 'handle-multi-language-input-fields-in-ui' of https://gi…
eighttrigrams 3ff6617
Translate fields in ProjectHome
eighttrigrams c603def
Adjust some typings from string to I18N.String
eighttrigrams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
defmodule Api.Worker.Enricher.I18NFieldConverter do | ||
|
||
alias Api.Core.CategoryTreeList | ||
alias Api.Worker.Enricher.Utils | ||
|
||
defp convert_string(resource, field_name, field_value) when not is_map(field_value) do # this is from legacy project then | ||
put_in(resource, [field_name], %{ "unspecifiedLanguage" => field_value }) | ||
end | ||
defp convert_string resource, _field_name, _field_value do | ||
resource | ||
end | ||
|
||
defp convert_string_array_item(item) when not is_map(item) do # legacy | ||
%{ "unspecifiedLanguage" => item } | ||
end | ||
defp convert_string_array_item item do | ||
item | ||
end | ||
|
||
defp convert_string_array(resource, field_name, field_value) do | ||
put_in(resource, [field_name], Enum.map(field_value, &convert_string_array_item/1)) | ||
end | ||
|
||
defp convert_dating_source(dating_item_source) when not is_map(dating_item_source) do # from legacy project | ||
# TODO review, here we use keyword, above we use string | ||
%{ unspecifiedLanguage: dating_item_source } | ||
end | ||
defp convert_dating_source dating_item_source do | ||
dating_item_source | ||
end | ||
|
||
defp convert_dating_item dating_item do | ||
put_in(dating_item.source, convert_dating_source(dating_item.source)) | ||
end | ||
|
||
defp convert_dating resource, field_name, field_value do | ||
put_in(resource, [field_name], Enum.map(field_value, &convert_dating_item/1)) | ||
end | ||
|
||
defp convert_dimension_measurement_comment(dimension_measurement_comment) when not is_map(dimension_measurement_comment) do # legacy project | ||
%{ unspecifiedLanguage: dimension_measurement_comment } | ||
end | ||
defp convert_dimension_measurement_comment dimension_measurement_comment do | ||
dimension_measurement_comment | ||
end | ||
|
||
defp convert_dimension_item dimension_item do | ||
put_in(dimension_item.measurementComment, convert_dimension_measurement_comment(dimension_item.measurementComment)) | ||
end | ||
|
||
defp convert_dimension resource, field_name, field_value do | ||
put_in(resource, [field_name], Enum.map(field_value, &convert_dimension_item/1)) | ||
end | ||
|
||
defp convert_resource_field category_definition_groups do | ||
fn {field_name, field_value}, resource -> | ||
field_definition = Utils.get_field_definition category_definition_groups, Atom.to_string(field_name) | ||
|
||
if is_nil(field_definition[:inputType]) do | ||
resource | ||
else | ||
cond do | ||
field_definition.inputType == "dating" -> | ||
convert_dating resource, field_name, field_value | ||
|
||
field_definition.inputType == "dimension" -> | ||
convert_dimension resource, field_name, field_value | ||
|
||
field_definition.inputType == "multiInput" | ||
or field_definition.inputType == "simpleMultiInput" -> | ||
convert_string_array resource, field_name, field_value | ||
|
||
field_definition.inputType == "input" | ||
or field_definition.inputType == "simpleInput" | ||
or field_definition.inputType == "text" -> | ||
convert_string resource, field_name, field_value | ||
|
||
true -> | ||
resource | ||
end | ||
end | ||
end | ||
end | ||
|
||
def convert_category change = %{ doc: %{ resource: resource } }, category_definition_groups do | ||
resource = Enum.reduce(resource, resource, convert_resource_field(category_definition_groups)) | ||
put_in(change.doc.resource, resource) | ||
end | ||
|
||
def convert change, configuration do | ||
name = change.doc.resource.category.name # TODO review category.name, maybe document the expectation | ||
category_definition = CategoryTreeList.find_by_name(name, configuration) | ||
convert_category change, category_definition.groups | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Api.Worker.Enricher.Utils do | ||
|
||
# TODO extract | ||
def get_field_definition(category_definition_groups, field_name) do | ||
group = Enum.find(category_definition_groups, &get_field_definition_from_group(&1, field_name)) | ||
get_field_definition_from_group(group, field_name) | ||
end | ||
|
||
defp get_field_definition_from_group(%{ fields: fields }, field_name) do | ||
Enum.find(fields, fn field -> field.name == field_name end) | ||
end | ||
defp get_field_definition_from_group(_, _), do: nil | ||
end |
116 changes: 116 additions & 0 deletions
116
web/api/test/unit/worker/enricher/i18n_field_converter_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule Api.Worker.Enricher.I18NFieldConverterTest do | ||
|
||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
alias Api.Worker.Enricher.I18NFieldConverter | ||
|
||
test "convert input, simpleInput, multiInput, simpleMultiInput" do | ||
change = %{ | ||
doc: %{ | ||
resource: %{ | ||
category: %{ | ||
name: "Trench" | ||
}, | ||
legacyInputField: "hallo", | ||
inputField: %{ "de" => "hallo-de" }, | ||
simpleInputField: "hallo-simple-input", | ||
legacyTextField: "hallo\ntext", | ||
textField: %{ "de" => "hallo-de\ntext" }, | ||
legacyMultiInputField: ["a", "b"], | ||
multiInputField: [%{ "de" => "a" }, %{ "de" => "b" }], | ||
simpleMultiInputField: ["a", "b"] | ||
} | ||
}, | ||
} | ||
category_definition_groups = | ||
[ | ||
%{ | ||
fields: [ | ||
%{ inputType: "input", name: "legacyInputField" }, | ||
%{ inputType: "input", name: "inputField" }, | ||
%{ inputType: "simpleInput", name: "simpleInputField" }, | ||
%{ inputType: "text", name: "legacyTextField" }, | ||
%{ inputType: "text", name: "textField" }, | ||
%{ inputType: "multiInput", name: "legacyMultiInputField" }, | ||
%{ inputType: "multiInput", name: "multiInputField" }, | ||
%{ inputType: "simpleMultiInput", name: "simpleMultiInputField" } | ||
] | ||
} | ||
] | ||
|
||
resource = (I18NFieldConverter.convert_category change, category_definition_groups).doc.resource | ||
|
||
assert %{ "unspecifiedLanguage" => "hallo" } == resource.legacyInputField | ||
assert %{ "de" => "hallo-de" } == resource.inputField | ||
assert %{ "unspecifiedLanguage" => "hallo-simple-input" } == resource.simpleInputField | ||
assert %{ "unspecifiedLanguage" => "hallo\ntext" } == resource.legacyTextField | ||
assert %{ "de" => "hallo-de\ntext" } == resource.textField | ||
assert [%{ "unspecifiedLanguage" => "a" }, %{ "unspecifiedLanguage" => "b" }] == resource.legacyMultiInputField | ||
assert [%{ "de" => "a" }, %{ "de" => "b" }] == resource.multiInputField | ||
assert [%{ "unspecifiedLanguage" => "a" }, %{ "unspecifiedLanguage" => "b" }] == resource.simpleMultiInputField | ||
end | ||
|
||
test "convert dating" do | ||
category_definition_groups = | ||
[ | ||
%{ | ||
fields: [ | ||
%{ inputType: "dating", name: "datingField" }, | ||
%{ inputType: "dating", name: "legacyDatingField" }, | ||
] | ||
} | ||
] | ||
change = %{ | ||
doc: %{ | ||
resource: %{ | ||
category: %{ | ||
name: "Trench" | ||
}, | ||
datingField: [%{ | ||
source: %{de: "Eine Datierung", en: "A Dating"} | ||
}], | ||
legacyDatingField: [%{ | ||
source: "Eine Datierung" | ||
}] | ||
} | ||
}, | ||
} | ||
|
||
resource = (I18NFieldConverter.convert_category change, category_definition_groups).doc.resource | ||
|
||
assert %{ de: "Eine Datierung", en: "A Dating" } == (List.first resource.datingField).source | ||
assert %{ unspecifiedLanguage: "Eine Datierung" } == (List.first resource.legacyDatingField).source | ||
end | ||
|
||
test "convert dimension" do | ||
category_definition_groups = | ||
[ | ||
%{ | ||
fields: [ | ||
%{ inputType: "dimension", name: "dimensionField" }, | ||
%{ inputType: "dimension", name: "legacyDimensionField" }, | ||
] | ||
} | ||
] | ||
change = %{ | ||
doc: %{ | ||
resource: %{ | ||
category: %{ | ||
name: "Trench" | ||
}, | ||
dimensionField: [%{ | ||
measurementComment: %{de: "Eine Abmessung", en: "A dimension"} | ||
}], | ||
legacyDimensionField: [%{ | ||
measurementComment: "Eine Abmessung" | ||
}] | ||
} | ||
}, | ||
} | ||
|
||
resource = (I18NFieldConverter.convert_category change, category_definition_groups).doc.resource | ||
|
||
assert %{ de: "Eine Abmessung", en: "A dimension" } == (List.first resource.dimensionField).measurementComment | ||
assert %{ unspecifiedLanguage: "Eine Abmessung" } == (List.first resource.legacyDimensionField).measurementComment | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Das TODO kann raus, oder?