Пример #1
0
    def test_translate_write_proj_file(self, mkdir_func):
        test_file = teststringio.TestStringIO(os.getcwd() +
                                              "/Resources/test/Sample.resx")
        csproj_file = teststringio.TestStringIO(
            os.getcwd() + "/Resources/test/Proj.csproj", RESXTests.test_csproj)
        files = {
            "Resources/test/Sample.it-IT.resx": test_file,
            "Resources/test/Proj.csproj": csproj_file
        }

        resx_parser = parser.RESX()
        vcs_class = mock.Mock()
        resx_parser._read_file = mock.Mock(return_value=RESXTests.sample_resx)

        resx_parser._open_file_for_writing = mock.Mock()

        def side_effect(arg):
            return files[arg]

        resx_parser._open_file_for_writing.side_effect = side_effect

        resx_parser._open_file_for_appending = mock.Mock(
            return_value=csproj_file)

        output_filename = resx_parser.translate(
            "Sample.resx", "Resources/test", {
                u"Translation for some string":
                u"Traduzione di Bablefish per questa stringa",
                u"Translation for the other string":
                u"Translation for the other string",
                u"Will not show up": u"Will not show up",
                u"A ToolTip String": u"Translated ToolTip String",
            }, "Italian", "it-IT", True, vcs_class,
            "Resources/test/Proj.csproj")
        self.assertEquals(csproj_file.getvalue(), RESXTests.expected_csproj)
Пример #2
0
    def test_translate(self):
        vcs_class = mock.Mock()
        translator = parser.Angular()
        output_file = teststringio.TestStringIO()

        translator._open_file_for_reading = mock.Mock(return_value=(
            teststringio.TestStringIO(AngularTests.sample_strings), "utf_8"))

        translator._open_file_for_writing = mock.Mock(return_value=output_file)

        mapping = {
            u"Translation for the other string":
            u"Can't \"quote\" \xe9\u4e00\xe9!"
        }

        translator.translate('english.i18n.ts', '.', mapping, 'Spanish', 'es',
                             True, vcs_class)

        self.assertEquals(output_file.getvalue(),
                          AngularTests.translated_strings)

        translator._open_file_for_writing.assert_called_with(
            os.path.join(".", "spanish.i18n.ts"))

        vcs_class.add_file.assert_called_with(
            os.path.join(".", "spanish.i18n.ts"))
Пример #3
0
    def test_translate(self, mkdir_func):
        file = cStringIO.StringIO()
        translator = parser.StringsDict()
        test_file = teststringio.TestStringIO()
        vcs_class = mock.Mock()

        translator._open_file_for_writing = mock.Mock(return_value=test_file)
        translator._read_file = mock.Mock(
            return_value=StringsDictTests.sample_strings)

        self.assertEquals(
            translator.translate(
                "test.stringsdict", "Resources", {
                    u'Activate %lu Fonts': u'Activate Plural',
                    u'Activate %lu Font': u'Activate Singular',
                    u'Deactivate %lu Fonts': u'Deactivate Plural',
                    u'Deactivate %lu Font': u'Deactivate Singular'
                }, "French", "fr", True, vcs_class),
            os.path.join("Resources", "test.stringsdict"))

        self.assertEquals(test_file.getvalue(),
                          StringsDictTests.translated_strings)

        mkdir_func.assert_called_with("Resources")

        translator._open_file_for_writing.assert_called_with(
            os.path.join("Resources", "test.stringsdict"))

        vcs_class.add_file.assert_called_with(
            os.path.join("Resources", "test.stringsdict"))

        file.close()
Пример #4
0
    def test_translate(self, mkdir_func, listdir_func):
        listdir_func.return_value = [
            "Localizable.strings", "Localizable.stringsdict"
        ]
        lproj_parser = parser.LPROJ()
        test_file = teststringio.TestStringIO()
        fake_strings_parser = parser.Strings()
        fake_stringsdict_parser = parser.StringsDict()
        vcs_class = mock.Mock()

        lproj_parser._open_file = mock.Mock(return_value=test_file)
        lproj_parser._create_strings_parser = mock.Mock(
            return_value=fake_strings_parser)

        lproj_parser._create_stringsdict_parser = mock.Mock(
            return_value=fake_stringsdict_parser)
        fake_stringsdict_parser.translate = mock.Mock()

        string_mapping = stringmapping.StringMapping()
        string_mapping.add_mapping('"SomeString"',
                                   u"Translation for some string")
        string_mapping.add_mapping('"NewString"', u"Untranslated string")
        string_mapping.add_mapping('InfoPlistVar', u"Untranslated string")
        fake_strings_parser.extract_mapping_from_filename = mock.Mock(
            return_value=string_mapping)

        translation_dict = {
            u"Translation for some string":
            u"Traduzione di Bablefish per questa stringa",
            u"Extra string": u"Not in file to localize",
        }

        output_filenames = lproj_parser.translate("en.lproj", "Resources",
                                                  translation_dict, "Italian",
                                                  "it", True, vcs_class)

        mkdir_func.assert_called_with(os.path.join("Resources", "it.lproj"))
        lproj_parser._open_file.assert_called_with(
            os.path.join("Resources", "it.lproj", "Localizable.strings"))

        self.assertEquals(output_filenames,
                          os.path.join("Resources", "it.lproj"))

        self.assertEquals(
            test_file.getvalue(), """"NewString" = "Untranslated string";
"SomeString" = "Traduzione di Bablefish per questa stringa";
InfoPlistVar = "Untranslated string";\n""")

        fake_stringsdict_parser.translate.assert_called_with(
            os.path.join("en.lproj", "Localizable.stringsdict"),
            os.path.join("Resources", "it.lproj"), translation_dict, "Italian",
            "it", True, vcs_class)

        vcs_class.add_file.assert_called_with(
            os.path.join("Resources", "it.lproj", "Localizable.strings"))
Пример #5
0
    def test_extract_strings_from_filename(self):
        extractor = parser.Properties()
        extractor._open_file_for_reading = mock.Mock(return_value=(
            teststringio.TestStringIO(None, PropertiesTests.sample_file)))

        self.assertEquals(
            extractor.extract_strings_from_filename("some_file"),
            set([
                u"SomeString", u"SomeOtherString", u"YetAnotherString",
                u"CouldNotOpenFont"
            ]))
Пример #6
0
    def test_write_mapping(self):
        file = teststringio.TestStringIO()
        parser.Properties().write_mapping(
            file, {
                u"SomeString": u"Translation for some string",
            })

        self.assertEquals(file.getvalue(),
                          """SomeString Translation for some string
""")

        file.close()
Пример #7
0
    def test_translate(self, mkdir_func):
        rc_parser = parser.RC()
        vcs_class = mock.Mock()
        test_output_file = teststringio.TestStringIO()
        test_input_file = None

        def _get_input_file(self):
            test_input_file = teststringio.TestStringIO(RCTests.sample_rc)
            return test_input_file, "utf-8"

        rc_parser._open_file_for_writing = mock.Mock(
            return_value = test_output_file
        )
        rc_parser._open_file = mock.Mock(side_effect = _get_input_file)

        output_filename = rc_parser.translate(
            "Sample.rc",
            "Resources",
            {
                u"Translation for \"some\" string" :
                    u"Traduzione di Bablefish per \"questa\" stringa",
                u"Translation\\nfor the other string" :
                    u"Translation\\nfor the other string",
                u"Will not show up" : u"Will not show up",
            },
            "Italian",
            "it-IT",
            True,
            vcs_class
        )

        mkdir_func.assert_called_with(
            "Resources"
        )

        rc_parser._open_file_for_writing.assert_called_with(
            os.path.join("Resources", "Sample.it-IT.rc")
        )

        self.assertEquals(
            output_filename,
            os.path.join("Resources", "Sample.it-IT.rc")
        )

        self.assertEquals(
            test_output_file.getvalue(),
            RCTests.sample_translated_rc
        )

        vcs_class.add_file.assert_called_with(
            os.path.join("Resources", "Sample.it-IT.rc")
        )
Пример #8
0
    def test_write_mapping_does_not_over_escape_newline(self):
        file = teststringio.TestStringIO()
        parser.Properties().write_mapping(
            file, {
                u"SomeString": u"String with a \\r\\n newline",
            })

        # This test is a little deceptive because we have to escape the python
        # string. The real string has only one backslash for each escaped
        # character. It is "String with a \r\n newline".
        self.assertEquals(file.getvalue(),
                          """SomeString String with a \\r\\n newline
""")
Пример #9
0
    def test_translate(self, mkdir_func):
        test_parser = parser.Properties()
        test_file   = teststringio.TestStringIO()
        vcs_class   = mock.Mock()

        test_parser._open_file_for_writing = mock.Mock(return_value = test_file)

        string_mapping = stringmapping.StringMapping()
        string_mapping.add_mapping("SomeString", u"Translation for some string")
        string_mapping.add_mapping("NewString",  u"Untranslated string")
        test_parser.extract_string_mapping_from_files = mock.Mock(
            return_value = string_mapping
        )

        output_filename = test_parser.translate(
            "strings.properties",
            "locale",
            {
                u"Translation for some string" :
                    u"Traduzione di Bablefish per questa stringa",
                u"Extra string" : u"Not in file to localize",
            },
            "Italian",
            "it_IT",
            True,
            vcs_class
        )

        mkdir_func.assert_called_with(
            os.path.join("locale", "it_IT")
        )
        test_parser._open_file_for_writing.assert_called_with(
            os.path.join("locale", "it_IT", "strings.properties")
        )

        self.assertEquals(
            output_filename,
            os.path.join("locale", "it_IT", "strings.properties")
        )

        self.assertEquals(
            test_file.getvalue(),
            """SomeString Traduzione di Bablefish per questa stringa
NewString Untranslated string\n"""
        )

        vcs_class.add_file.assert_called_with(
            os.path.join("locale", "it_IT", "strings.properties")
        )
Пример #10
0
    def test_translate(self, mkdir_func):
        resx_parser = parser.RESX()
        vcs_class = mock.Mock()
        resx_parser._read_file = mock.Mock(return_value=RESXTests.sample_resx)
        test_file = teststringio.TestStringIO('test.it.resx')

        resx_parser._open_file_for_writing = mock.Mock(return_value=test_file)

        self.assertEquals(
            resx_parser.translate(
                "test.it-IT.resx", "Resources", {
                    u"Translation for \"some\" string":
                    u"Traduzione di Bablefish per \"questa\" stringa",
                    u"Translation\\nfor the other string":
                    u"Translation\\nfor the other string",
                    u"Will not show up": u"Will not show up",
                    u"A ToolTip String": u"Translated ToolTip String",
                }, "Italian", "it-IT", True, vcs_class, None),
            "test.it-IT.resx")

        self.assertFalse(mkdir_func.called)
        self.assertFalse(resx_parser._open_file_for_writing.called)
        self.assertFalse(vcs_class.add_file.called)

        output_filename = resx_parser.translate(
            "Sample.resx", "Resources", {
                u"Translation for some string":
                u"Traduzione di Bablefish per questa stringa",
                u"Translation for the other string":
                u"Translation for the other string",
                u"Will not show up": u"Will not show up",
                u"A ToolTip String": u"Translated ToolTip String",
            }, "Italian", "it-IT", True, vcs_class, None)

        mkdir_func.assert_called_with("Resources")
        resx_parser._open_file_for_writing.assert_called_with(
            os.path.join("Resources", "Sample.it-IT.resx"))

        self.assertEquals(output_filename,
                          os.path.join("Resources", "Sample.it-IT.resx"))

        self.assertEquals(test_file.getvalue(),
                          RESXTests.sample_translated_resx)

        vcs_class.add_file.assert_called_with(
            os.path.join("Resources", "Sample.it-IT.resx"))
Пример #11
0
    def test_extract_mapping_from_filename(self):
        extractor = parser.Properties()
        extractor._open_file_for_reading = mock.Mock(
            return_value=teststringio.TestStringIO(
                None, PropertiesTests.sample_file), )

        string_mapping = extractor.extract_mapping_from_filename("some_file")

        self.assertEquals(
            string_mapping.string_mapping_dict, {
                u"SomeString": u"Translation for some string",
                u"SomeOtherString": u"Translation for the other string",
                u"YetAnotherString": u"\\ Yet another string",
                u"CouldNotOpenFont": u"Could not open font \"{0}\"."
            })

        for key, value in string_mapping.string_mapping_dict.iteritems():
            self.assertEquals(type(key), types.UnicodeType)
            self.assertEquals(type(value), types.UnicodeType)
Пример #12
0
    def test_extract_mapping_from_filename(self):
        extractor = parser.RC()
        extractor._open_file = mock.Mock(return_value=(
            teststringio.TestStringIO(RCTests.sample_rc),
            "utf_8",
        ))

        string_mapping = extractor.extract_mapping_from_filename("some_file")

        self.assertEquals(
            string_mapping.string_mapping_dict, {
                u"SomeString": u"Translation for \"some\" string",
                u"SomeOtherString": u"Translation\\nfor the other string",
                u"YetAnotherString": u"YetAnotherString",
                u"Activating fonts": u"Activating fonts",
            })

        for key, value in string_mapping.string_mapping_dict.iteritems():
            self.assertEquals(type(key), types.UnicodeType)
            self.assertEquals(type(value), types.UnicodeType)
Пример #13
0
    def test_extract_strings_from_filename(self):
        extractor = parser.RC()
        extractor._open_file = mock.Mock(return_value=(
            teststringio.TestStringIO(RCTests.sample_rc),
            "iso-8859-1",
        ))

        extracted_strings = extractor.extract_strings_from_filename(
            "some_file")

        self.assertEquals(
            extracted_strings,
            set([
                u"SomeString",
                u"SomeOtherString",
                u"YetAnotherString",
                u"Activating fonts",
            ]))

        for string in extracted_strings:
            self.assertEquals(type(string), types.UnicodeType)
Пример #14
0
    def test_translate(self, mkdir_func):
        xml_parser = parser.PasteboardXML()
        vcs_class = mock.Mock()
        xml_parser._read_file = mock.Mock(
            return_value=PasteboardXMLTests.sample_xml)
        test_file = teststringio.TestStringIO()

        xml_parser._open_file_for_writing = mock.Mock(return_value=test_file)

        self.assertEquals(
            xml_parser.translate(
                "pasteboard-test-en.xml", "Resources", {
                    u"English Category": u"Translated Category",
                    u"English Title": u"Translated Title",
                    u"English Test Element 1": u"Translated Test Element 1",
                    u"English Test Element 2": u"Translated Test Element 2",
                }, "Japanese", "jp", True, vcs_class),
            os.path.join("Resources", "pasteboard-test-jp.xml"))

        mkdir_func.assert_called_with("Resources")

        self.assertEquals(test_file.getvalue(),
                          PasteboardXMLTests.sample_translated_xml)
Пример #15
0
 def _get_input_file(self):
     test_input_file = teststringio.TestStringIO(RCTests.sample_rc)
     return test_input_file, "utf-8"