locale metadata, including hidden locales for item loading and selection

This commit is contained in:
Emi Matchu 2013-01-17 22:16:34 -06:00
parent ef2423e87f
commit c9ae7155b1
9 changed files with 119 additions and 12 deletions

View file

@ -57,7 +57,7 @@ class ApplicationController < ActionController::Base
end
def valid_locale?(locale)
locale && I18n.available_locales.include?(locale.to_sym)
locale && I18n.usable_locales.include?(locale.to_sym)
end
end

View file

@ -100,7 +100,7 @@ class ItemsController < ApplicationController
raise ActiveRecord::RecordNotFound, 'Pet type not found'
end
@items = @pet_type.needed_items.with_translations(I18n.locale).
@items = @pet_type.needed_items.with_translations.
alphabetize_by_translations
assign_closeted!

View file

@ -103,9 +103,18 @@ module ApplicationHelper
end
def locale_options
I18n.available_locales.map do |available_locale|
current_locale_is_public = false
options = I18n.public_locales.map do |available_locale|
current_locale_is_public = true if I18n.locale == available_locale
[translate('locale_name', :locale => available_locale), available_locale]
end
unless current_locale_is_public
name = translate('locale_name', :locale => I18n.locale) + ' (alpha)'
options << [name, I18n.locale]
end
options
end
def localized_cache(key={}, &block)

View file

@ -267,8 +267,9 @@ class Item < ActiveRecord::Base
end
def needed_translations
translatable_locales = Set.new(I18n.locales_with_neopets_language_code)
translated_locales = Set.new(translations.map(&:locale))
I18n.available_locales.reject { |locale| translated_locales.include?(locale) }
translatable_locales - translated_locales
end
def self.all_by_ids_or_children(ids, swf_assets)

View file

@ -25,7 +25,7 @@ class Pet < ActiveRecord::Base
require 'ostruct'
begin
neopets_language_code = I18n.translate('neopets_language_code')
neopets_language_code = I18n.neopets_language_code_for(options[:locale])
envelope = PET_VIEWER.request([name, 0]).post(
:timeout => 2,
:headers => {
@ -105,10 +105,11 @@ class Pet < ActiveRecord::Base
last_pet_loaded = nil
reloaded_pets = Parallel.map(candidates.keys, :in_threads => 8) do |locale|
Rails.logger.info "Reloading #{name} in #{locale}"
last_pet_loaded = Pet.load(name, :item_scope => Item.with_translations,
:locale => locale)
reloaded_pet = Pet.load(name, :item_scope => Item.with_translations,
:locale => locale)
Pet.connection_pool.with_connection { reloaded_pet.save! }
last_pet_loaded = reloaded_pet
end
reloaded_pets.map(&:save!)
previous_candidates = candidates
candidates = last_pet_loaded.item_translation_candidates

View file

@ -0,0 +1,51 @@
module LocaleMeta
PUBLIC_LOCALES = []
USABLE_LOCALES = []
NEOPETS_LANGUAGE_CODES_BY_LOCALE = {}
LOCALES_WITH_NEOPETS_LANGUAGE_CODE = []
COMPATIBLE_LOCALES = {}
end
config = YAML.load_file(Rails.root.join('config', 'locale_meta.yml'))
config.each do |locale_str, locale_meta|
locale = locale_str.to_sym
visibility = locale_meta['visibility']
if visibility == 'public'
LocaleMeta::PUBLIC_LOCALES << locale
LocaleMeta::USABLE_LOCALES << locale
elsif visibility == 'private'
LocaleMeta::USABLE_LOCALES << locale
end
if locale_meta.has_key?('neopets_language_code')
neopets_language_code = locale_meta['neopets_language_code']
LocaleMeta::NEOPETS_LANGUAGE_CODES_BY_LOCALE[locale] = neopets_language_code
LocaleMeta::LOCALES_WITH_NEOPETS_LANGUAGE_CODE << locale
elsif locale_meta.has_key?('compatible_with')
compatible_locale = locale_meta['compatible_with'].to_sym
LocaleMeta::COMPATIBLE_LOCALES[locale] = compatible_locale
else
raise "locale #{locale} must either have a neopets_language_code or " +
"be compatible_with a locale that does"
end
end
module I18n
def self.public_locales
LocaleMeta::PUBLIC_LOCALES
end
def self.usable_locales
LocaleMeta::USABLE_LOCALES
end
def self.locales_with_neopets_language_code
LocaleMeta::LOCALES_WITH_NEOPETS_LANGUAGE_CODE
end
def self.neopets_language_code_for(locale)
LocaleMeta::NEOPETS_LANGUAGE_CODES_BY_LOCALE[locale]
end
end

47
config/locale_meta.yml Normal file
View file

@ -0,0 +1,47 @@
en:
visibility: public
neopets_language_code: en
en-MEEP:
visibility: public
compatible_with: en
pt:
visibility: private
neopets_language_code: pt
es:
visibility: none
neopets_language_code: es
nl:
visibility: none
neopets_language_code: nl
de:
visibility: none
neopets_language_code: de
fr:
visibility: none
neopets_language_code: fr
it:
visibility: none
neopets_language_code: it
zh-CN:
visibility: none
neopets_language_code: ch
zh-TW:
visibility: none
neopets_language_code: zh
ja:
visibility: none
neopets_language_code: ja
ko:
visibility: none
neopets_language_code: ko

View file

@ -5,7 +5,6 @@ en:
infinite_closet: Infinite Closet
modeling_hub: Modeling Hub
locale_name: English
neopets_language_code: en
activerecord:
attributes:

View file

@ -1,6 +1,5 @@
pt:
locale_name: Portuguese
neopets_language_code: pt
# This is a placeholder Portuguese locale, so that we can look up items.
# Everything else will fall back to English.
# This is a placeholder Portuguese locale, so that we can test things
# involving its presence.