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

Support for nested locales on a per view base #27

Open
rubytastic opened this issue Jan 25, 2013 · 1 comment
Open

Support for nested locales on a per view base #27

rubytastic opened this issue Jan 25, 2013 · 1 comment

Comments

@rubytastic
Copy link

Currently Tolk does not support for storing locales on a per view/folder base.
Storing locales on a per view base makes maintaining locales much easier.
Is there a way to implement this already by some easy config change or should the rake task be rewritten for this?

By adding to application.rb this line
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*', '.{rb,yml}')]

Then storing your views like
/config/locales/views/home/en.yml
/config/locales/views/register/en.yml

If this is really not possible out of the box, I will try to create a commit that makes this possible.

@rubytastic
Copy link
Author

I Have tried to overrule the import rake task to load all locale files and refactored it to use the locale strings from the filenames.

Any ideas, contributions to make this code more solid?
The regex part seems to not fully work yet have to dive into that one

However trying to view the imported records that are listed in Tolk it gives me AR error record not found.

module Tolk
module Import
def self.included(base)
base.send :extend, ClassMethods
end

module ClassMethods

  def import_secondary_locales

    locales = Dir.glob("config/locales/**/*")

    locale_block_filter = Proc.new {
        |l| ['.', '..'].include?(l) ||
          !l.ends_with?('.yml')
    }

    locales = locales.reject(&locale_block_filter)
    locales = locales - [Tolk::Locale.primary_locale.name]

    #locales.each { |l| puts "locale file: " + l }
    locales.each { |l| import_locale(l) }

  end

  # load single file to database
  def import_locale(locale_name)

    locale = Tolk::Locale.find_or_create_by_name(locale_name)
    data = locale.read_locale_file
    return unless data

    phrases = Tolk::Phrase.all
    count = 0

    data.each do |key, value|
      phrase = phrases.detect { |p| p.key == key }

      if phrase
        translation = locale.translations.new(:text => value, :phrase => phrase)
        if translation.save
          count = count + 1
        elsif translation.errors[:variables].present?
          puts "[WARN] Key '#{key}' from '#{locale_name}' could not be saved: #{translation.errors[:variables].first}"
        end
      else
        puts "[ERROR] Key '#{key}' was found in '#{locale_name}' but #{Tolk::Locale.primary_language_name} translation is missing"
      end
    end

    puts "[INFO] Imported #{count} keys from #{locale_name}"
  end

end

# Load up every single file for import
def read_locale_file

  locale_file = "#{self.name}"

  if locale_file.match(/.*?\.(.+)\.yml$/)
    # handle thisfile.en.yml
    locale = locale_file.scan(/.*?\.(.+)\.yml$/).flatten.first
  end

  if locale_file.basename(fname, '.*').mb_chars.length == "2"
    # handle en.yml
    locale = locale_file.basename(fname, '.*')
  end

  puts "[INFO] Reading #{locale_file} for locale #{locale}"

  raise "[ERROR] Locale file #{locale_file} does not exists" unless File.exists?(locale_file)

  self.class.flat_hash(YAML::load(IO.read(locale_file))[locale.to_s])
rescue
  puts "[ERROR] File #{locale_file} expected to declare #{locale} locale, but it does not. Skipping this file."
  nil
end

end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant