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

Translating with HTML #23

Closed
stewardjornsen opened this issue May 14, 2015 · 14 comments
Closed

Translating with HTML #23

stewardjornsen opened this issue May 14, 2015 · 14 comments

Comments

@stewardjornsen
Copy link

Thanks for the translator script. I think the issue is more with Google than with your script. if html is translated, ends up as </ li> which isn't right. So one can't translate an article that has paragraphs and html elements without it breaking. So my question is more of a tip:

Can I possibly translate html without damaging the html codes? If yes, how? Any suggestion is appreciated.

@Stichoza
Copy link
Owner

Yes, this is a problem with Google Translate itself.

I guess you can write a regex (or just str_replace) to replace < / with </ (without space). But anyways, do not depend on "hacks" like that.

@Stichoza
Copy link
Owner

You should do it with regex as the space is after /.

@stewardjornsen
Copy link
Author

Any examples? It really misses up everything...

On Thu, May 14, 2015 at 8:03 PM, Levan Velijanashvili <
[email protected]> wrote:

You should do it with regex as the space is after /.


Reply to this email directly or view it on GitHub
#23 (comment)
.

Steward Godwin Jornsen
+234.803.9468.290
StewardXpress Nigeria Limited
www.stewardxpress.com

In His Name Bible Church
www.inhisnamebiblechurch.com http://www.ihnbc.com

This mail should only be used for the purpose of which it was sent and by
the receiver of this mail only. We guarantee that this mail is sent free of
all viruses or spyware. You are advised to scan attachments before opening.
StewardXpress/In His Name Bible Church is not responsible for how you used
the provided data.

@Stichoza
Copy link
Owner

$result = $t->translate('html string here');
$result_with_correct_html = preg_replace('/\/\s/', '/', $result);

That's it.

@stewardjornsen
Copy link
Author

Thank you very much. I shall give that a try. If that works great, you
could as well extend the class to have that html return object. That could
be cool, don't you think?

Thanks again.

On Wed, May 20, 2015 at 11:00 AM, Levan Velijanashvili <
[email protected]> wrote:

$result = $t->translate('html string here');$result_with_correct_html = preg_replace('//\s/', '/', $result);

That's it.


Reply to this email directly or view it on GitHub
#23 (comment)
.

Steward Godwin Jornsen
+234.803.9468.290
StewardXpress Nigeria Limited
www.stewardxpress.com

In His Name Bible Church
www.inhisnamebiblechurch.com http://www.ihnbc.com

This mail should only be used for the purpose of which it was sent and by
the receiver of this mail only. We guarantee that this mail is sent free of
all viruses or spyware. You are advised to scan attachments before opening.
StewardXpress/In His Name Bible Church is not responsible for how you used
the provided data.

@Stichoza
Copy link
Owner

I really appreciate your idea, but I don't think so. This package has a very simple API - just three methods and that's it. This is not intended to translate HTML strings. If so, then why not add JSON or XML support? It will lead to very impure code. Bringing back HTML tags is one simple regex (noted above). It's not worth adding extra functionality for that. Plus, I'm not sure this regex will fix all problems with translated HTML.

P.S I won't close this issue for now. Let it be open for further public discussions.

@Stichoza
Copy link
Owner

The official paid API has format attribute where you can set that translated text is HTML.

So if we could find any way* to make it work with HTML strings without damaging tags and avoiding usage of extra "hacky" regexes, it would be really cool.

* - Maybe it can be achieved using some attribute in URL string. The actual translate.google.com doesn't have option to set type to HTML, so I'm not sure we can find an easy solution for this

@stewardjornsen
Copy link
Author

Thanks,
No point anyway. Google sometimes translates the names of classes and Tag
ID which does seriously scramble website functionality.

Steward

On Thu, May 21, 2015 at 1:01 AM, Levan Velijanashvili <
[email protected]> wrote:

The official paid API has format attribute
https://cloud.google.com/translate/v2/using_rest#query-params where you
can set that translated text is HTML.

So if we could find any way* to make it work with HTML strings without
damaging tags and avoiding usage of extra "hacky" regexes, it would be
really cool.

* - Maybe it can be achieved using some attribute in URL string

'client' => 't',
'text' => null, // String
'hl' => 'en',
'sl' => null, // Source language
'tl' => null, // Target language
'ie' => 'UTF-8', // Input encoding
'oe' => 'UTF-8', // Output encoding
'multires' => '1',
'otf' => '0',
'pc' => '1',
'trs' => '1',
'ssel' => '0',
'tsel' => '0',
'sc' => '1'
.


Reply to this email directly or view it on GitHub
#23 (comment)
.

Steward Godwin Jornsen
+234.803.9468.290
StewardXpress Nigeria Limited
www.stewardxpress.com

In His Name Bible Church
www.inhisnamebiblechurch.com http://www.ihnbc.com

This mail should only be used for the purpose of which it was sent and by
the receiver of this mail only. We guarantee that this mail is sent free of
all viruses or spyware. You are advised to scan attachments before opening.
StewardXpress/In His Name Bible Church is not responsible for how you used
the provided data.

@Stichoza
Copy link
Owner

That's exactly what I mean "without damaging tags". Yea, I think there's no way to translate HTML using this service url.

@dimaslanjaka
Copy link

Can you make autoload versions ?. There i using hosting. Not supported for installing new php library

@vielhuber
Copy link

The official API has way better support for html tags. When reverse engineering the current inline translation of Google Chrome, these results are also very good (and with correct html tags).

I've extracted the following parameters, Google Chrome currently uses:

image

Can anybody check if some parameters are missing in this library?

@thanhansoft
Copy link

Please help me regex replace: class = "row" => class="row"

@RokonuzzamanJhinuk
Copy link

bro you can use this to translate your html database data to any language using this package.
just use like this---- First decode the html code.. then use inside this package translate function.

{!! \Stichoza\GoogleTranslate\TranslateClient::translate( Session::get('s_lang'), Session::get('t_lang'), html_entity_decode(str_limit($n->body,500) ) ) !!}

@milahu
Copy link

milahu commented May 26, 2021

The official paid API has format attribute where you can set that translated text is HTML.

the paid API also allows <div class="notranslate">...</div> to disable translation for specific terms

So if we could find any way* to make it work with HTML strings without damaging tags and avoiding usage of extra "hacky" regexes, it would be really cool.

* - Maybe it can be achieved using some attribute in URL string. The actual translate.google.com doesn't have option to set type to HTML, so I'm not sure we can find an easy solution for this

yes, it was not easy ^^ but i got it working in my translate.js script
also see my post in Prohibit the translation of pieces of text in Google Translate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants