From: Tom Hughes Date: Thu, 3 Sep 2009 21:55:42 +0000 (+0000) Subject: Add support for reloading the language table. X-Git-Tag: live~7248 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/73340e1780bfee7b3ebfd17f9fa204647ea40d34?ds=inline Add support for reloading the language table. --- diff --git a/app/models/language.rb b/app/models/language.rb index a33c76081..7bb0b6e77 100644 --- a/app/models/language.rb +++ b/app/models/language.rb @@ -3,6 +3,22 @@ class Language < ActiveRecord::Base has_many :diary_entries, :foreign_key => 'language' + def self.load(file) + Language.transaction do + YAML.load(File.read(file)).each do |k,v| + begin + Language.update(k, :english_name => v["english"], :native_name => v["native"]) + rescue ActiveRecord::RecordNotFound + Language.create do |l| + l.code = k + l.english_name = v["english"] + l.native_name = v["native"] + end + end + end + end + end + def name name = english_name name += " (#{native_name})" unless native_name.nil? diff --git a/db/migrate/034_create_languages.rb b/db/migrate/034_create_languages.rb index 6b8973cf3..ab5a7eee6 100644 --- a/db/migrate/034_create_languages.rb +++ b/db/migrate/034_create_languages.rb @@ -10,13 +10,7 @@ class CreateLanguages < ActiveRecord::Migration add_primary_key :languages, [:code] - YAML.load(File.read(RAILS_ROOT + "/config/languages.yml")).each do |k,v| - Language.create do |l| - l.code = k - l.english_name = v["english"] - l.native_name = v["native"] - end - end + Language.load(RAILS_ROOT + "/config/languages.yml") add_foreign_key :users, [:locale], :languages, [:code] add_foreign_key :diary_entries, [:language_code], :languages, [:code] diff --git a/script/locale/reload_languages b/script/locale/reload_languages new file mode 100755 index 000000000..b4f829c7b --- /dev/null +++ b/script/locale/reload_languages @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../config/environment' + +Language.load(RAILS_ROOT + "/config/languages.yml")