#!/usr/bin/env ruby =begin index.cgi - Sample script for CGI Copyright (C) 2008 Masao Mutoh You may redistribute it and/or modify it under the same license terms as Ruby. =end $:<< "../../lib" begin require 'rubygems' rescue LoadError end require 'locale' require 'cgi' # Initialize Locale library. Locale.init(:driver => :cgi) cgi = CGI.new Locale.set_cgi(cgi) url_base = "http://#{cgi.server_name}:#{cgi.server_port}/" sample_locales = ["en-US", "fr-FR", "ja-JP"] langs = Locale.candidates(:type => :common) #Try :rfc, :simple and others. # # CGI part # print "Content-type:text/html; charset=UTF-8\n\n" puts %Q[ Sample script for CGI/ERB and Ruby/Locale

Ruby/Locale CGI sample scripts

Requested Locales order by the priority:

#{CGI.escapeHTML(langs.inspect)}

Auto-Detect the locale from the WWW browser

#{url_base}

Set locale as the "lang" parameter

    ] sample_locales.each do |lang| url = url_base + "?lang=" + lang puts %Q!
  1. #{CGI.escapeHTML(url)} [#{lang}]
  2. ! end url = url_base + "?lang=zh_CN;lang=ko_KR" puts %Q!
  3. #{CGI.escapeHTML(url)} [Plural locales]
  4. ! puts "
" puts %Q[

Set "lang" as the cookie value.

Click one of the link below, and then click "Auto-Detect the locale from the WWW browser".

    ] sample_locales.each do |lang| url = url_base + "cookie.cgi?lang=" + lang puts %Q!
  1. #{CGI.escapeHTML(url)} [#{lang}]
  2. ! end puts %Q!
  3. Clear the cookie value
  4. ! puts "
" puts "

Source codes

" puts "
    " Dir.glob("*cgi\0*rb)").sort.each do |src| unless /http.rb|makemo.rb/ =~ src puts %Q[
  1. #{src}
  2. ] end end puts %Q[

]