We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
po2ts fails with ascii encode error on py2 (should use utf-8)
Test file:
[octave.zip](https://github.com/translate/translate/files/870288/octave.zip)

```
$ po2ts octave.po oct.ts
processing 1 files...
po2ts: WARNING: Error processing: input octave.po, output oct.ts, template None: 'ascii' codec can't encode characters in position 187-188: ordinal not in range(128)
[###########################################] 100%

$ python --version
Python 2.7.12
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `translate/convert/po2ts.py`
Content:
```
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2004-2006 Zuza Software Foundation
5 #
6 # This file is part of translate.
7 #
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, see <http://www.gnu.org/licenses/>.
20 
21 """Convert Gettext PO localization files to Qt Linguist (.ts) files.
22 
23 See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/ts2po.html
24 for examples and usage instructions.
25 """
26 
27 from translate.storage import po, ts
28 
29 
30 class po2ts(object):
31 
32     def convertstore(self, inputstore, templatefile=None, context=None):
33         """converts a .po file to .ts format (using a template .ts file if given)"""
34         if templatefile is None:
35             tsfile = ts.QtTsParser()
36         else:
37             tsfile = ts.QtTsParser(templatefile)
38         for inputunit in inputstore.units:
39             if inputunit.isheader() or inputunit.isblank():
40                 continue
41             source = inputunit.source
42             translation = inputunit.target
43             comment = inputunit.getnotes("translator")
44             transtype = None
45             if not inputunit.istranslated():
46                 transtype = "unfinished"
47             elif inputunit.getnotes("developer") == "(obsolete)":
48                 transtype = "obsolete"
49             if isinstance(source, bytes):
50                 source = source.decode("utf-8")
51             if isinstance(translation, bytes):
52                 translation = translation.decode("utf-8")
53             for sourcelocation in inputunit.getlocations():
54                 if context is None:
55                     if "#" in sourcelocation:
56                         contextname = sourcelocation[:sourcelocation.find("#")]
57                     else:
58                         contextname = sourcelocation
59                 else:
60                     contextname = context
61                 tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True)
62         return tsfile.getxml()
63 
64 
65 def convertpo(inputfile, outputfile, templatefile, context):
66     """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
67     inputstore = po.pofile(inputfile)
68     if inputstore.isempty():
69         return 0
70     convertor = po2ts()
71     outputstring = convertor.convertstore(inputstore, templatefile, context)
72     outputfile.write(outputstring)
73     return 1
74 
75 
76 def main(argv=None):
77     from translate.convert import convert
78     formats = {"po": ("ts", convertpo), ("po", "ts"): ("ts", convertpo)}
79     parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__)
80     parser.add_option("-c", "--context", dest="context", default=None,
81                       help="use supplied context instead of the one in the .po file comment")
82     parser.passthrough.append("context")
83     parser.run(argv)
84 
85 
86 if __name__ == '__main__':
87     main()
88
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/translate/convert/po2ts.py b/translate/convert/po2ts.py
--- a/translate/convert/po2ts.py
+++ b/translate/convert/po2ts.py
@@ -69,7 +69,7 @@
         return 0
     convertor = po2ts()
     outputstring = convertor.convertstore(inputstore, templatefile, context)
-    outputfile.write(outputstring)
+    outputfile.write(outputstring.encode('utf-8'))
     return 1

{"golden_diff": "diff --git a/translate/convert/po2ts.py b/translate/convert/po2ts.py\n--- a/translate/convert/po2ts.py\n+++ b/translate/convert/po2ts.py\n@@ -69,7 +69,7 @@\n         return 0\n     convertor = po2ts()\n     outputstring = convertor.convertstore(inputstore, templatefile, context)\n-    outputfile.write(outputstring)\n+    outputfile.write(outputstring.encode('utf-8'))\n     return 1\n", "issue": "po2ts fails with ascii encode error on py2 (should use utf-8)\nTest file:\r\n[octave.zip](https://github.com/translate/translate/files/870288/octave.zip)\r\n\r\n```\r\n$ po2ts octave.po oct.ts\r\nprocessing 1 files...\r\npo2ts: WARNING: Error processing: input octave.po, output oct.ts, template None: 'ascii' codec can't encode characters in position 187-188: ordinal not in range(128)\r\n[###########################################] 100%\r\n\r\n$ python --version\r\nPython 2.7.12\r\n```\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright 2004-2006 Zuza Software Foundation\n#\n# This file is part of translate.\n#\n# translate is free software; you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 2 of the License, or\n# (at your option) any later version.\n#\n# translate is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, see <http://www.gnu.org/licenses/>.\n\n\"\"\"Convert Gettext PO localization files to Qt Linguist (.ts) files.\n\nSee: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/ts2po.html\nfor examples and usage instructions.\n\"\"\"\n\nfrom translate.storage import po, ts\n\n\nclass po2ts(object):\n\n    def convertstore(self, inputstore, templatefile=None, context=None):\n        \"\"\"converts a .po file to .ts format (using a template .ts file if given)\"\"\"\n        if templatefile is None:\n            tsfile = ts.QtTsParser()\n        else:\n            tsfile = ts.QtTsParser(templatefile)\n        for inputunit in inputstore.units:\n            if inputunit.isheader() or inputunit.isblank():\n                continue\n            source = inputunit.source\n            translation = inputunit.target\n            comment = inputunit.getnotes(\"translator\")\n            transtype = None\n            if not inputunit.istranslated():\n                transtype = \"unfinished\"\n            elif inputunit.getnotes(\"developer\") == \"(obsolete)\":\n                transtype = \"obsolete\"\n            if isinstance(source, bytes):\n                source = source.decode(\"utf-8\")\n            if isinstance(translation, bytes):\n                translation = translation.decode(\"utf-8\")\n            for sourcelocation in inputunit.getlocations():\n                if context is None:\n                    if \"#\" in sourcelocation:\n                        contextname = sourcelocation[:sourcelocation.find(\"#\")]\n                    else:\n                        contextname = sourcelocation\n                else:\n                    contextname = context\n                tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True)\n        return tsfile.getxml()\n\n\ndef convertpo(inputfile, outputfile, templatefile, context):\n    \"\"\"reads in stdin using fromfileclass, converts using convertorclass, writes to stdout\"\"\"\n    inputstore = po.pofile(inputfile)\n    if inputstore.isempty():\n        return 0\n    convertor = po2ts()\n    outputstring = convertor.convertstore(inputstore, templatefile, context)\n    outputfile.write(outputstring)\n    return 1\n\n\ndef main(argv=None):\n    from translate.convert import convert\n    formats = {\"po\": (\"ts\", convertpo), (\"po\", \"ts\"): (\"ts\", convertpo)}\n    parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__)\n    parser.add_option(\"-c\", \"--context\", dest=\"context\", default=None,\n                      help=\"use supplied context instead of the one in the .po file comment\")\n    parser.passthrough.append(\"context\")\n    parser.run(argv)\n\n\nif __name__ == '__main__':\n    main()\n", "path": "translate/convert/po2ts.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright 2004-2006 Zuza Software Foundation\n#\n# This file is part of translate.\n#\n# translate is free software; you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 2 of the License, or\n# (at your option) any later version.\n#\n# translate is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, see <http://www.gnu.org/licenses/>.\n\n\"\"\"Convert Gettext PO localization files to Qt Linguist (.ts) files.\n\nSee: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/ts2po.html\nfor examples and usage instructions.\n\"\"\"\n\nfrom translate.storage import po, ts\n\n\nclass po2ts(object):\n\n    def convertstore(self, inputstore, templatefile=None, context=None):\n        \"\"\"converts a .po file to .ts format (using a template .ts file if given)\"\"\"\n        if templatefile is None:\n            tsfile = ts.QtTsParser()\n        else:\n            tsfile = ts.QtTsParser(templatefile)\n        for inputunit in inputstore.units:\n            if inputunit.isheader() or inputunit.isblank():\n                continue\n            source = inputunit.source\n            translation = inputunit.target\n            comment = inputunit.getnotes(\"translator\")\n            transtype = None\n            if not inputunit.istranslated():\n                transtype = \"unfinished\"\n            elif inputunit.getnotes(\"developer\") == \"(obsolete)\":\n                transtype = \"obsolete\"\n            if isinstance(source, bytes):\n                source = source.decode(\"utf-8\")\n            if isinstance(translation, bytes):\n                translation = translation.decode(\"utf-8\")\n            for sourcelocation in inputunit.getlocations():\n                if context is None:\n                    if \"#\" in sourcelocation:\n                        contextname = sourcelocation[:sourcelocation.find(\"#\")]\n                    else:\n                        contextname = sourcelocation\n                else:\n                    contextname = context\n                tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True)\n        return tsfile.getxml()\n\n\ndef convertpo(inputfile, outputfile, templatefile, context):\n    \"\"\"reads in stdin using fromfileclass, converts using convertorclass, writes to stdout\"\"\"\n    inputstore = po.pofile(inputfile)\n    if inputstore.isempty():\n        return 0\n    convertor = po2ts()\n    outputstring = convertor.convertstore(inputstore, templatefile, context)\n    outputfile.write(outputstring.encode('utf-8'))\n    return 1\n\n\ndef main(argv=None):\n    from translate.convert import convert\n    formats = {\"po\": (\"ts\", convertpo), (\"po\", \"ts\"): (\"ts\", convertpo)}\n    parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__)\n    parser.add_option(\"-c\", \"--context\", dest=\"context\", default=None,\n                      help=\"use supplied context instead of the one in the .po file comment\")\n    parser.passthrough.append(\"context\")\n    parser.run(argv)\n\n\nif __name__ == '__main__':\n    main()\n", "path": "translate/convert/po2ts.py"}]}