Skip to content

Commit

Permalink
better fast_gettext integration - do not use a combined textdomain
Browse files Browse the repository at this point in the history
remember the requested textdomain in each class instead.

This could also slightly improve start up time as the old approach
loaded _all_ available translations, not just the needed ones.
  • Loading branch information
Ladislav Slezak committed Jun 7, 2013
1 parent e9dbc63 commit 606df12
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/ruby/ycp/builtins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ def self.deletechars string, chars
return string.gsub(/[#{Regexp.escape chars}]/, "")
end

extend FastGettext::Translation
extend YCP::I18n
# Translates the text using the given text domain
def self.dgettext (domain, text)
Expand All @@ -657,6 +656,7 @@ def self.dgettext (domain, text)
return _(text)
ensure
FastGettext.text_domain = old_text_domain
textdomain old_text_domain
end

# Translates the text using a locale-aware plural form handling
Expand All @@ -666,14 +666,15 @@ def self.dngettext (domain, singular, plural, num)
return n_(singular, plural, num)
ensure
FastGettext.text_domain = old_text_domain
textdomain old_text_domain
end

# Translates the text using the given text domain and path
def self.dpgettext (domain, dirname, text)
old_text_domain = FastGettext.text_domain
FastGettext.add_text_domain(domain, :path => dirname)
FastGettext.text_domain = domain
return _(text)
return FastGettext::Translation::_(text)
ensure
FastGettext.text_domain = old_text_domain
end
Expand Down
60 changes: 27 additions & 33 deletions src/ruby/ycp/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,45 @@ module I18n
DEFAULT_LOCALE = "en_US"

def textdomain domain
# TODO FIXME:
# A single combined text domain is used for all translations
# to solve the problem with switching domain across different files
#
# FastGettext does not track which file/class uses which text domain,
# it has just single global text domain (the current one)
#
# This simple code does not work properly:
# FastGettext.add_text_domain(domain, :path => LOCALE_DIR)
# FastGettext.text_domain = domain
# it has just single global text domain (the current one),
# remember the requested text domain here
@my_textdomain = domain

# initialize available locales at first use or when the current language is changed
if FastGettext.available_locales.nil? || current_language != FastGettext.locale
available = available_locales
if FastGettext.available_locales != available
# reload the translations, a new language is available
FastGettext.translation_repositories.keys.each {|dom| FastGettext.add_text_domain(domain, :path => LOCALE_DIR)}
FastGettext.available_locales = available
end

# see https://github.com/grosser/fast_gettext#chains about the FastGettext chains
FastGettext.add_text_domain "combined", :type => :chain, :chain => combined_repositories
FastGettext.default_text_domain = "combined"

FastGettext.available_locales = available_locales
FastGettext.set_locale current_language
end
end

private

# get translation repositories for all available text domains
def combined_repositories
text_domains = []
repos = []

Dir[File.join(LOCALE_DIR, "*/LC_MESSAGES/*.mo")].each do |mofile|
text_domain = File.basename(mofile, ".mo")
# add the text domain (only if missing to avoid re-reading translations)
FastGettext.add_text_domain(domain, :path => LOCALE_DIR) unless FastGettext::translation_repositories[domain]
end

if !text_domains.include? text_domains
repos << FastGettext::TranslationRepository.build(text_domain, :path => LOCALE_DIR)
text_domains << text_domain
end
end
def _(str)
old_text_domain = FastGettext.text_domain
FastGettext.text_domain = @my_textdomain
FastGettext::Translation::_ str
ensure
FastGettext.text_domain = old_text_domain
end

repos
def n_(singular, plural, num)
old_text_domain = FastGettext.text_domain
FastGettext.text_domain = @my_textdomain
FastGettext::Translation::n_(singular, plural, num)
ensure
FastGettext.text_domain = old_text_domain
end

private

def available_locales
# the first item is used as the fallback
# when the requested locale is not available
Expand Down Expand Up @@ -77,8 +74,5 @@ def current_language
lang
end

def self.included mod
mod.send :include, FastGettext::Translation
end
end
end

0 comments on commit 606df12

Please sign in to comment.