func TestTextSanitizedForJavaString(t *testing.T) { ass := func(expected string, input string) { assert.Equal(t, expected, java.SanitizedForStringValue(input), input) } ass("", "") ass("Foo", "Foo") // https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html // Escaping curly braces with single quotes // ass(util.XMLEscaped("eka '{}' toka"), "eka {} toka") ass(util.XMLEscaped("eka '{{}}' toka"), "eka {{}} toka") ass(util.XMLEscaped("eka '{' toka"), "eka { toka") ass(util.XMLEscaped("eka '{'0'}' toka"), "eka {0} toka") ass(util.XMLEscaped("eka '{' keski moro '}' toka"), "eka { keski moro } toka") // Escaping single quotes themselves // ass(util.XMLEscaped("eka '' toka"), "eka ' toka") ass(util.XMLEscaped("eka '''{}''' toka"), "eka '{}' toka") ass(util.XMLEscaped("eka '{''}' toka"), "eka {'} toka") ass(util.XMLEscaped("eka '{''''}' toka"), "eka {''} toka") // XML-escaping ass("<Foo>", "<Foo>") }
func GetStringsFileContents(set model.TranslationSet, language string) string { ret := "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n" for _, section := range set.Sections { sectionHeadingPrinted := false for _, translation := range section.Translations { if !translation.IsForPlatform(model.PlatformAndroid) { continue } value := translation.ValueForLanguage(language) if value == nil { continue } if !sectionHeadingPrinted && 0 < len(section.Name) { ret += "\n <!-- ********** " + sanitizedForXMLComment(section.Name) + " ********** -->\n\n" sectionHeadingPrinted = true } if 0 < len(translation.Comment) { ret += fmt.Sprintf(" <!-- %s -->\n", sanitizedForXMLComment(translation.Comment)) } ret += fmt.Sprintf(" <string name=\"%s\">%s</string>\n", util.XMLEscaped(translation.Key), stringFromSegments(value.Segments)) } } ret += "</resources>\n" return ret }
func SanitizedForKey(text string) string { // Visual Studio generates C# code from resx (and sometimes resw) format // resource XML files, which means that the keys must be valid C# // identifiers. Let's automatically fix some common issues related to that. // invalidCharsRegexp, err := regexp.Compile("[. ]") if err != nil { panic(err) } return util.XMLEscaped(invalidCharsRegexp.ReplaceAllString(text, "_")) }
func SanitizedForStringValue(text string) string { // The escaping/quoting rules for Java MessageFormat strings are a // MASSIVE PAIN IN THE ASS. Let’s just do our best. // https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html // if _moustacheRegexp == nil { _moustacheRegexp, _ = regexp.Compile("([{}]+)") } s := "" for index, part := range strings.Split(text, "'") { p := _moustacheRegexp.ReplaceAllString(part, "'$1'") if index == 0 || strings.HasSuffix(s, "'") && strings.HasPrefix(p, "'") { s += p } else { s += "''" + p } } return util.XMLEscaped(s) }
func GetStringsFileContents(set model.TranslationSet, language string) string { ret := "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<!--\n" + ".NET Resource File\n" + "Generated by Sanat\n" + "Language: " + language + "\n" + "-->\n" + "<root>\n" + xmlHeaderString for _, section := range set.Sections { sectionHeadingPrinted := false for _, translation := range section.Translations { if !translation.IsForPlatform(model.PlatformWindows) { continue } value := translation.ValueForLanguage(language) if value == nil { continue } if !sectionHeadingPrinted && 0 < len(section.Name) { sanitizedSectionName := strings.Replace(section.Name, "--", "- -", -1) ret += "\n <!-- ********** " + sanitizedSectionName + " ********** -->\n\n" sectionHeadingPrinted = true } ret += fmt.Sprintf(" <data name=\"%s\" xml:space=\"preserve\">\n", SanitizedForKey(translation.Key)) ret += fmt.Sprintf(" <value>%s</value>\n", stringFromSegments(value.Segments)) if 0 < len(translation.Comment) { ret += fmt.Sprintf(" <comment>%s</comment>\n", util.XMLEscaped(translation.Comment)) } ret += " </data>\n" } } ret += "</root>\n" return ret }
func SanitizedForString(text string) string { return util.XMLEscaped(strings.Replace(text, "%", "%%", -1)) }
func SanitizedForStringValue(text string) string { return util.XMLEscaped(text) }
func SanitizedForKey(text string) string { return util.XMLEscaped(text) }