glebm / i18n-tasks

Manage translation and localization with static analysis, for Ruby i18n
http://glebm.github.io/i18n-tasks
MIT License
2.06k stars 260 forks source link

Newline placeholder `<br id=i18n />` is not replaced with newline after translation #595

Open paulwalk opened 2 weeks ago

paulwalk commented 2 weeks ago

I am finding the <br id=i18n /> in my translated locales. I understand from this recent commit that:

It is the "and then back after translating" part which does not seem to be working.

davidwessman commented 2 weeks ago

I am finding the <br id=i18n /> in my translated locales. I understand from this recent commit that:

  • Line breaks are not kept when translating html content via Google Translate

  • Therefore we transform \n into a placeholder and then back after

translating

It is the "and then back after translating" part which does not seem to be working.

Could you try to write a test case which triggers it? Or try edit existing tests?

paulwalk commented 2 weeks ago

I'm afraid that I'm not competent to write a unit test for this, but I can offer an example that fails:

en.yml

---
en:
  test_html_translation:
    some_html: |-
      <p>Each repository in this directory is characterised by the following features:</p>
      <ul>
        <li>The majority of its content is fully Open Access</li>
        <li>It has a functioning OAI-PMH interface</li>
      </ul>

Running i18n-tasks translate-missing generates the following file (Spanish example):

es.yml

---
es:
  test_html_translation:
    some_html: "<p>Cada repositorio de este directorio se caracteriza por las siguientes características:</p><br id=i18n /><ul><br id=i18n /><li> La mayor parte de su contenido es totalmente de acceso abierto.</li><br id=i18n /><li> Tiene una interfaz OAI-PMH funcional</li><br id=i18n /></ul>"
paulwalk commented 2 weeks ago

I have just discovered that naming the key *_html seems to be what triggers this.

So, running the same experiment, with a new key called some_text, but containg the exact same content as some_html, gives the following:

en.yml

---
en:
  test_html_translation:
    some_html: |-
      <p>Each repository in this directory is characterised by the following features:</p>
      <ul>
        <li>The majority of its content is fully Open Access</li>
        <li>It has a functioning OAI-PMH interface</li>
      </ul>
    some_text: |-
      <p>Each repository in this directory is characterised by the following features:</p>
      <ul>
        <li>The majority of its content is fully Open Access</li>
        <li>It has a functioning OAI-PMH interface</li>
      </ul>

gives:

es.yml

---
es:
  test_html_translation:
    some_html: "<p>Cada repositorio de este directorio se caracteriza por las siguientes características:</p><br id=i18n /><ul><br id=i18n /><li> La mayor parte de su contenido es totalmente de acceso abierto.</li><br id=i18n /><li> Tiene una interfaz OAI-PMH funcional</li><br id=i18n /></ul>"
    some_text: |-
      <p>Cada repositorio de este directorio se caracteriza por las siguientes características:</p>
      <ul>
      <li>La mayor parte de su contenido es de acceso abierto completo</li>
      <li>Tiene una interfaz OAI-PMH funcional</li>
      </ul>

So, simply avoiding the use of keys ending in *_html is enough to resolve this!

glebm commented 2 weeks ago

_html is a special suffix in that it makes Rails automatically mark such values as html_safe, so renaming the key is not ideal.

@davidwessman Can you please have a look?

glebm commented 2 weeks ago

I think the issue may be here:

https://github.com/glebm/i18n-tasks/blob/2cba1093e3c555b6664f62604a2e2f2dfe6f1a6e/lib/i18n/tasks/translators/google_translator.rb#L80

There probably shouldn't be a space after #{NEWLINE_PLACEHOLDER}.

davidwessman commented 5 days ago

I have been swamped, but it is on my list unless someone is faster than me!

davidwessman commented 1 day ago

@glebm Do you somehow have access to Google Translate API that I can use?

davidwessman commented 23 hours ago

@paulwalk can you please try out https://github.com/glebm/i18n-tasks/pull/597?