{"id":1690,"date":"2020-12-26T00:34:37","date_gmt":"2020-12-26T00:34:37","guid":{"rendered":"https:\/\/ofalcao.pt\/blog\/?p=1690"},"modified":"2020-12-26T00:38:17","modified_gmt":"2020-12-26T00:38:17","slug":"ev3dev-and-portuguese","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2020\/ev3dev-and-portuguese","title":{"rendered":"ev3dev and portuguese"},"content":{"rendered":"\n<p>Using ev3dev and python to speak a few words in english in english is very easy:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom ev3dev2.sound import Sound\nsound = Sound()\nsound.speak('Good morning')\n<\/pre><\/div>\n\n\n<p>Making it speak portuguese words is also easy:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom ev3dev2.sound import Sound\nsound = Sound()\nsound.speak('Bom dia', espeak_opts='-v pt')\n<\/pre><\/div>\n\n\n<p>not a great pronounce but still understandable.<\/p>\n\n\n\n<p>Problems started when trying to speak sentences written with portuguese characters like &#8220;Ol\u00e1&#8221; (&#8220;Hello&#8221;)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsound.speak('Ol\u00e1', espeak_opts='-v pt')\n<\/pre><\/div>\n\n\n<p>it generates an error similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">UnicodeEncodeError: 'ascii' codec can't encode character u'\\xa0' in position 20: ordinal not in range(128)<\/pre>\n\n\n\n<p>my ev3dev installation wasn&#8217;t configured to understand non-ASCII characters so a quick setting at the command line corrected this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ export LC_ALL=\"en_US.UTF-8\"\n$ export LC_CTYPE=\"en_US.UTF-8\"<\/pre>\n\n\n\n<p>Now I wanted to speak the current date, like &#8216;Today is Friday 25 December&#8221; so I found &#8216;datetime&#8217;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import datetime as dt\nimport datetime\n\ntoday = dt.today()\nspeech = today.strftime(&quot;%A&quot;) + ' ' + today.strftime(&quot;%-d&quot;) + ' ' + today.strftime(&quot;%B&quot;)\nsound.speak('Today is ' + speech)\n<\/pre><\/div>\n\n\n<p>Great. At least in english. But how to do it in portuguese (i.e. &#8216;Hoje \u00e9 Sexta 25 Dezembro&#8221;)? How to make today.strftime(&#8220;%A) return &#8220;Sexta&#8221; instead of &#8220;Friday&#8221;?<br>The trick is using &#8216;locale&#8217;:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import datetime as dt\nimport datetime\nimport locale\n\nlocale.setlocale(locale.LC_TIME, &quot;pt_PT&quot;) \ntoday = dt.today()\nspeech = today.strftime(&quot;%A&quot;) + ' ' + today.strftime(&quot;%-d&quot;) + ' ' + today.strftime(&quot;%B&quot;)\nsound.speak('Today is ' + speech)\n<\/pre><\/div>\n\n\n<p>And now another error:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">locale.Error: unsupported locale setting<\/pre>\n\n\n\n<p>ev3dev doesn&#8217;t have Portuguse locale settings (we can check with &#8216;<code>locale -a<\/code>&#8216;). So I needed to install them:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo dpkg-reconfigure locales<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"307\" class=\"wp-image-1694\" style=\"width: 400px;\" src=\"https:\/\/i0.wp.com\/ofalcao.pt\/blog\/wp-content\/uploads\/2020\/12\/pt-locales.jpg?resize=400%2C307&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/ofalcao.pt\/blog\/wp-content\/uploads\/2020\/12\/pt-locales.jpg?w=647&amp;ssl=1 647w, https:\/\/i0.wp.com\/ofalcao.pt\/blog\/wp-content\/uploads\/2020\/12\/pt-locales.jpg?resize=300%2C230&amp;ssl=1 300w\" sizes=\"auto, (max-width: 400px) 85vw, 400px\" \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using ev3dev and python to speak a few words in english in english is very easy: Making it speak portuguese words is also easy: not a great pronounce but still understandable. Problems started when trying to speak sentences written with portuguese characters like &#8220;Ol\u00e1&#8221; (&#8220;Hello&#8221;) it generates an error similar to this: UnicodeEncodeError: &#8216;ascii&#8217; codec &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2020\/ev3dev-and-portuguese\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;ev3dev and portuguese&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"series":[],"class_list":["post-1690","post","type-post","status-publish","format-standard","hentry","category-sem-categoria"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-rg","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1690"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1690\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1690"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}