NativeScript / nativescript-app-sync

♻️ Update your app without going through the app store!
MIT License
125 stars 24 forks source link

[Update translate] Synchronize nativescript-app-sync with nativescript-localize #59

Open kefahB opened 4 years ago

kefahB commented 4 years ago

Hi @EddyVerbruggen,

I use currently this plugin on my prod App, since we can not edit the Resources file and the translate wont be affected by the update. I've thought about how we can update the text or the translate on the app. I have done a little test, it work but I dont know how much it is a good solution !!

The idea is : On the nativescript-app-sync side : add a new options syncWithNsLocalize if it is set, create new boolean on the application settings __SYNC_NS_LOCALIZE__

On the nativescript-localize side : 1- if __SYNC_NS_LOCALIZE__ is set, we will import the json file __app__language__.json into localize.android.ts and localize.ios.ts 2- Override the string if the value that come from the json file dont match with the value from the resources file

I know the parser it is bizarre but I haven't thought about it yet if there is a better way to achieve this

// here it is the localize.ios.ts

const data = require('__app__language__.json');
let str = parser.parse(data, key, args);

if(str !== null && str  !== vsprintf(convertAtSignToStringSign(localizedString), args)) {
        console.log("VALUE ARE NOT SAME !")
        return str;
}

return vsprintf(convertAtSignToStringSign(localizedString), args);

const parser = {
    parse: (data: any, key: string, args?: any): string => {
        try {
            let splits = key.split(".");
            let value = null;
            for(let i = 0; i < splits.length; i++) {
                switch(i) {
                    case 0:
                        value = parser.value(data[splits[0]]);
                        break;
                    case 1:
                        value = parser.value(data[splits[0]][splits[1]]);
                        break;
                    case 2:
                        value = parser.value(data[splits[0]][splits[1]][splits[2]]);
                        break;
                    case 3:
                        value = parser.value(data[splits[0]][splits[1]][splits[2]][splits[3]]);
                        break;
                    case 4:
                        value = parser.value(data[splits[0]][splits[1]][splits[2]][splits[3]][splits[4]]);
                        break;
                    case 5:
                        value = parser.value(data[splits[0]][splits[1]][splits[2]][splits[3]][splits[4]][splits[5]]);
                        break;
                }
            }
            return vsprintf(value, args);
        } catch(e) {
            console.log(e)
            return null;
        }
    },
    value: (data) => {
        return data !== "undefined" ? data : null;
    }
}
kefahB commented 4 years ago

Hi @shiv19, what do you think about this ?

shiv19 commented 4 years ago

Interesting idea. So we could technically also add new localisation strings on the fly. Instead of using the switch case, we could do something like this,

let value;
while(splits.length) {
  value = parser.value(data[splits.shift()]);
}

That way it can handle any length of splits :) Not sure of the performance implications though. It would be worth stress testing this on a large application.

kefahB commented 4 years ago

Yes of cours :-D