Skip to content

Commit 580a1f8

Browse files
committed
Use uv to auto-detect encoding
1 parent 051ac1d commit 580a1f8

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ a browser.
3333
options: {
3434
biDiSupport: false, // enables bi-directional text support
3535
defaultLocale: 'en', // used if resolution from filename fails
36-
encoding: 'latin1', // .properties file encoding
36+
encoding: 'auto', // .properties file encoding, use one of
37+
// 'auto', 'latin1', or 'utf8'
3738
keyPath: false, // if true, dots '.' key names will result
3839
// in multi-level objects -- use a string
3940
// value to customize

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { parse } = require('dot-properties')
22
const loaderUtils = require('loader-utils')
33
const MessageFormat = require('messageformat')
44
const path = require('path')
5+
const uv = require('uv')
56

67
// expected to follow the pattern baseName_language[_script]_country_variant.properties
78
// source: https://docs.oracle.com/javase/9/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-java.util.Locale-java.lang.ClassLoader-
@@ -16,9 +17,13 @@ function localeFromResourcePath(resourcePath, pathSep, defaultLocale) {
1617
}
1718

1819
module.exports = function(content) {
19-
const { biDiSupport, defaultLocale, encoding, keyPath, pathSep } =
20+
let { biDiSupport, defaultLocale, encoding, keyPath, pathSep } =
2021
loaderUtils.getOptions(this) || {}
21-
const input = content.toString(encoding || 'latin1')
22+
23+
if (!encoding || encoding === 'auto')
24+
encoding = uv(content) ? 'utf8' : 'latin1'
25+
const input = content.toString(encoding)
26+
2227
const translations = parse(input, keyPath || false)
2328
const locale = localeFromResourcePath(
2429
this.resourcePath,

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"dependencies": {
2323
"dot-properties": "^0.4.0",
24-
"loader-utils": "^2.0.0"
24+
"loader-utils": "^2.0.0",
25+
"uv": "^1.1.0"
2526
},
2627
"peerDependencies": {
2728
"messageformat": "1.x | 2.x"

0 commit comments

Comments
 (0)