kelvinhokk / cordova-plugin-localization-strings

Cordova Plugin for Localization of Strings on the App
MIT License
119 stars 106 forks source link

(iOS) Does not support encoded characters in project name (&) #77

Open florian72810 opened 1 year ago

florian72810 commented 1 year ago

Hello 👋 Thanks for this plugin.

I have a use-case where the project name contains encoded characters food&good.

In my config.xml, the & is encoded as &amp; -> <name>food&amp;good</name>

Cordova handles this name and decode &amp;. Everything works fine on Xcode, I can build the project.

When this plugin is added, the prepare process return an error:

Executing script found in plugin cordova-plugin-localization-strings for hook "after_prepare": plugins/cordova-plugin-localization-strings/scripts/create_strings.js
Error: Cannot read property 'project' of undefined

I search the root cause and it seems the plugin is trying to find the project at path /food&amp;good/ and does not find it. It is expected as the project path is food&good. The function getProjectName does not decoded encoded characters.

To reproduce

  1. start a new Cordova project cordova create hello com.example.hello "Hello&World"
  2. go to project cd hello
  3. add iOS platform cordova platform add ios
  4. prepare iOS (it does work) cordova prepare ios
  5. add plugin cordova plugin add cordova-plugin-localization-strings
  6. prepare iOS again (it does NOT work) cordova prepare ios

Quick and dirty fix

For my case, I change the file scripts/create_ios_strings.js

diff --git a/node_modules/cordova-plugin-localization-strings/scripts/create_ios_strings.js b/node_modules/cordova-plugin-localization-strings/scripts/create_ios_strings.js
index 5d46c73..f286a2f 100755
--- a/node_modules/cordova-plugin-localization-strings/scripts/create_ios_strings.js
+++ b/node_modules/cordova-plugin-localization-strings/scripts/create_ios_strings.js
@@ -24,7 +24,7 @@ function getProjectName() {
     if (!matches)
         matches = config.match(new RegExp('<name short=".*?">(.*?)</name>', 'i'));

-    return (matches && matches[1]) || null;
+    return (matches && matches[1]).replace('&amp;','&') || null;
 }

 function initIosDir() {

Not dirty fix

I tried to find how cordova decode the project name, but I did not find it. It seems he lib can do this, but it adds a dependency (https://www.npmjs.com/package/he#hedecodehtml-options)

rodrigograca31 commented 1 year ago

Doesnt affect Android? 🤔

florian72810 commented 1 year ago

I don't use it with Android, I can't confirm if the plugin works fully as expected.

With the same reproduction steps, Android does work, no error at prepare step.