• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iain/http_accept_language: Ruby on Rails plugin. Fishes out the Accept-Language ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

iain/http_accept_language

开源软件地址:

https://github.com/iain/http_accept_language

开源编程语言:

Ruby 94.2%

开源软件介绍:

HttpAcceptLanguage Build Status

A gem which helps you detect the users preferred language, as sent by the "Accept-Language" HTTP header.

The algorithm is based on RFC 2616, with one exception: when a user requests "en-US" and "en" is an available language, "en" is deemed compatible with "en-US". The RFC specifies that the requested language must either exactly match the available language or must exactly match a prefix of the available language. This means that when the user requests "en" and "en-US" is available, "en-US" would be compatible, but not the other way around. This is usually not what you're looking for.

Since version 2.0, this gem is Rack middleware.

Example

The http_accept_language method is available in any controller:

class SomeController < ApplicationController
  def some_action
    http_accept_language.user_preferred_languages # => %w(nl-NL nl-BE nl en-US en)
    available = %w(en en-US nl-BE)
    http_accept_language.preferred_language_from(available) # => 'nl-BE'

    http_accept_language.user_preferred_languages # => %w(en-GB)
    available = %w(en-US)
    http_accept_language.compatible_language_from(available) # => 'en-US'

    http_accept_language.user_preferred_languages # => %w(nl-NL nl-BE nl en-US en)
    available = %w(en nl de) # This could be from I18n.available_locales
    http_accept_language.preferred_language_from(available) # => 'nl'
  end
end

You can easily set the locale used for i18n in a before-filter:

class SomeController < ApplicationController
  before_filter :set_locale

  private
    def set_locale
      I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
    end
end

If you want to enable this behavior by default in your controllers, you can just include the provided concern:

class ApplicationController < ActionController::Base
  include HttpAcceptLanguage::AutoLocale

#...
end

Then set available locales in config/application.rb:

config.i18n.available_locales = %w(en nl de fr)

To use the middleware in any Rack application, simply add the middleware:

require 'http_accept_language'
use HttpAcceptLanguage::Middleware
run YourAwesomeApp

Then you can access it from env:

class YourAwesomeApp

  def initialize(app)
    @app = app
  end

  def call(env)
    available = %w(en en-US nl-BE)
    language = env.http_accept_language.preferred_language_from(available)

    [200, {}, ["Oh, you speak #{language}!"]]
  end

end

Available methods

  • user_preferred_languages: Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE, sanitized and all.
  • preferred_language_from(languages): Finds the locale specifically requested by the browser
  • compatible_language_from(languages): Returns the first of the user_preferred_languages that is compatible with the available locales. Ignores region.
  • sanitize_available_locales(languages): Returns a supplied list of available locals without any extra application info that may be attached to the locale for storage in the application.
  • language_region_compatible_from(languages): Returns the first of the user preferred languages that is also found in available languages. Finds best fit by matching on primary language first and secondarily on region. If no matching region is found, return the first language in the group matching that primary language.

Installation

Without Bundler

Install the gem http_accept_language

With Bundler

Add the gem to your Gemfile:

gem 'http_accept_language'

Run bundle install to install it.


Released under the MIT license




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
siyujie/OkHttpLogger-Frida: Frida 实现拦截okhttp的脚本发布时间:2022-06-18
下一篇:
Sponsor @httptoolkit on GitHub Sponsors · GitHub发布时间:2022-06-17
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap