README.md in world-flags-0.2.4 vs README.md in world-flags-0.2.7
- old
+ new
@@ -202,59 +202,74 @@
```ruby
class MainController < ApplicationController
def home
@json = Property.all.to_gmaps4rails
- @country_code = WorldFlags::Geo.ip_country_code
+ @country_code = WorldFlags::Helper::Geo.ip_country_code
end
end
```
Alternatively you can include the modules directly into the controller:
```ruby
class MainController < ApplicationController
- include WorldFlags::Geo
- include WorldFlags::Browser
+ include WorldFlags::Helper::Geo
+ include WorldFlags::Helper::Browser
def home
@json = Property.all.to_gmaps4rails
@country_code = ip_country_code
@locale = browser_locale
end
end
```
-If you include the `WorldFlags::Locale` module, you can simply do:
+If you include the `WorldFlags::Helper::Locale` module, you can simply do:
```ruby
before_filter :set_locale
```
And it should set the I18n.locale appropriately, trying `params[locale], browser, ip address` in succession, defaulting to `I18n.default_locale`.
-For each locale it will check if it is a vaild locale, using
-`WorldFlags::Locale#valid_locales`
-For convenience you can also include `WorldFlags::All` to include all these modules.
+For each locale it will check if it is a valid locale. By default it will call `valid_locales` in the controller, which will first try `I18n.available_locales` and then fall-back to `WorldFlags#valid_locales`.
+You can override this behavior by defining you custom `valid_locales` method in the controller.
+For convenience you can include `WorldFlags::Helper::All` to include all these helper modules.
+
+Note that if you include the `WorldFlags::Helper::Geo you need the `httparty` gem as well.
+
Example:
```ruby
class MainController < ApplicationController
include WorldFlags::All
before_filter :set_locale
end
```
-You must set up valid locales for use with WorldFlags in some initializer:
+You can configure valid locales for use with WorldFlags in an initializer, fx `initializers/locales.rb` :
```ruby
# fx [:da, :en] or even ['da', 'en']
-WorldFlags::Locale.locales = my_valid_locales_list
+WorldFlags.valid_locales = my_valid_locales_list
```
+Note that if not set, this list is preconfigured to: `['en', 'de', 'es', 'ru']`
+
+Alternatively configure in your `application.rb` file:
+
+```ruby
+class Application < Rails::Application
+ # ...
+ config.i18n.available_locales = [:da, :sv, :no]
+``
+
+Note: This approach in turn works well with the `i18n-docs` gem ;)
+
## Post flag selection
Here an example of setting up a flag click handler in order to call the server with the country/locale/language selection of the flag.
```javascript
@@ -262,13 +277,32 @@
country = $(this).data('locale');
// full page reload
// window.location.href = "/locale/select/" + country;
+ // window.location.href = window.location.href + '?locale=' + country;
+
// async post
$.post("/locale/select", { "country": country }, function(data) {
console.log(data);
});
+});
+```
+
+This gem now comes with a simple javascript object `WorldFlagsUrlHelper` baked in. To use it, add the following to your ``application.js manifest.
+
+```
+//= require world_flags/url_helper
+```
+
+And use it something like this:
+
+```javascript
+$("li.flag").click(function() {
+ country = $(this).data('locale');
+
+ // full page reload with locale=xx param added to url :)
+ WorldFlagsUrlHelper.reloadWithLocaleParam('da');
});
```
## Enjoy