catberry / catberry-l10n

Localization module for catberry framework
https://catberry.github.io/
Other
3 stars 1 forks source link

Language definition by the URI path segment #11

Closed dschewchenko closed 9 years ago

dschewchenko commented 9 years ago

Hello What about setting/getting language by uri segment?

rdner commented 9 years ago

Could you describe an example?

dschewchenko commented 9 years ago

www.example.com/en www.example.com/ru

Usualy priority of language is: 1 uri 2 cookie 3 browser language

rdner commented 9 years ago

Probably it's not a good idea to force everyone implement language switching like that. Because a lot of people use different approaches doing that. For example,

So, the approach you described is very easy to implement in your project using Catberry's routing rules and adding LocaleStore that takes locale parameter from the URI. The store data can be used by any other stores without performance impact.

For example, you have such route rule:

module.exports = [
  '/:locale[LocaleStore]/somecrazysegment'
];

Also, you have LocaleStore:

LocaleStore.prototype.load = function () {
  // here can be a profile's language request or something else
  return this.$context.state.locale;
};

So, we have LocaleStore and now we can use it.

SomeStore.prototype.load = function () {
  return Promise.all([
    this.$context.getStoreData('LocaleStore'),
    this._uhr.get('http://api.some.org/data')
  ])
    .then(function (results) {
      return {
        locale: results[0],
        obj: results[1]
      };
    });
};

After that, you can use such data in your component like it was in the example:

Component.prototype.render = function () {
  var self = this;
  return this.$context.getStoreData()
    .then(function (data) {
       return {
         localizedEat: self._l10n.get(data.locale, 'EAT'),
         localizedApple: util.format(
           self._l10n.pluralize(data.locale, 'APPLE', data.obj.appleCount),
           appleCount
         )
       };
   });
};
dschewchenko commented 9 years ago

Oh, thanx for fast and full answer )) And I have small question about catberry. How I can to implement Accordion using my own library and catberrys events 'autobinder'?

P.S. I think that You are working on very fantastic and perspective project

rdner commented 9 years ago

This question is kinda offtop here. You can use manual event listener adding in bind/unbind methods of any component. Just wrap your Accordion with the Catberry component and implement everything you need in bind/unbind methods. For example, there is an answer how to use jQuery with Catberry here.

rdner commented 9 years ago

FYI I've posted your question to StackOverflow here.

dschewchenko commented 9 years ago

Big thanx :) If I can, I will help You