angular-gettext icon indicating copy to clipboard operation
angular-gettext copied to clipboard

Translating doesn't work in a Factory

Open smehrbrodt opened this issue 11 years ago • 6 comments

Using gettextCatalog.getString() doesn't work in a factory. It returns the untranslated string.

Is there anything I can do about this?

smehrbrodt avatar Apr 14 '15 12:04 smehrbrodt

Can you post more of your code? You most likely have something misconfigured, and it's impossible to tell what's going on otherwise.

gabegorelick avatar Apr 19 '15 18:04 gabegorelick

export function PersonService(gettextCatalog) {
  return {
    /**
     * A dictionary with gender translations
     */
    genders: {
      'm': gettextCatalog.getString('Male'),
      'f': gettextCatalog.getString('Female')
    },

  };
}

The problem might be that translations are not yet initialized when creating the factory?

smehrbrodt avatar Apr 20 '15 06:04 smehrbrodt

I can confirm that it does not work in my factory.

random-one avatar Oct 14 '15 11:10 random-one

I had the same issue, and solved it by setting the strings as translatable :

Instead of getting the translation for the strings, you do this :

export function PersonService(gettext) {
  return {
    /**
     * A dictionary with genders
     */
    genders: {
      'm': gettext('Male'),
      'f': gettext('Female')
    },
  };
}

And then using the translate filter in my view (supposing you do that through a controller) :

{{genders.m|translate}}

a-carvallo avatar Mar 29 '16 10:03 a-carvallo

No answer to this still? How come in a directive: gettextCatalog.getString("yes") returns "oui" ...but in a factory: gettextCatalog.getString("yes") returns "yes" ? I'd be very grateful of assistance else I will need to so some horrible hacking Thanks :)

rikpalmermm avatar Jun 07 '17 11:06 rikpalmermm

@rikpalmermm are you sure that factory is good place to translate strings? Could you anotate (call getext(key)) at factory layer and then use translate directive or filter at template;)

smth like: key string or {{factory.prop | filter}}

aprocom avatar Jun 08 '17 07:06 aprocom