ReactNativeLocalization icon indicating copy to clipboard operation
ReactNativeLocalization copied to clipboard

string keys support

Open vaukalak opened this issue 9 years ago • 3 comments

Hello, I don't know if it's done specially, but string keys are not supported. Wouldn't it be more logical to write:

Hello {title}, you have ${amount} on your balance

and then

strings.formatString(strings.foo, {title : 'Mr', amount: 10000000})

?

vaukalak avatar Jun 12 '16 16:06 vaukalak

Yeah, could be a great alternative... If you'd like to implement surely I'll merge it.

stefalda avatar Jun 15 '16 16:06 stefalda

Ill created a method for our projects. Currently it only supports ...params instead of an object.

const Strings = new LocalizedStrings(...);
export const substitute = (key, ...params)=> {
    const translatedString = Strings[key];
    if (params.length === 0) {
        return translatedString;
    } else {
        let result = translatedString;
        for (var x = 0; x < params.length - 1; x++) {
            const search = '{' + x + '}';
            while (result.indexOf(search) >= 0) {
                result = result.replace(search, params[x + 1]);
            }
        }
        return result;
    }
};

maluramichael avatar Oct 28 '16 11:10 maluramichael

I assign directly functions to the localized string elements, it works like breeze.

 your_routine_has_triggered: (name) => `Your routine ${name} has been triggered 🎆`,
/* ... later... */
i18n.your_routine_has_triggered(routine.name)

jsdario avatar Dec 13 '16 11:12 jsdario