Skip to content

Commit

Permalink
bugfix: locale was not changing based on the users session
Browse files Browse the repository at this point in the history
By using a DEFAULT_OPTIONS constant, we were setting the locale to the locale of the first request (usually en).

To get around this, make it a class method which we call when creating the configurations.

Also added checking of the locale. If it doesn't exist in TinyMCE, default to English.
  • Loading branch information
Kieran Pilkington committed Mar 25, 2010
1 parent 58fffef commit fc84549
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/tiny_mce/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ class Configuration
# Also loads which options are valid, and provides an plugins attribute to allow
# more configuration options dynamicly

DEFAULT_OPTIONS = {
'mode' => 'textareas',
'editor_selector' => 'mceEditor',
'theme' => 'simple',
'language' => (defined?(I18n) ? I18n.locale[0,2] : :en)
}
# The default tiny_mce options. Tries it's best to determine the locale
# If the current locale doesn't have a lang in TinyMCE, default to en
def self.default_options
locale = I18n.locale[0,2] if defined?(I18n)
locale = :en unless locale && valid_langs.include?(locale)
{ 'mode' => 'textareas', 'editor_selector' => 'mceEditor',
'theme' => 'simple', 'language' => locale }
end

# The YAML file might not exist, might be blank, might be invalid, or
# might be valid. Catch all cases and make sure we always return a Hash
def self.config_file_options
@@config_file_options ||= begin
tiny_mce_yaml_filepath = File.join(RAILS_ROOT, 'config', 'tiny_mce.yml')
# The YAML file might not exist, might be blank, might be invalid, or
# might be valid. Catch all cases and make sure we always return a Hash
(YAML::load(IO.read(tiny_mce_yaml_filepath)) rescue nil) || Hash.new
end
end

# Parse the options file and load it into an array
# (this method is called when tiny_mce is initialized - see init.rb)
# Parse the valid langs file and load it into an array
def self.valid_langs
@@valid_langs ||= begin
valid_langs_path = File.join(File.dirname(__FILE__), 'valid_tinymce_langs.yml')
File.open(valid_langs_path) { |f| YAML.load(f.read) }
end
end

# Parse the valid options file and load it into an array
def self.valid_options
@@valid_options ||= begin
valid_options_path = File.join(File.dirname(__FILE__), 'valid_tinymce_options.yml')
Expand All @@ -34,8 +43,9 @@ def self.valid_options

def initialize(options = {}, raw_options = nil)
options = Hash.new unless options.is_a?(Hash)
@options = DEFAULT_OPTIONS.merge(self.class.config_file_options.stringify_keys).
merge(options.stringify_keys)
@options = self.class.default_options.
merge(self.class.config_file_options.stringify_keys).
merge(options.stringify_keys)
@raw_options = [raw_options]
end

Expand Down
75 changes: 75 additions & 0 deletions lib/tiny_mce/valid_tinymce_langs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
# For more information about available languages, see
# http://tinymce.moxiecode.com/download_i18n.php
#

- ar
- az
- be
- bg
- bn
- br
- bs
- ca
- ch
- cs
- cy
- da
- de
- dv
- el
- en
- es
- et
- fa
- fi
- fr
- gl
- gu
- he
- hi
- hr
- hu
- hy
- ia
- id
- ii
- is
- it
- ja
- ko
- lb
- lt
- lv
- mk
- ml
- mn
- ms
- nb
- nl
- nn
- no
- pl
- ps
- pt
- ro
- ru
- sc
- se
- si
- sk
- sl
- sq
- sr
- sv
- ta
- te
- th
- tr
- tt
- tw
- uk
- ur
- vi
- zh
- zu

0 comments on commit fc84549

Please sign in to comment.