diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 40a763c8a6..e79cefaa21 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -16,6 +16,11 @@ part of the locale specifier if it has a very common/default value Bug Fixes: * Fixed unit test failures which occur on QT 5.12 +* Fixed problem where two resource bundle files with the same name and same locale but loaded from +different directories were cached in the same place. + * Introduced the new "basePath" property to ResBundle constructor to specify which directory + to load the resource bundle from. This property is used to differentiate files loaded from + different directories. Build 006 ------- diff --git a/js/lib/Loader.js b/js/lib/Loader.js index 01f56c624b..9cdb5598b7 100644 --- a/js/lib/Loader.js +++ b/js/lib/Loader.js @@ -1,7 +1,7 @@ /* * Loader.js - shared loader implementation * - * Copyright © 2015, 2018, JEDLSoft + * Copyright © 2015, 2018-2019, JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -86,8 +86,9 @@ Loader.prototype._loadFileAlongIncludePath = function(includePath, pathname) { return undefined; }; -Loader.prototype.loadFiles = function(paths, sync, params, callback) { - var includePath = params && params.base ? [params.base].concat(this.includePath) : this.includePath; +Loader.prototype.loadFiles = function(paths, sync, params, callback, root) { + root = root || (params && params.base); + var includePath = root ? [root].concat(this.includePath) : this.includePath; //console.log("Loader loadFiles called"); // make sure we know what we can load diff --git a/js/lib/NormString.js b/js/lib/NormString.js index 4e144921e8..3051c6dc59 100644 --- a/js/lib/NormString.js +++ b/js/lib/NormString.js @@ -1,7 +1,7 @@ /* * NormString.js - ilib normalized string subclass definition * - * Copyright © 2013-2015, 2018, JEDLSoft + * Copyright © 2013-2015, 2018-2019, JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -141,7 +141,7 @@ NormString.init = function(options) { if (files.length) { //console.log("loading files " + JSON.stringify(files)); - Utils._callLoadData(files, sync, loadParams, function(arr) { + Utils._callLoadData(files, sync, loadParams, undefined, function(arr) { for (var i = 0; i < arr.length; i++) { if (typeof(arr[i]) !== 'undefined') { ilib.extend(ilib.data.norm[toLoad[i]], arr[i]); diff --git a/js/lib/ResBundle.js b/js/lib/ResBundle.js index d542085088..be4e814f62 100644 --- a/js/lib/ResBundle.js +++ b/js/lib/ResBundle.js @@ -1,7 +1,7 @@ /* * ResBundle.js - Resource bundle definition * - * Copyright © 2012-2016, 2018, JEDLSoft + * Copyright © 2012-2016, 2018-2019, JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -71,6 +71,11 @@ var IString = require("./IString.js"); * The default behaviour is the same as before, which is to return the source string * unchanged. * + *
* * Suggested implementations of this method might load files diff --git a/js/test/root/resources/ja/basetest.json b/js/test/root/resources/ja/basetest.json new file mode 100644 index 0000000000..2049bb2aa7 --- /dev/null +++ b/js/test/root/resources/ja/basetest.json @@ -0,0 +1,6 @@ +{ + "hello" : "こんにちは", + "Hello from {country}": "{country}からこんにちは", + "Hello from {city}": "{city}からこんにちは", + "Greetings from {city} in {country}": "{city}と{country}からこんにちは" +} diff --git a/js/test/root/resources2/ja/basetest.json b/js/test/root/resources2/ja/basetest.json new file mode 100644 index 0000000000..72622f469c --- /dev/null +++ b/js/test/root/resources2/ja/basetest.json @@ -0,0 +1,6 @@ +{ + "hello" : "こんにちは2", + "Hello from {country}": "{country}からこんにちは2", + "Hello from {city}": "{city}からこんにちは2", + "Greetings from {city} in {country}": "{city}と{country}からこんにちは2" +} diff --git a/js/test/root/testresources.js b/js/test/root/testresources.js index be245d6629..6d268097c6 100644 --- a/js/test/root/testresources.js +++ b/js/test/root/testresources.js @@ -1,7 +1,7 @@ /* * testresources.js - test the Resources object * - * Copyright © 2012-2015, 2017-2018, JEDLSoft + * Copyright © 2012-2015, 2017-2019, JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,9 @@ if (typeof(ResBundle) === "undefined") { if (typeof(Locale) === "undefined") { var Locale = require("../../lib/Locale.js"); } +if (ilib._getPlatform() === "nodejs" && ilib._dyndata && ilib._dyncode) { + var path = require("path"); +} ilib.data.strings = { "first string": "first", @@ -2072,6 +2075,58 @@ module.exports.testresources = { "dritte String 2" ]); + test.done(); + }, + + testResBundleGetStringWithBasePath: function(test) { + if (ilib._getPlatform() !== "nodejs" || !ilib._dyndata || !ilib._dyncode) { + test.done(); + return; + } + + test.expect(4); + + // clear this to be sure it is actually loading something + ilib.clearCache(); + + var base = path.relative(process.cwd(), path.resolve(__dirname, "./resources")); + + var rb = new ResBundle({ + locale: "ja-JP", + name: "basetest", + basePath: base + }); + + test.ok(rb !== null); + + test.equal(rb.getString("Hello from {country}").toString(), "{country}からこんにちは"); + test.equal(rb.getString("Hello from {city}").toString(), "{city}からこんにちは"); + test.equal(rb.getString("Greetings from {city} in {country}").toString(), "{city}と{country}からこんにちは"); + test.done(); + }, + + testResBundleGetStringWithDifferentBasePath: function(test) { + if (ilib._getPlatform() !== "nodejs" || !ilib._dyndata || !ilib._dyncode) { + test.done(); + return; + } + + test.expect(4); + + // don't clear the cache + var base = path.relative(process.cwd(), path.resolve(__dirname, "./resources2")); + + var rb = new ResBundle({ + locale: "ja-JP", + name: "basetest", + basePath: base + }); + + test.ok(rb !== null); + + test.equal(rb.getString("Hello from {country}").toString(), "{country}からこんにちは2"); + test.equal(rb.getString("Hello from {city}").toString(), "{city}からこんにちは2"); + test.equal(rb.getString("Greetings from {city} in {country}").toString(), "{city}と{country}からこんにちは2"); test.done(); } }; \ No newline at end of file diff --git a/js/test/root/testresourcesasync.js b/js/test/root/testresourcesasync.js index c370506a01..88026283a2 100644 --- a/js/test/root/testresourcesasync.js +++ b/js/test/root/testresourcesasync.js @@ -1,7 +1,7 @@ /* * testresourcesasync.js - test the Resources object * - * Copyright © 2018, JEDLSoft + * Copyright © 2018-2019, JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -93,7 +93,38 @@ module.exports.testresourcesasync = { test.done(); } }); + }, + + testResBundleAsyncGetStringWithPathesMX: function(test) { + if (ilib._getPlatform() !== "nodejs" || !ilib._dyndata || !ilib._dyncode) { + test.done(); + return; + } + + test.expect(4); + + // clear this to be sure it is actually loading something + ilib.data.strings = undefined; + ilib.data.strings_es = undefined; + ilib.data.strings_und_MX = undefined; + ilib.data.strings_es_MX = undefined; + ilib.clearCache(); + var base = path.relative(process.cwd(), path.resolve(__dirname, "./resources")); + + new ResBundle({ + locale: "es-MX", + sync: false, + basePath: base, + onLoad: function(rb) { + test.ok(rb !== null); + + test.equal(rb.getString("Hello from {country}").toString(), "Que tal de {country}"); + test.equal(rb.getString("Hello from {city}").toString(), "Que tal de {city}"); + test.equal(rb.getString("Greetings from {city} in {country}").toString(), "Hola de {city} en {country}"); + test.done(); + } + }); }, testResBundleAsyncGetStringOtherBundlePsuedoRaw: function(test) { diff --git a/js/test/util/testutils.js b/js/test/util/testutils.js index 8c706f5a77..7693eaf7b7 100644 --- a/js/test/util/testutils.js +++ b/js/test/util/testutils.js @@ -1,7 +1,7 @@ /* * testutils.js - test the utility routines * - * Copyright © 2012-2015, 2017-2018 JEDLSoft + * Copyright © 2012-2015, 2017-2019 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -2031,6 +2031,36 @@ module.exports.testutils = { } }, + testLoadDataDontMixDifferentBasePaths: function(test) { + ilib.data.foo = ilib.data.foo_de = ilib.data.foo_und_DE = ilib.data.foo_de_DE = undefined; + ilib.setLoaderCallback(mockLoaderNoMulti); + try { + Utils.loadData({ + name: "foo.json", + locale: "de-DE", + basePath: "asdf", + callback: function (results) { + test.ok(results); + Utils.loadData({ + name: "foo.json", + locale: "de-DE", + basePath: "foobar", + callback: function (results2) { + // if there is a cache miss when it attempts to load a file from disk twice + // then the mock loader will throw an exception, which is expected here + // because the base paths are different and Utils.loadData should try to + // load two files with the same name but different bases. + test.fail(); + test.done(); + } + }); + } + }); + } catch (e) { + test.done(); + } + }, + testLoadDataCacheResultAlreadyMerged: function(test) { ilib.data.foo = ilib.data.foo_de = ilib.data.foo_und_DE = ilib.data.foo_de_DE = undefined; ilib.setLoaderCallback(mockLoaderNoMulti);