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

Recursively translating yaml's with lists does not work #456

Closed
rscherf opened this issue May 26, 2022 · 3 comments
Closed

Recursively translating yaml's with lists does not work #456

rscherf opened this issue May 26, 2022 · 3 comments

Comments

@rscherf
Copy link

rscherf commented May 26, 2022

What an incredibly useful gem! Thank you for building/maintaining it! 👏

I have a somewhat complex translation yaml schema, which includes lists/sequences. Consider the following simple example:

en:
  test:
    one: Will this translate?
    two:
    - title: How about this?
      body: Or this?
    three:
      four:
      - title: How about this level?
        body: And this?
    five:
      six: This translates
      seven:
        eight: And this does as well

Then, when running i18n-tasks translate-missing, I receive this (alphabetized for simplicity):

de: 
  test:
    one: Wird das übersetzt?
    two:
    - title: How about this?
      body: Or this?
    three:
      four:
      - title: How about this level?
        body: And this?
    five:
      seven:
        eight: Und dies auch
      six: Dies übersetzt

Is there a mechanism for auto-translating all these keys, including those with a - dash prefix? Am I missing something? Can I force the gem to recognize this structure?

Thanks for all you do!

@rscherf
Copy link
Author

rscherf commented May 26, 2022

FWIW, I also tried with various [] and {}:

en:
  test:
    one: Will this translate?
    two: {
      title: How about this?,
      body: Or this?
    }
    three:
      four: {
        title: How about this level?,
        body: And this?
      }
    five:
      six: This translates
      seven:
        eight: And this does as well
        nine: [
          {
            title: The title,
            body: A body,
          },
          {
            title: Another title,
            body: Another body,
          },
        ]

to

de:
  test:
    one: Wird das übersetzt?
    two:
      body: Oder dieses?
      title: Wie wäre es damit?
    three:
      four:
        body: Und das?
        title: Wie wäre es mit diesem Niveau?
    five:
      seven:
        eight: Und dies auch
        nine:
        - title: The title
          body: A body
        - title: Another title
          body: Another body
      six: Dies übersetzt

Which is obviously closer, but still can't handle multiple list items.

@rscherf
Copy link
Author

rscherf commented May 26, 2022

And after even further testing, the following does work:

en:
  test:
    - Translate this
    - Then this
    - And after, this

Properly translates to:

de:
  test:
    - Übersetze das
    - Dann das
    - Und danach das

So naturally, I tried:

en:
  test:
    - {
      item: The first item,
      title: The first title
    }
    - {
      item: the second thing,
      title: the second title
    }

And got 😩

de:
  test:
  - item: The first item
    title: The first title
  - item: the second thing
    title: the second title

@rscherf rscherf changed the title Translating yaml's with lists Recursively translating yaml's with lists does not work May 26, 2022
@glebm
Copy link
Owner

glebm commented May 27, 2022

The translator only handles arrays and strings at the moment

If you'd like to attempt a fix by adding Hash support, the code is here

# Prepare value for translation.
# @return [String, Array<String, nil>, nil] value for Google Translate or nil for non-string values
def dump_value(value)
case value
when Array
# dump recursively
value.map { |v| dump_value v }
when String
replace_interpolations value unless value.empty?
end
end
# Parse translated value from the each_translated enumerator
# @param [Object] untranslated
# @param [Enumerator] each_translated
# @return [Object] final translated value
def parse_value(untranslated, each_translated)
case untranslated
when Array
# implode array
untranslated.map { |from| parse_value(from, each_translated) }
when String
if untranslated.empty?
untranslated
else
restore_interpolations untranslated, each_translated.next
end
else
untranslated
end
end

georf added a commit to Lichtbit/i18n-tasks that referenced this issue Nov 13, 2023
With reference to ticket glebm#456, this change now allows you to translate lists of hashes with DeepL and other translators.
georf added a commit to Lichtbit/i18n-tasks that referenced this issue Nov 13, 2023
With reference to ticket glebm#456, this change now allows you to translate lists of hashes with DeepL and other translators.
glebm pushed a commit that referenced this issue Nov 21, 2023
With reference to ticket #456, this change now allows you to translate lists of hashes with DeepL and other translators.
@glebm glebm closed this as completed Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants