Skip to content

Commit

Permalink
Add android import support, fix import bug where plural form few is i…
Browse files Browse the repository at this point in the history
…gnored and keys are not converted to plural keys
  • Loading branch information
chrztoph committed Oct 28, 2024
1 parent 0c5e832 commit d8b994f
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 3 deletions.
6 changes: 4 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
"misogi.ruby-rubocop",
"esbenp.prettier-vscode",
"castwide.solargraph",
"rebornix.ruby",
"heaths.vscode-guid",
"connorshea.vscode-ruby-test-adapter",
"hbenl.vscode-test-explorer"
"hbenl.vscode-test-explorer",
"ms-azuretools.vscode-docker",
"Shopify.ruby-lsp",
"redhat.vscode-xml"
],

// "postCreateCommand": "bundle exec rails db:create db:migrate db:seed"
Expand Down
29 changes: 29 additions & 0 deletions app/lib/texterify/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def self.parse_file_content(_file_name, file_content, file_format)
json?(file_content, false)
elsif file_format == 'ios'
strings?(file_content)
elsif file_format == 'android'
android?(file_content)
elsif file_format == 'toml'
toml?(file_content)
elsif file_format == 'properties'
Expand Down Expand Up @@ -228,6 +230,33 @@ def self.strings?(content)
{ success: false, error_message: e.message }
end

def self.android?(content)
parsed = {}

xml = Nokogiri.XML(content)

# <string name="xxx">
string_elements = xml.css('resources string')
string_elements.map { |string_element| parsed[string_element['name']] = string_element.text }

# <plurals name="xxx">
plurals_elements = xml.css('resources plurals')
plurals_elements.map do |plural_element|
parsed[plural_element['name']] = {
other: plural_element.css("item[quantity='other']").text,
zero: plural_element.css("item[quantity='zero']").text,
one: plural_element.css("item[quantity='one']").text,
two: plural_element.css("item[quantity='two']").text,
few: plural_element.css("item[quantity='few']").text,
many: plural_element.css("item[quantity='many']").text
}
end

{ success: true, content: parsed }
rescue StandardError => e
{ success: false, error_message: e.message }
end

def self.toml?(content)
parsed = TomlRB.parse(content)

Expand Down
4 changes: 4 additions & 0 deletions app/models/import_file_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def differs_to_translation(translation)

zero_different || one_different || two_different || few_different || many_different || other_different
end

def plurals_content?
self.zero || self.one || self.two || self.few || self.many
end
end
8 changes: 8 additions & 0 deletions app/workers/import_import_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def perform(background_job_id, project_id, import_id, user_id)
key.project_id = project.id
end
key.description = import_file_translation.key_description

# If the imported translation has plural content convert the key to a plural key.
# If the key is a plural key don't convert to a simple key, because there might be
# plural content in some other language.
if import_file_translation.plurals_content?
key.pluralization_enabled = true
end

key.save!

# Update or create translation.
Expand Down
1 change: 1 addition & 0 deletions app/workers/import_verify_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def perform(background_job_id, _project_id, import_id)
translation.zero = json_value[:zero]
translation.one = json_value[:one]
translation.two = json_value[:two]
translation.few = json_value[:few]
translation.many = json_value[:many]
translation.other = json_value[:other]
else
Expand Down
2 changes: 1 addition & 1 deletion db/seeds/seeds_file_formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
{
format: 'android',
name: 'Android',
import_support: false,
import_support: true,
export_support: true,
plural_support: true,
skip_empty_plural_translations_support: true,
Expand Down
1 change: 1 addition & 0 deletions spec/cypress/e2e/import.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ context("import", () => {
{ id: "json-formatjs", file: "example_json_formatjs.json", searchFor: "json" },
{ id: "json-poeditor", file: "example_json_poeditor.json", searchFor: "json" },
{ id: "ios", file: "example_ios.strings", searchFor: "ios" },
{ id: "android", file: "example_android.xml", searchFor: "android" },
{ id: "toml", file: "example_toml.toml", searchFor: "toml" },
{ id: "rails", file: "example_rails.yml", searchFor: "rails" },
{ id: "properties", file: "example_properties.properties", searchFor: "prop" },
Expand Down
16 changes: 16 additions & 0 deletions spec/cypress/fixtures/example_android.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<resources>
<string name="my_title">My title</string>
<string name="my_description">My description</string>
<plurals name="plurals_item">
<item quantity="other">other text</item>
<item quantity="zero">zero text</item>
<item quantity="one">one text</item>
<item quantity="two">two text</item>
<item quantity="few">few text</item>
<item quantity="many">many text</item>
</plurals>
<plurals name="plurals_item_with_one_only">
<item quantity="one">123</item>
</plurals>
</resources>
16 changes: 16 additions & 0 deletions spec/fixtures/android/example_android.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<resources>
<string name="my_title">My title</string>
<string name="my_description">My description</string>
<plurals name="plurals_item">
<item quantity="other">other text</item>
<item quantity="zero">zero text</item>
<item quantity="one">one text</item>
<item quantity="two">two text</item>
<item quantity="few">few text</item>
<item quantity="many">many text</item>
</plurals>
<plurals name="plurals_item_with_one_only">
<item quantity="one">123</item>
</plurals>
</resources>
29 changes: 29 additions & 0 deletions spec/lib/texterify/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,33 @@
)
end
end

describe 'parses android' do
it 'parses android file' do
file = File.read('spec/fixtures/android/example_android.xml')
parse_result = Texterify::Import.parse_file_content('', file, 'android')
expect(parse_result[:content]).to eq(
{
'my_title' => 'My title',
'my_description' => 'My description',
'plurals_item' => {
other: 'other text',
zero: 'zero text',
one: 'one text',
two: 'two text',
few: 'few text',
many: 'many text'
},
'plurals_item_with_one_only' => {
other: '',
zero: '',
one: '123',
two: '',
few: '',
many: ''
}
}
)
end
end
end

0 comments on commit d8b994f

Please sign in to comment.