Code Monkey home page Code Monkey logo

Comments (24)

craPkit avatar craPkit commented on July 28, 2024 2

It seems you can work around this issue using computed properties, like this:

Polymer({
  properties: {
    errorMessage: {
      type: String,
      computed: '_initErrorMessage(localize)'
    },
  },
  _initErrorMessage: function(localize) {
    return localize('myMsg');
  }
});

from app-localize-behavior.

hyyan avatar hyyan commented on July 28, 2024 1

You can use it like that:

this.async(function(){
    this.localize('translation')
},100);

from app-localize-behavior.

lluisgener avatar lluisgener commented on July 28, 2024 1

@davebaol Me neither, I think it has something to do with the computed properties and the way Polymer handles them. But, why not make localize a regular function?

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

I think it should be this.localize('login_failed'), not just localize('login_failed').
However I got it working from inside regular functions only, not from inside lifecycle functions like ready or even attached. Not sure why, though. Maybe this has something to do with computed properties.

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

@hyyan
Thanks, this looks like an acceptable solution most of the times.
However it's still not clear why computed functions are not available in lifecycle functions.
It seems that Polymer documentation is lacking on this topic. 😞

from app-localize-behavior.

Naehs avatar Naehs commented on July 28, 2024

Thanks guys !

I will try it later.
In the case where i want to call a function inside the ready function or a normal function, how it's work ?

For example : See the showToast() function

<script>
 Polymer({
   is: 'my-login',

   ready: function() {
     this.async(function() {
       this.showToast("This is a message...");
     },100);
   },

   otherFunction: function() {
     this.showToast("This is a message...");
   },

   showToast: function(message) {
     toast.show({text: message, duration: 5000});
   }
 });
</script>

Thanks,
Loïc.

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

Just replace the text message with this.localize('myKey');
Also, about the function you're passing to async, you should either bind it to this or make it an arrow function.

from app-localize-behavior.

Naehs avatar Naehs commented on July 28, 2024

Hi guys !

I tried this after your recommendation :

toastMessage.show({horizontalAlign: 'right', verticalAlign: 'top', duration: 5000, text: this.async(function() { this.localize('login_failed'); },100)});

And I still have :

Uncaught ReferenceError: this.localize is not defined()

Loïc.

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

@SheanYu
Try something like this

this.async((function() { 
  toastMessage.show({
    horizontalAlign: 'right',
    verticalAlign: 'top',
    duration: 5000,
    text: this.localize('login_failed')
  });
}).bind(this), 100);

from app-localize-behavior.

Naehs avatar Naehs commented on July 28, 2024

@davebaol
Same error...

Note that i'm in the ready function :

ready: function() {
  ...

  loginForm.addEventListener('iron-form-response', function(event) {
    if (event.detail.response.error) {
      this.async((function() { 
        toastMessage.show({
          horizontalAlign: 'right',
          verticalAlign: 'top',
          duration: 5000,
          text: this.localize('login_failed')
        });
      }).bind(this), 100);
    } else {
      ...
    }
  });

  ...
},

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

@SheanYu
Actually, that code is executed out of the ready function since it's an event handler.
You just have to bind the event handler to this like that (function(event) {...}).bind(this)

from app-localize-behavior.

Naehs avatar Naehs commented on July 28, 2024

@davebaol

With this :

ready: function() {
  ...

  loginForm.addEventListener('iron-form-response', function(event) {
    if (event.detail.response.error) {
      this.async((function() { 
        toastMessage.show({
          horizontalAlign: 'right',
          verticalAlign: 'top',
          duration: 5000,
          text: this.localize('login_failed')
        });
      }).bind(this), 100);
    } else {
      ...
    }
  }).bind(this);

  ...
},

I got this error :

Uncaught TypeError: Cannot read property 'bind' of undefined()

ready  @ my-login.html:165
_invokeBehavior @ polymer-micro.html:455
_doBehavior @ polymer-micro.html:445
_readySelf @ polymer-mini.html:88
_ready @ polymer-mini.html:75
_tryReady @ polymer-mini.html:60
_initFeatures @ polymer.html:4053
createdCallback @ polymer-micro.html:202
window.Polymer @ polymer-micro.html:65
(anonymous function) @ my-login.html:124

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

@SheanYu
Well, you're binding this to nothing. This should make it work

  loginForm.addEventListener('iron-form-response', (function(event) {
    ...
  }).bind(this));

from app-localize-behavior.

lluisgener avatar lluisgener commented on July 28, 2024

I'm having problems too with localize in the ready event. It says localize is not a function.

The (ugly) async approach works most of the times, but sometimes 100ms is not enough and it fails. I know I can increase the timeout, but I think it would be better to have a solution to make it always work.

I understand that resources may not yet be loaded, but I expect it to be another kind of error...

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

I understand that resources may not yet be loaded, but I expect it to be another kind of error...

I don't think it's a matter of resources not loaded yet

from app-localize-behavior.

davebaol avatar davebaol commented on July 28, 2024

@lluisgener
Because being localize a computed property when language or resources change all [[localize(...)]] are dynamically re-calculated.

from app-localize-behavior.

lluisgener avatar lluisgener commented on July 28, 2024

@davebaol
I see. But I guess only bound localizations are re-calculated, not the ones invoked programatically. So maybe a good solution would be to keep the computed property as is, and also have a regular function which should be the recommended in a no-bound usage.

Or make the Polymer team invoke the ready event when computed properties have been initialized, if it's possible...

from app-localize-behavior.

Mika83AC avatar Mika83AC commented on July 28, 2024

So is there even any way of calling localize from within a polymer element, indifferent of in the ready() function or any other element function?

As it is usual to process UI texts in functions, it is necessary to grab translations in the code and then pass it to the UI.

Regards,
Michael

from app-localize-behavior.

lluisgener avatar lluisgener commented on July 28, 2024

@Mika83AC look at @craPkit post. It worked flawlessly for me. But you need to tweak a little your code to correctly use computed properties.

Regards.

from app-localize-behavior.

Mika83AC avatar Mika83AC commented on July 28, 2024

Hm... sure, this is possible but creates an immense overhead (code size, complexity ...). This is just not possible for larger applications and can't be the solution the Polymer team wants to see.

So I really hope they have another solution for us. :)

from app-localize-behavior.

lluisgener avatar lluisgener commented on July 28, 2024

I think that it's the way to go, because localize is a computed method to be able to change language during execution, and I think the better way to do it is with a calculated method, as it is.

from app-localize-behavior.

Mika83AC avatar Mika83AC commented on July 28, 2024

** Removed because of completely bullshit, sorry :D **

from app-localize-behavior.

craPkit avatar craPkit commented on July 28, 2024

Doesn't this.localize('translationKey') work outside of ready() anyway?

from app-localize-behavior.

shaffi avatar shaffi commented on July 28, 2024

I manage to resolve this way

<link rel="import" href="../bower_components/app-localize-behavior/app-localize-behavior.html">

<dom-module id="x-custom">

  <template>
    <span> [[_greetings(name, localize)]]</span>
  </template>

  <script>
    Polymer({

      is: 'x-custom',
      behaviors: [
          Polymer.AppLocalizeBehavior
      ],
      properties: {
        name: String,
        language: { type: String,  value: 'zh-CN' }
      },
      attached: function() {
        this.loadResources(this.resolveUrl('locales.json'));
      },
      _greetings: function(name, localize) {
        return  localize('hello')+ ' ' + name;
      }

    });
  </script>

</dom-module>

from app-localize-behavior.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.