Beispiel #1
0
    def test_upper_camel_case(self):
        # One token
        result_tokens = ["Doe"]
        self.assertEqual(split_cased_tokens("Doe"),
                         (CaseType.UPPER_CAMEL, result_tokens))

        # Multiple Tokens
        result_tokens.append("Deer")
        self.assertEqual(split_cased_tokens("DoeDeer"),
                         (CaseType.UPPER_CAMEL, result_tokens))

        # Ending on a capital letter
        result_tokens.append("A")
        self.assertEqual(split_cased_tokens("DoeDeerA"),
                         (CaseType.UPPER_CAMEL, result_tokens))

        # Two capital letters in a row
        result_tokens.append("Female")
        self.assertEqual(split_cased_tokens("DoeDeerAFemale"),
                         (CaseType.UPPER_CAMEL, result_tokens))
Beispiel #2
0
def translate_identifier(identifier, translation_client, target_language):
    known_translations = translation_utils.get_known_translations(
        target_language)
    if identifier in known_translations:
        print('known..')
        return known_translations[identifier]

    case_type, tokens = translation_utils.split_cased_tokens(identifier)
    is_multitoken = len(tokens) > 1

    print(case_type, tokens)
    phrase = " ".join(tokens).lower()
    print(phrase)
    translated_identifier_tokens = translation_client.translate(
        phrase,
        target_language=target_language,
        source_language="en",
        format_="text")["translatedText"]
    if case_type == translation_utils.CaseType.UPPER_SNAKE:
        translated_identifier = "_".join(
            map(str.upper, translated_identifier_tokens.split(" ")))
    elif case_type == translation_utils.CaseType.LOWER_SNAKE:
        translated_identifier = "_".join(
            map(str.lower, translated_identifier_tokens.split(" ")))
    elif case_type == translation_utils.CaseType.UPPER_CAMEL:
        translated_identifier = "".join(
            map(str.title, translated_identifier_tokens.split(" ")))
    elif case_type == translation_utils.CaseType.LOWER_CAMEL:
        upper_camel = "".join(
            map(str.title, translated_identifier_tokens.split(" ")))
        translated_identifier = upper_camel[0].lower() + upper_camel[1:]
    else:
        print("Error determining identifier {}'s case type".format(identifier))
        # TODO make this error case more robust

    # Only store translations for tokens that vanilla text translation will not be able to handle
    if identifier != translated_identifier and is_multitoken:
        translation_utils.add_known_translation(target_language, identifier,
                                                translated_identifier)

    return translated_identifier
Beispiel #3
0
def translate_identifier(identifier, target_language):
    api_translations = translation_utils.get_api_translations(target_language)
    if identifier in api_translations:
        return api_translations[identifier]
    # if identifier in known_translations:
    #     return known_translations[identifier]

    # dont translate 1 char ids like "i"
    if len(identifier) <= 1: return identifier

    case_type, tokens = translation_utils.split_cased_tokens(identifier)
    is_multitoken = len(tokens) > 1

    phrase = " ".join(tokens).lower()

    translated_identifier_tokens = translateWithForced(phrase, target_language)
    split_translation = translated_identifier_tokens.split(" ")

    split_sanitized = sanitize_translation_tokens(split_translation)

    if case_type == translation_utils.CaseType.UPPER_SNAKE:
        translated_identifier = "_".join(map(str.upper, split_sanitized))
    elif case_type == translation_utils.CaseType.LOWER_SNAKE:
        translated_identifier = "_".join(map(str.lower, split_sanitized))
    elif case_type == translation_utils.CaseType.UPPER_CAMEL:
        translated_identifier = "".join(map(str.title, split_sanitized))
    elif case_type == translation_utils.CaseType.LOWER_CAMEL:
        upper_camel = "".join(map(str.title, split_sanitized))
        translated_identifier = upper_camel[0].lower() + upper_camel[1:]
    else:
        print("Error determining identifier {}'s case type".format(identifier))
        # TODO make this error case more robust

    # Only store translations for tokens that vanilla text translation will not be able to handle
    if identifier != translated_identifier and is_multitoken:
        translation_utils.add_known_translation(target_language, identifier,
                                                translated_identifier)

    return translated_identifier
Beispiel #4
0
 def test_upper(self):
     self.assertEqual(split_cased_tokens("FOO"),
                      (CaseType.UPPER_SNAKE, ["FOO"]))
     self.assertEqual(split_cased_tokens("FOO_BAR"),
                      (CaseType.UPPER_SNAKE, ["FOO", "BAR"]))
Beispiel #5
0
 def test_lower_camel_case(self):
     result_tokens = ["foo", "Bar"]
     self.assertEqual(split_cased_tokens("fooBar"),
                      (CaseType.LOWER_CAMEL, result_tokens))
Beispiel #6
0
 def test_lower(self):
     self.assertEqual(split_cased_tokens("bar"),
                      (CaseType.LOWER_SNAKE, ["bar"]))
     self.assertEqual(split_cased_tokens("foo_bar"),
                      (CaseType.LOWER_SNAKE, ["foo", "bar"]))