Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Add support for yml filenames where locale is used as prefix #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 1 addition & 10 deletions lib/i18n_yaml_editor/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,7 @@ def create_missing_keys
missing_locales = self.locales - key.translations.map(&:locale)
missing_locales.each {|locale|
translation = key.translations.first

# this just replaces the locale part of the file name. should
# be possible to do in a simpler way. gsub, baby.
path = Pathname.new(translation.file)
dirs, file = path.split
file = file.to_s.split(".")
file[-2] = locale
file = file.join(".")
path = dirs.join(file).to_s

path = replace_locale_in_path(translation.locale, locale, translation.file)
new_translation = Translation.new(name: "#{locale}.#{key.name}", file: path)
add_translation(new_translation)
}
Expand Down
20 changes: 20 additions & 0 deletions lib/i18n_yaml_editor/transformation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,25 @@ def nest_hash hash
result
end
module_function :nest_hash

##
# Replaces from_locale with to_locale in filename of path
# Supports filenames with locale prefix or suffix
#
# @param [String] from_locale
# @param [String] to_locale
# @param [String] path
#
# @example Get path for en locale path from existing dk locale path
# Transformation.replace_locale_in_path('dk', 'en', '/tmp/dk.foo.yml') #=> "/tmp/en.foo.yml"
# Transformation.replace_locale_in_path('dk', 'en', '/tmp/foo.dk.yml') #=> "/tmp/foo.en.yml"
#
# @return [String]
def replace_locale_in_path(from_locale, to_locale, path)
parts = File.basename(path).split('.')
parts[parts.index(from_locale)] = to_locale
File.join(File.dirname(path), parts.join('.'))
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method extraction is fine, but what's the use case for making this public API? Would rather it was just a private method in store.rb.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also think path comes more naturally as the first argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to use IYE not only through the browser frontend, but in migration scripts etc. too. So having the nest_hash and flatten_hash methods available was pretty useful and the same applies to this new method in my opinion.
Besides, why not make it public? It is documented and tested.

module_function :replace_locale_in_path
end
end
16 changes: 16 additions & 0 deletions test/unit/test_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ def test_create_missing_translations_in_top_level_file
assert_nil translation.text
end

def test_create_missing_keys_uses_replace_locale_in_path_transformation
calls = []
@store.define_singleton_method(:replace_locale_in_path) do |*args|
calls << args
"/tmp/special_path/en.yml"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're just testing that you've stubbed a method. No go. Can't we just call the method, and assert the outputted path? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, see the two tests at unit/test_transformation.rb:45 for that.

end

@store.add_translation Translation.new(name: "da.app_name", text: "Oversætter", file: "/tmp/da.yml")
@store.add_locale("en")
@store.create_missing_keys

assert_equal(1, calls.count)
assert_equal(['da', 'en', '/tmp/da.yml'], calls.first)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see the point in testing argument positions, sounds like a bag of hurt going forward 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am testing, that the interface defined by Transformation. replace_locale_in_path is used and used correctly. Otherwise I can't be sure, that it is actually used and not being replaced with something inline, which would make my encapsulated tests obsolete.

assert_equal "/tmp/special_path/en.yml", @store.translations["en.app_name"].file
end

def test_from_yaml
input = {
da: {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/test_transformation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ def test_nest_hash

assert_equal expected, Transformation.nest_hash(input)
end

def test_replace_locale_in_path_suffixed
assert_equal '/tmp/en.foo.yml', Transformation.replace_locale_in_path('dk', 'en', '/tmp/dk.foo.yml')
end

def test_create_missing_translations_in_prefix_named_file
assert_equal '/tmp/foo.en.yml', Transformation.replace_locale_in_path('dk', 'en', '/tmp/foo.dk.yml')
end
end