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

Access resources as objects instead of strings #49

Closed
renatoch opened this issue May 23, 2015 · 6 comments
Closed

Access resources as objects instead of strings #49

renatoch opened this issue May 23, 2015 · 6 comments

Comments

@renatoch
Copy link

Is there a way to access resource strings using object properties / keys instead of passing strings?

I've tried to get access to the resource bundle, but the variable is private, so I cannot even iterate to obtain list of keys.

This would make the code more readable, better syntax, and color coding in my text editor would be able to differentiate from regular strings in the code.

How I use today:

    var resourcePath = 'Document.Create';

    locale.getString(resourcePath + '.New');

How I would like to use:

    var resDocCreate = locale.keys('Document.Create');

    locale.getString(resDocCreate.New); 
@doshprompt
Copy link
Owner

Yes I think this is possible, however, you will still need to wrap the getString in a ready promise block, will that be fine?

@renatoch
Copy link
Author

Perhaps that wouldn't be needed. I intend to declare the variable in my controller initialization, and use locale.getString only on error messages and other stuff, which should give enough time to load all resources.

Me and a team member, @thiagoalves, were looking at promises and thread locking as a solution to another issue.
The idea is to either prevent page being shown while resources are not ready, or locking the thread once we call getString but the resource is not yet available.
This way I wouldn't need to worry about ready promise, also making the code cleaner.

Does that make sense?

@doshprompt
Copy link
Owner

@renatoch if you want to prevent the page from loading, you can utilize locale.ready() which returns a promise. It's fully compatible with ui-router and ng-router resolves out-of-the-box.

The only caveat is that you will have to manually maintain the list of all possible resource files that must be loaded for any route.

For example:

resolve: {
  langs: function (locale) {
    return locale.ready([
      'hello',
      'world'
    ]);
  }
}

will block route changes until both hello.lang.json and world.lang.json have been loaded successfully.

So if you introduce even a single localization key from say common.lang.json then you will have to manually add common to the array passed to locale.ready

Does that make sense?

@renatoch
Copy link
Author

renatoch commented Jun 1, 2015

Thanks for the info! We'll try it out and let you know.
Em 01/06/2015 14:28, "Rahul Doshi" notifications@github.com escreveu:

@renatoch https://github.com/renatoch if you want to prevent the page
from loading, you can utilize locale.ready() which returns a promise.
It's fully compatible with ui-router and ng-router resolves out-of-the-box.

The only caveat is that you will have to manually maintain the list of all
possible resource files that must be loaded for any route.

For example:

resolve: {
langs: function (locale) {
return locale.ready([
'hello',
'world'
]);
}
}

will block route changes until both hello.lang.json and world.lang.json
have been loaded successfully.


Reply to this email directly or view it on GitHub
#49 (comment)
.

cdjackson added a commit to cdjackson/angular-localization that referenced this issue Jun 6, 2015
cdjackson added a commit to cdjackson/angular-localization that referenced this issue Jun 6, 2015
@cdjackson
Copy link

I've implemented a getBundle() method to allow me to use angular-localization to load a complete bundle into another package which has its own i18n system. This might also cover the request here?

To use, you need to call -:

ready("bundle").then(function() {
  localised_bundle = locale.getBundle("bundle");
})

#51

@doshprompt
Copy link
Owner

You can now listen on localeEvents.resourceUpdates which will include the bundle

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

3 participants