Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't force english language #98

Merged
merged 1 commit into from
Aug 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/google_visualr/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(data_table, options={})
@data_table = data_table
@listeners = []
@version = options.delete(:version) || DEFAULT_VERSION
@language = options.delete(:language) || "en"
@language = options.delete(:language)
@material = options.delete(:material) || false
send(:options=, options)
end
Expand Down Expand Up @@ -74,7 +74,9 @@ def to_js(element_id)
# Parameters:
# *div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
def load_js(element_id)
"\n google.load('visualization', '#{version}', {packages: ['#{package_name}'], language: '#{language}', callback: #{chart_function_name(element_id)}});"
language_opt = ", language: '#{language}'" if language

"\n google.load('visualization', '#{version}', {packages: ['#{package_name}']#{language_opt}, callback: #{chart_function_name(element_id)}});"
end

# Generates JavaScript function for rendering the chart.
Expand Down
6 changes: 6 additions & 0 deletions spec/google_visualr/base_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@chart.version.should == GoogleVisualr::BaseChart::DEFAULT_VERSION
@chart.material.should == false
@chart.options.should == { "legend" => "Test Chart", "width" => 800, "is3D" => true }
@chart.language.should == nil
end

it "accepts version attribute" do
Expand Down Expand Up @@ -98,6 +99,11 @@
js.should include("<script")
end

it "generates JS without any locale as default" do
js = @chart.to_js("body")
js.should == base_chart_js("body", nil)
end

it "generates JS of a different locale" do
@chart.language = "ja"

Expand Down
12 changes: 7 additions & 5 deletions spec/support/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ def base_chart(data_table = data_table())
GoogleVisualr::BaseChart.new(data_table, { :legend => "Test Chart", :width => 800, :is3D => true })
end

def base_chart_js_without_script_tag(div_class = "div_class", language = "en")
js = "\n google.load('visualization', '1.0', {packages: ['basechart'], language: '#{language}', callback: draw_#{div_class}});"
def base_chart_js_without_script_tag(div_class = "div_class", language = nil)
language_opt = ", language: '#{language}'" if language

js = "\n google.load('visualization', '1.0', {packages: ['basechart']#{language_opt}, callback: draw_#{div_class}});"
js << "\n function draw_#{div_class}() {"
js << "\n var data_table = new google.visualization.DataTable();data_table.addColumn({\"type\":\"string\",\"label\":\"Year\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Sales\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Expenses\"});data_table.addRow([{v: \"2004\"}, {v: 1000}, {v: 400}]);data_table.addRow([{v: \"2005\"}, {v: 1200}, {v: 450}]);data_table.addRow([{v: \"2006\"}, {v: 1500}, {v: 600}]);data_table.addRow([{v: \"2007\"}, {v: 800}, {v: 500}]);\n var chart = new google.visualization.BaseChart(document.getElementById('#{div_class}'));"
js << "\n chart.draw(data_table, {legend: \"Test Chart\", width: 800, is3D: true});"
js << "\n };"
end

def base_chart_js(div_class = "div_class", language = "en")
def base_chart_js(div_class = "div_class", language = nil)
js = "\n<script type='text/javascript'>"
js << base_chart_js_without_script_tag(div_class, language)
js << "\n</script>"
end

def base_chart_with_listener_js(div_class = "div_class")
js = "\n<script type='text/javascript'>"
js << "\n google.load('visualization', '1.0', {packages: ['basechart'], language: 'en', callback: draw_#{div_class}});"
js << "\n google.load('visualization', '1.0', {packages: ['basechart'], callback: draw_#{div_class}});"
js << "\n function draw_#{div_class}() {"
js << "\n var data_table = new google.visualization.DataTable();data_table.addColumn({\"type\":\"string\",\"label\":\"Year\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Sales\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Expenses\"});data_table.addRow([{v: \"2004\"}, {v: 1000}, {v: 400}]);data_table.addRow([{v: \"2005\"}, {v: 1200}, {v: 450}]);data_table.addRow([{v: \"2006\"}, {v: 1500}, {v: 600}]);data_table.addRow([{v: \"2007\"}, {v: 800}, {v: 500}]);\n var chart = new google.visualization.BaseChart(document.getElementById('#{div_class}'));"
js << "\n google.visualization.events.addListener(chart, 'select', function() {test_event(chart);});"
Expand All @@ -44,7 +46,7 @@ def base_chart_with_listener_js(div_class = "div_class")

def material_chart(div_class = "div_class")
js = "\n<script type='text/javascript'>"
js << "\n google.load('visualization', '1.0', {packages: ['basechart'], language: 'en', callback: draw_#{div_class}});"
js << "\n google.load('visualization', '1.0', {packages: ['basechart'], callback: draw_#{div_class}});"
js << "\n function draw_#{div_class}() {"
js << "\n var data_table = new google.visualization.DataTable();data_table.addColumn({\"type\":\"string\",\"label\":\"Year\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Sales\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Expenses\"});data_table.addRow([{v: \"2004\"}, {v: 1000}, {v: 400}]);data_table.addRow([{v: \"2005\"}, {v: 1200}, {v: 450}]);data_table.addRow([{v: \"2006\"}, {v: 1500}, {v: 600}]);data_table.addRow([{v: \"2007\"}, {v: 800}, {v: 500}]);\n var chart = new google.charts.Base(document.getElementById('#{div_class}'));"
js << "\n chart.draw(data_table, {legend: \"Test Chart\", width: 800, is3D: true});"
Expand Down