Skip to content

Commit

Permalink
rubocop corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Sep 2, 2024
1 parent 1b33f37 commit f556b8c
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 79 deletions.
20 changes: 20 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,23 @@ Lint/EmptyBlock:

Naming/MethodParameterName:
Exclude: [spec/**/*.rb]

RSpec/BeforeAfterAll:
Enabled: false

# could change to `#method` and `.method` instead
RSpec/DescribeSymbol:
Enabled: false

RSpec/NestedGroups:
Enabled: false

RSpec/EmptyExampleGroup:
Enabled: false

Lint/SuppressedException:
Enabled: false

# somehow crashes
RSpec/VariableName:
Enabled: false
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'
gemspec

Expand Down
30 changes: 15 additions & 15 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'bundler/setup'
require 'bundler/gem_tasks'
require 'bump/tasks'
Expand All @@ -8,6 +9,7 @@ require 'i18n_data'

task default: [:spec, :rubocop]

desc "Run tests"
task :spec do
sh "rspec --warnings spec/"
end
Expand All @@ -19,49 +21,47 @@ end

desc "write all languages to output"
task :all_languages do
I18nData.languages.keys.each do |lc|
I18nData.languages.each_key do |lc|
`rake languages LANGUAGE=#{lc}`
end
end

desc "write languages to output/languages_{language}"
task :languages do
raise unless language = ENV['LANGUAGE']
raise unless (language = ENV.fetch('LANGUAGE', nil))
`mkdir -p output`
data = I18nData.languages(language.upcase)
File.write "output/languages_#{language.downcase}.yml", data.to_yaml
end

desc "write all countries to output to debug"
task :all_countries do
I18nData.languages.keys.each do |lc|
I18nData.languages.each_key do |lc|
`rake countries LANGUAGE=#{lc}`
end
end

desc "write countries to output/countries_{language} to debug"
task :countries do
raise unless language = ENV['LANGUAGE']
raise unless (language = ENV.fetch('LANGUAGE', nil))
`mkdir -p output`
data = I18nData.countries(language.upcase)
File.open("output/countries_#{language.downcase}.yml",'w') {|f|f.puts data.to_yaml}
File.open("output/countries_#{language.downcase}.yml", 'w') { |f| f.puts data.to_yaml }
end

desc "write example output, just to show off :D"
task :example_output do
`mkdir -p example_output`

#all names for germany, france, united kingdom and unites states
['DE','FR','GB','US'].each do |cc|
# all names for germany, france, united kingdom and unites states
['DE', 'FR', 'GB', 'US'].each do |cc|
names = I18nData.languages.keys.map do |lc|
begin
[I18nData.countries(lc)[cc], I18nData.languages[lc]]
rescue I18nData::NoTranslationAvailable
nil
end
[I18nData.countries(lc)[cc], I18nData.languages[lc]]
rescue I18nData::NoTranslationAvailable
nil
end
File.open("example_output/all_names_for_#{cc}.txt",'w') do |f|
f.puts names.reject(&:nil?).map{|x|x*" ---- "} * "\n"
File.open("example_output/all_names_for_#{cc}.txt", 'w') do |f|
f.puts names.compact.map { |x| x * " ---- " } * "\n"
end
end
end
Expand All @@ -71,7 +71,7 @@ task :stats do
dir = "cache/file_data_provider"
[:languages, :countries].each do |type|
files = FileList["#{dir}/#{type}*"]
lines = File.readlines(files.first).reject{|l|l.empty?}
lines = File.readlines(files.first).reject(&:empty?)
puts "#{lines.size} #{type} in #{files.size} languages"
end
end
Expand Down
1 change: 1 addition & 0 deletions i18n_data.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
name = "i18n_data"
require "./lib/#{name}/version"

Expand Down
18 changes: 9 additions & 9 deletions lib/i18n_data.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'i18n_data/version'

module I18nData

class BaseException < StandardError
def to_s
"#{self.class} -- #{super}"
Expand All @@ -13,13 +13,13 @@ class AccessDenied < BaseException; end
class Unknown < BaseException; end

class << self
def languages(language_code='EN')
def languages(language_code = 'EN')
fetch :languages, language_code do
data_provider.codes(:languages, normal_to_region_code(language_code.to_s.upcase))
end
end

def countries(language_code='EN')
def countries(language_code = 'EN')
fetch :countries, language_code do
data_provider.codes(:countries, normal_to_region_code(language_code.to_s.upcase))
end
Expand All @@ -34,10 +34,10 @@ def language_code(name)
end

def data_provider
@data_provider ||= (
@data_provider ||= begin
require 'i18n_data/file_data_provider'
FileDataProvider
)
end
end

def data_provider=(provider)
Expand All @@ -57,15 +57,15 @@ def fetch(type, language_code)
def normal_to_region_code(normal)
{
"ZH" => "zh_CN",
"BN" => "bn_IN",
"BN" => "bn_IN"
}[normal] || normal
end

def recognise_code(type, search)
search = search.strip

# common languages first <-> faster in majority of cases
language_codes = ['EN','ES','FR','DE','ZH'] | available_language_codes
language_codes = ['EN', 'ES', 'FR', 'DE', 'ZH'] | available_language_codes

language_codes.each do |language_code|
options =
Expand All @@ -88,8 +88,8 @@ def recognise_code(type, search)
# NOTE: this is not perfect since the used provider might have more or less languages available
# but it's better than just using the available english language codes
def available_language_codes
@available_languges ||= begin
files = Dir[File.expand_path("../../cache/file_data_provider/languages-*", __FILE__)]
@available_language_codes ||= begin
files = Dir[File.expand_path('../cache/file_data_provider/languages-*', __dir__)]
files.map! { |f| f[/languages-(.*)\./, 1] }
end
end
Expand Down
25 changes: 11 additions & 14 deletions lib/i18n_data/file_data_provider.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module I18nData
module FileDataProvider
require 'fileutils'
Expand All @@ -6,7 +7,7 @@ module FileDataProvider
extend self

def codes(type, language_code)
unless data = read_from_file(cache_file_for(type, language_code))
unless (data = read_from_file(cache_file_for(type, language_code)))
raise NoTranslationAvailable, "#{type}-#{language_code}"
end
data
Expand All @@ -16,14 +17,12 @@ def write_cache(provider)
languages = provider.codes(:languages, 'EN').keys + ['zh_CN', 'zh_TW', 'zh_HK', 'bn_IN', 'pt_BR', 'sr@latin']
languages.map do |language_code|
[:languages, :countries].each do |type|
begin
data = provider.send(:codes, type, language_code)
write_to_file(data, cache_file_for(type, language_code))
rescue NoTranslationAvailable
$stderr.puts "No translation available for #{type} #{language_code}" if $DEBUG
rescue AccessDenied
$stderr.puts "Access denied for #{type} #{language_code}"
end
data = provider.send(:codes, type, language_code)
write_to_file(data, cache_file_for(type, language_code))
rescue NoTranslationAvailable
warn "No translation available for #{type} #{language_code}" if $DEBUG
rescue AccessDenied
warn "Access denied for #{type} #{language_code}"
end
end
end
Expand All @@ -33,7 +32,7 @@ def write_cache(provider)
def read_from_file(file)
return nil unless File.exist?(file)
data = {}
File.readlines(file, :encoding => 'utf-8').each do |line|
File.readlines(file, encoding: 'utf-8').each do |line|
code, translation = line.strip.split(DATA_SEPARATOR, 2)
data[code] = translation
end
Expand All @@ -43,12 +42,10 @@ def read_from_file(file)
def write_to_file(data, file)
return if data.empty?
FileUtils.mkdir_p File.dirname(file)
File.open(file,'w') do |f|
f.write data.map{|code, translation| "#{code}#{DATA_SEPARATOR}#{translation}" } * "\n"
end
File.write(file, data.map { |code, translation| "#{code}#{DATA_SEPARATOR}#{translation}" } * "\n")
end

def cache_file_for(type,language_code)
def cache_file_for(type, language_code)
file = "#{type}-#{language_code.sub('-', '_').upcase}"
File.join(File.dirname(__FILE__), '..', '..', 'cache', 'file_data_provider', "#{file}.txt")
end
Expand Down
27 changes: 13 additions & 14 deletions lib/i18n_data/live_data_provider.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'json'
require 'simple_po_parser'

Expand All @@ -7,14 +8,14 @@ module LiveDataProvider
extend self

JSON_CODES = {
:countries => 'data/iso_3166-1.json',
:languages => 'data/iso_639-2.json'
}
countries: 'data/iso_3166-1.json',
languages: 'data/iso_639-2.json'
}.freeze

TRANSLATIONS = {
:countries => 'iso_3166-1/',
:languages => 'iso_639-2/'
}
countries: 'iso_3166-1/',
languages: 'iso_639-2/'
}.freeze
REPO = "https://salsa.debian.org/iso-codes-team/iso-codes.git"
CLONE_DEST = "/tmp/i18n_data_iso_clone"

Expand All @@ -34,7 +35,7 @@ def clear_cache
raise unless $?.success?
end

private
private

def ensure_checkout
unless File.exist?(CLONE_DEST)
Expand All @@ -46,10 +47,8 @@ def ensure_checkout
def translated(type, language_code)
@translated ||= {}

@translated["#{type}_#{language_code}"] ||= begin
Hash[send("alpha_codes_for_#{type}").map do |alpha2, alpha3|
[alpha2, translate(type, alpha3, language_code) || fallback_name(type, alpha3)]
end]
@translated["#{type}_#{language_code}"] ||= send("alpha_codes_for_#{type}").transform_values do |alpha3|
translate(type, alpha3, language_code) || fallback_name(type, alpha3)
end
end

Expand All @@ -68,7 +67,7 @@ def translations(type, language_code)
begin
file_path = "#{CLONE_DEST}/#{TRANSLATIONS[type]}#{code}.po"
data = SimplePoParser.parse(file_path)
data = data[1..-1] # Remove the initial info block in the .po file
data = data[1..] # Remove the initial info block in the .po file

# Prefer the "Common name for" blocks, but fallback to "Name for" blocks
common_names = get_po_data(data, 'Common name for')
Expand All @@ -90,7 +89,7 @@ def get_po_data(data, extracted_comment_string)
# Maps over the alpha3 country code in the 'extracted_comment'
# Eg: "Name for GBR"
po_entries.map.with_object({}) do |t, translations|
alpha3 = t[:extracted_comment][-3..-1].upcase
alpha3 = t[:extracted_comment][-3..].upcase
translation = t[:msgstr]
translations[alpha3] = translation.is_a?(Array) ? translation.join : translation
end
Expand Down Expand Up @@ -118,7 +117,7 @@ def english_languages
json(:languages)['639-2'].each do |entry|
name = entry['name'].to_s
code = entry['alpha_2'].to_s.upcase
next if code.empty? or name.empty?
next if code.empty? || name.empty?
codes[code] = name
end
codes
Expand Down
3 changes: 2 additions & 1 deletion lib/i18n_data/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module I18nData
VERSION = Version = "0.17.1"
VERSION = Version = "0.17.1" # rubocop:disable Naming/ConstantName
end
11 changes: 5 additions & 6 deletions spec/benchmark.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true
# Benchmark performance by looking up every available language's country and
# language translations multiple times.

require 'benchmark'
require 'i18n_data'

types = %i(countries languages)
types = [:countries, :languages]

type_codes = {}
types.each do |type|
Expand All @@ -18,12 +19,10 @@
10.times do
type_codes.each_pair do |type, codes|
codes.each do |code|
begin
I18nData.send(type, code).keys.each do |key|
I18nData.send(type, code)[key]
end
rescue I18nData::NoTranslationAvailable
I18nData.send(type, code).each_key do |key|
I18nData.send(type, code)[key]
end
rescue I18nData::NoTranslationAvailable
end
end
end
Expand Down
Loading

0 comments on commit f556b8c

Please sign in to comment.