# Globalite - no match with yml files

**URL:** <https://www.ruby-forum.com/t/globalite-no-match-with-yml-files/138765>\
**Category:** Rails\
**Created:** [May 27, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/globalite-no-match-with-yml-files/138765 "2008-05-27T15:43:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [May 27, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/globalite-no-match-with-yml-files/138765/1 "2008-05-27T15:43:43Z")

</div>

Hello,

I use â€œglobaliteâ€ for my site but the system has some problems to find  
the right yml file.

Iâ€™ve defined the default language in environment.rb so:  
Globalite.language = :nl  
If I use a browser with default language fr for example, in the  
development.log I see that the system recognizes well the fr-FR.yml but  
it doesnâ€™t match this and use per default the nl.yml file to translate  
the error messages for example.

My development.log  
[globalite] client accepted locales: fr, nl, nl-be, en-us, en en  
[globalite] trying to match fr-\*  
[globalite] found a valid http header locale: fr  
[globalite] client accepted locales: fr, nl, nl-be, en-us, en en  
[globalite] trying to match fr-\*  
[globalite] Locale set to nl-\*

Thanks in advance for your help,

---

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [May 29, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/globalite-no-match-with-yml-files/138765/2 "2008-05-29T16:22:55Z")

</div>

I had the same problem, I solved it by replacing the code in  
ApplicationController with:

# Filters added to this controller apply to all controllers in the

application.

# Likewise, all the methods added will be available for all

controllers.

class ApplicationController \< ActionController::Base  
helper :all # include all helpers, all the time  
around\_filter :set\_locale

private

# Set the locale from the parameters, the session, or the navigator

# If none of these works, the Globalite default locale is set (en-\*)

def set\_locale  
# Get the current path and request method (useful in the layout  
for changing the language)  
@current\_path = request.env[‘PATH\_INFO’]  
@request\_method = request.env[‘REQUEST\_METHOD’]

```
# Try to get the locale from the parameters, from the session, and

```

then from the navigator  
if params[:user\_locale]  
# Store the locale in the session  
session[:locale] = Locale.code  
elsif session[:locale]  
Locale.code = session[:locale]  
else  
Locale.code = local\_case(get\_valid\_lang\_from\_accept\_header)  
end

```
logger.debug "[globalite] Locale set to #{Locale.code}"
# render the page
yield

# reset the locale to its default value
Locale.reset!

```

end

# Get a sorted array of the navigator languages

def get\_sorted\_langs\_from\_accept\_header  
accept\_langs = (request.env[‘HTTP\_ACCEPT\_LANGUAGE’] || “en-  
us,en;q=0.5”).split(/,/) rescue nil  
return nil unless accept\_langs

```
# Extract langs and sort by weight
# Example HTTP_ACCEPT_LANGUAGE: "en-au,en-

```

gb;q=0.8,en;q=0.5,ja;q=0.3"  
wl = {}  
accept\_langs.each {|accept\_lang|  
if (accept\_lang + ‘;q=1’) =~ /^(.+?);q=([^;]+).\*/  
wl[($2.to\_f rescue -1.0)]= $1  
end  
}  
logger.debug “[globalite] client accepted locales: #{wl.sort{|a,b|  
b[0] \<=\> a[0] }.map{|a| a[1] }.to\_sentence}”  
sorted\_langs = wl.sort{|a,b| b[0] \<=\> a[0] }.map{|a| a[1] }  
end

# Returns a valid language that best suits the HTTP\_ACCEPT\_LANGUAGE

request header.

# If no valid language can be deduced, then nil is

returned.  
def get\_valid\_lang\_from\_accept\_header  
# Get the sorted navigator languages and find the first one that  
matches our available languages  
get\_sorted\_langs\_from\_accept\_header.detect{|l|  
get\_matching\_ui\_locale(l) }  
end

# Returns the UI locale that best matches with the parameter

# or nil if not found

def get\_matching\_ui\_locale(locale)  
lang = locale[0,2].downcase

```
# Check with exact matching
if Globalite.ui_locales.values.include?(local_case(locale))
  local_case(locale)
end

# Check on the language only
Globalite.ui_locales.values.each do |value|
  value.to_s =~ /#{lang}-*/ ? value : nil
end

```

end  
def local\_case(l)  
if l[3,5]  
“#{l[0,2]}-#{l[3,5].upcase}”.to\_sym  
else  
“#{l[0,2]}-\*”.to\_sym  
end  
end  
end

Cheers

Geoffroy G.

---

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [May 29, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/globalite-no-match-with-yml-files/138765/3 "2008-05-29T16:24:07Z")

</div>

I solved this problem by modifying the ApplicationController with:

# Filters added to this controller apply to all controllers in the

application.

# Likewise, all the methods added will be available for all

controllers.

class ApplicationController \< ActionController::Base  
helper :all # include all helpers, all the time  
around\_filter :set\_locale

private

# Set the locale from the parameters, the session, or the navigator

# If none of these works, the Globalite default locale is set (en-\*)

def set\_locale  
# Get the current path and request method (useful in the layout  
for changing the language)  
@current\_path = request.env[‘PATH\_INFO’]  
@request\_method = request.env[‘REQUEST\_METHOD’]

```
# Try to get the locale from the parameters, from the session, and

```

then from the navigator  
if params[:user\_locale]  
# Store the locale in the session  
session[:locale] = Locale.code  
elsif session[:locale]  
Locale.code = session[:locale]  
else  
Locale.code = local\_case(get\_valid\_lang\_from\_accept\_header)  
end

```
logger.debug "[globalite] Locale set to #{Locale.code}"
# render the page
yield

# reset the locale to its default value
Locale.reset!

```

end

# Get a sorted array of the navigator languages

def get\_sorted\_langs\_from\_accept\_header  
accept\_langs = (request.env[‘HTTP\_ACCEPT\_LANGUAGE’] || “en-  
us,en;q=0.5”).split(/,/) rescue nil  
return nil unless accept\_langs

```
# Extract langs and sort by weight
# Example HTTP_ACCEPT_LANGUAGE: "en-au,en-

```

gb;q=0.8,en;q=0.5,ja;q=0.3"  
wl = {}  
accept\_langs.each {|accept\_lang|  
if (accept\_lang + ‘;q=1’) =~ /^(.+?);q=([^;]+).\*/  
wl[($2.to\_f rescue -1.0)]= $1  
end  
}  
logger.debug “[globalite] client accepted locales: #{wl.sort{|a,b|  
b[0] \<=\> a[0] }.map{|a| a[1] }.to\_sentence}”  
sorted\_langs = wl.sort{|a,b| b[0] \<=\> a[0] }.map{|a| a[1] }  
end

# Returns a valid language that best suits the HTTP\_ACCEPT\_LANGUAGE

request header.

# If no valid language can be deduced, then nil is

returned.  
def get\_valid\_lang\_from\_accept\_header  
# Get the sorted navigator languages and find the first one that  
matches our available languages  
get\_sorted\_langs\_from\_accept\_header.detect{|l|  
get\_matching\_ui\_locale(l) }  
end

# Returns the UI locale that best matches with the parameter

# or nil if not found

def get\_matching\_ui\_locale(locale)  
lang = locale[0,2].downcase

```
# Check with exact matching
if Globalite.ui_locales.values.include?(local_case(locale))
  local_case(locale)
end

# Check on the language only
Globalite.ui_locales.values.each do |value|
  value.to_s =~ /#{lang}-*/ ? value : nil
end

```

end  
def local\_case(l)  
if l[3,5]  
“#{l[0,2]}-#{l[3,5].upcase}”.to\_sym  
else  
“#{l[0,2]}-\*”.to\_sym  
end  
end  
end

Cheers

Geoffroy G.
