简体   繁体   English

将字符串从Java .properties文件安全地传递到JavaScript处理的字符串

[英]Safely pass strings from Java .properties file to a string processed by JavaScript

I've run into a problem with internationalization and need to know the best way to move forward. 我遇到了国际化的问题,需要了解前进的最佳方法。 we use java ResourceBundle with .properties files to internationalize the content on our software. 我们使用带有.properties文件的Java ResourceBundle来国际化我们软件上的内容。 There are areas where a dynamic value gets passed to a JavaScript alert, for example 例如,有些区域会将动态值传递给JavaScript警报

#.properties file with ca_ES locale
warningText=regles d’apostrofació

#JavaScript with the property as parameter
<script>
    alert('warningText');
</script>

So the apostrophe in the value for warningText escapes the string and causes a fatal error. 因此,warningText的值中的撇号使字符串转义并导致致命错误。 We can't really use double quotes instead because of our coding standards, and it would be a ton of work to go through and change all of the code to double quotes, and we can't use something like &quot; 由于我们的编码标准,我们不能真正使用双引号来代替,要完成所有代码并将其更改为双引号需要大量的工作,而我们不能使用类似&quot; because html tags don't resolve inside a javascript alert. 因为html标记无法在javascript警报内解析。

Lastly if within the string in the properties file we did something like this: 最后,如果在属性文件的字符串中,我们执行以下操作:

warningText=regles d\’apostrofació

the escape is applied during compilation or in the JVM or somewhere prior to the javascript and we end up just escaping the string anyways 转义是在编译期间或在JVM或javascript之前的某个位置应用的,无论如何我们最终只是转义了字符串

You may simply use unicodes \\uxxxx instead of unicode characters into properties file which are also supported by browsers, below are the unicodes for regles d'apostrofació : 您可以简单地在属性文件中使用unicodes \\uxxxx而不是unicode字符,浏览器也支持unicode字符,以下是reg'd'apostrofació的regles d'apostrofació

#warningText=regles d’apostrofació
warningText=\u0072\u0065\u0067\u006c\u0065\u0073\u0020\u0064\u2019\u0061\u0070\u006f\u0073\u0074\u0072\u006f\u0066\u0061\u0063\u0069\u00f3

 alert('\r\e\g\l\e\s\ \d\’\a\p\o\s\t\r\o\f\a\c\i\ó') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM