require 'spec_helper' require 'trip_advisor/translation' require 'trip_advisor/strings_file' describe TripAdvisor::StringsFile do describe '.read' do before(:each) do tempfile = Tempfile.open('Strings.txt') { |f| f << fixture_content('Strings.txt') } @strings_file = TripAdvisor::StringsFile.read(tempfile.path) end it "loads all strings into Translations" do @strings_file.translations.size.should == 16 end it "reads all language tags defined in the strings file" do @strings_file.language_tags.should == %w{en es} end end describe '.read' do before(:each) do tempfile = Tempfile.open('AndroidStrings.txt') { |f| f << fixture_content('AndroidStrings.txt') } @strings_file = TripAdvisor::StringsFile.read(tempfile.path) end it "loads all strings into Translations" do @strings_file.translations.size.should == 6 end it "reads all language tags defined in the strings file" do @strings_file.language_tags.should == %w{en es} end end describe '#[]' do before(:each) do tempfile = Tempfile.open('Strings.txt') { |f| f << fixture_content('Strings.txt') } @strings_file = TripAdvisor::StringsFile.read(tempfile.path) end it "reads a string by key from the file" do @strings_file['%1$d Passengers'].should_not be_nil end it "returns a Translation object" do translation = @strings_file['%1$d Passengers'] translation.should be_kind_of(TripAdvisor::Translation) end it "returns a Localization for each string" do translation = @strings_file['%1$d Passengers'] translation.localizations.size.should == 1 end it "allows indexed access to the localization" do translation = @strings_file['%1$d Passengers'] translation.localizations[0].string.should == '%1$d Passengers' end it "allows keyed access to the localizations by locale identifier" do translation = @strings_file['%1$d Passengers'] translation.localizations['en'].string.should == '%1$d Passengers' end end describe '#[]' do before(:each) do tempfile = Tempfile.open('AndroidStrings.txt') { |f| f << fixture_content('AndroidStrings.txt') } @strings_file = TripAdvisor::StringsFile.read(tempfile.path) end it "reads a string by key from the file" do @strings_file['flights_app_arrival'].should_not be_nil end it "returns a Translation object" do translation = @strings_file['flights_app_arrival'] translation.should be_kind_of(TripAdvisor::Translation) end it "returns a Localization for each string" do translation = @strings_file['flights_app_arrival'] translation.localizations.size.should == 1 end it "allows indexed access to the localization" do translation = @strings_file['flights_app_arrival'] translation.localizations[0].string.should == 'Arrival' end it "allows keyed access to the localizations by locale identifier" do translation = @strings_file['flights_app_arrival'] translation.localizations['en'].string.should == 'Arrival' end end describe '#merge_translations' do before(:each) do tempfile = Tempfile.open('Strings.txt') { |f| f << fixture_content('Strings.txt') } @strings_file = TripAdvisor::StringsFile.read(tempfile.path) end it "adds a new translation" do translation = TripAdvisor::Translation.new(id: 123456, key: 'TAFlights_exampleKey', note: 'Note for translator') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '1 Passenger', status: 'Active') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'es_CO', language_name: 'Spanish', string: '1 pasajero', status: 'Active') @strings_file.merge_translations([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['1 Passenger'].should_not be_nil new_strings_file['1 Passenger'].localizations.size.should == 2 localizations = new_strings_file['1 Passenger'].localizations.inject({}) { |hash, l| hash[l.locale_identifier] = l.string; hash } localizations.should == { 'en' => '1 Passenger', 'es_CO' => '1 pasajero' } end it "merges new localizations for an existing translation" do translation = TripAdvisor::Translation.new(id: 123456, key: '%1$d Passengers') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '%1$d Passengers', status: 'Active') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'es_CO', language_name: 'Spanish', string: '1 pasajero', status: 'Active') @strings_file.merge_translations([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['%1$d Passengers'].should_not be_nil new_strings_file['%1$d Passengers'].localizations.size.should == 2 localizations = new_strings_file['%1$d Passengers'].localizations.inject({}) { |hash, l| hash[l.locale_identifier] = l.string; hash } localizations.should == { 'en' => '%1$d Passengers', 'es_CO' => '1 pasajero' } end it "updates the string for an existing localization" do translation = TripAdvisor::Translation.new(id: 123456, key: '%1$d Passengers') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'es_CO', language_name: 'Spanish', string: '1 pasajero', status: 'Active') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '%1$d Passengers', status: 'Active') @strings_file.merge_translations([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['%1$d Passengers'].should_not be_nil new_strings_file['%1$d Passengers'].localizations.size.should == 2 localizations = new_strings_file['%1$d Passengers'].localizations.inject({}) { |hash, l| hash[l.locale_identifier] = l.string; hash } localizations.should == { 'en' => '%1$d Passengers', 'es_CO' => '1 pasajero' } end it "adds a note to an existing localization if there is not one" do @strings_file["%1$d Passengers"].note.should be_nil translation = TripAdvisor::Translation.new(id: 123456, key: '%1$d Passengers', note: "Number of Passengers") translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '%1$d Passengers', status: 'Active') @strings_file.merge_translations([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['%1$d Passengers'].note.should == 'Number of Passengers' end end describe '#merge_translations' do before(:each) do tempfile = Tempfile.open('AndroidStrings.txt') { |f| f << fixture_content('AndroidStrings.txt') } @strings_file = TripAdvisor::StringsFile.read(tempfile.path) end it "adds a new translation" do translation = TripAdvisor::Translation.new(id: 123456, key: 'flights_app_example_key', note: 'Note for translator') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '1 Passenger', status: 'Active') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'es_CO', language_name: 'Spanish', string: '1 pasajero', status: 'Active') @strings_file.merge_translations_android([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['flights_app_example_key'].should_not be_nil new_strings_file['flights_app_example_key'].localizations.size.should == 2 localizations = new_strings_file['flights_app_example_key'].localizations.inject({}) { |hash, l| hash[l.locale_identifier] = l.string; hash } localizations.should == { 'en' => '1 Passenger', 'es_CO' => '1 pasajero' } end it "merges new localizations for an existing translation" do translation = TripAdvisor::Translation.new(id: 123456, key: 'flights_app_example_key') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '%1$d Passengers', status: 'Active') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'es_CO', language_name: 'Spanish', string: '1 pasajero', status: 'Active') @strings_file.merge_translations_android([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['flights_app_example_key'].should_not be_nil new_strings_file['flights_app_example_key'].localizations.size.should == 2 localizations = new_strings_file['flights_app_example_key'].localizations.inject({}) { |hash, l| hash[l.locale_identifier] = l.string; hash } localizations.should == { 'en' => '%1$d Passengers', 'es_CO' => '1 pasajero' } end it "updates the string for an existing localization" do translation = TripAdvisor::Translation.new(id: 123456, key: 'flights_app_example_key') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'es_CO', language_name: 'Spanish', string: '1 pasajero', status: 'Active') translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '%1$d Passengers', status: 'Active') @strings_file.merge_translations_android([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['flights_app_example_key'].should_not be_nil new_strings_file['flights_app_example_key'].localizations.size.should == 2 localizations = new_strings_file['flights_app_example_key'].localizations.inject({}) { |hash, l| hash[l.locale_identifier] = l.string; hash } localizations.should == { 'en' => '%1$d Passengers', 'es_CO' => '1 pasajero' } end it "adds a note to an existing localization if there is not one" do @strings_file['flights_app_number_of_passengers'].note.should be_nil translation = TripAdvisor::Translation.new(id: 123456, key: 'flights_app_number_of_passengers', note: "Number of Passengers") translation.localizations << TripAdvisor::Translation::Localization.new(locale_identifier: 'en', language_name: 'English', string: '%1$d Passengers', status: 'Active') @strings_file.merge_translations_android([translation]) @strings_file.write # Read the new key back out new_strings_file = TripAdvisor::StringsFile.read(@strings_file.path) new_strings_file['flights_app_number_of_passengers'].note.should == 'Number of Passengers' end end end