Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kefahB opened this issue Apr 22, 2020 · 3 comments

Comments

@kefahB
Copy link

kefahB commented Apr 22, 2020

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
Copy link
Author

kefahB commented Sep 1, 2020

Hi @shiv19, what do you think about this ?

@shiv19
Copy link
Member

shiv19 commented Sep 1, 2020

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
Copy link
Author

kefahB commented Sep 1, 2020

Yes of cours :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants