Class: Rosette::Core::RepoConfig
- Inherits:
-
Object
- Object
- Rosette::Core::RepoConfig
- Includes:
- Integrations::Integratable
- Defined in:
- lib/rosette/core/extractor/repo_config.rb
Overview
Configuration for a single repository. Instances of RepoConfig can be configured to:
-
Extract phrases (via #add_extractor). Phrase extraction means certain files (that you specify) will be monitored for changes and processed. For example, you could specify that all files with a .yml extension be monitored. When Rosette, using git, detects that any of those files have changed, it will parse the files using an extractor and store the phrases in the datastore. The Rosette project contains a number of pre-built extractors. Visit github for a complete list: github.com/rosette-proj. For example, the yaml extractor is called rosette-extractor-yaml and is available at github.com/rosette-proj/rosette-extractor-yaml. You would need to add it to your Gemfile and require it before use.
-
Serialize phrases (via #add_serializer). Serializing phrases can be thought of as the opposite of extracting them. Instead of parsing a yaml file for example, serialization is the process of turning a collection of foreign language translations into a big string of yaml that can be written to a file. Usually serialization happens when you’re ready to export translations from Rosette. In the Rails world for example, you’d export (or serialize) translations per locale and store them as files in the config/locales directory. Spanish translations would be exported to config/locales/es.yml and Japanese translations to config/locales/ja.yml. The Rosette project contains a number of pre-built serializers. Visit github for a complete list: github.com/rosette-proj. For example, the yaml serializer is called rosette-serializer-yaml and is available at github.com/rosette-proj/rosette-serializer-yaml. You would need to add it to your Gemfile and require it before use.
-
Pre-process phrases using SerializerConfig#add_preprocessor. Serializers can also pre-process translations (see the example below). Pre-processing is the concept of modifying a translation just before it gets serialized. Examples include rosette-preprocessor-normalization, which is capable of applying Unicode’s text normalization algorithm to translation text. See github.com/rosette-proj for a complete list of pre-processors.
-
Interact with third-party libraries or services via integrations (see the Integrations::Integratable#add_integration method). Integrations are very general in that they can be almost anything. For the most part however, integrations serve as bridges to external APIs or libraries. For example, the Rosette project currently contains an integration called rosette-integration-smartling that’s responsible for pushing and pulling translations to/from the Smartling translation platform (Smartling is a translation management system, or TMS). Since Rosette is not a TMS (i.e. doesn’t provide any GUI for entering translations), you will need to use a third-party service like Smartling or build your own TMS solution. Another example is rosette-integration-rollbar. Rollbar is a third-party error reporting system. The Rollbar integration not only adds a Rosette-style ErrorReporter, it also hooks into a few places errors might happen, like Rosette::Server’s rack stack.
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
A description of the repository.
-
#extractor_configs ⇒ Array<ExtractorConfig>
readonly
A list of the currently configured extractors.
-
#hooks ⇒ Hash<Hash<Array<Proc>>>
readonly
A hash of callbacks.
-
#locales ⇒ Array<Locale>
readonly
A list of the locales this repo supports.
-
#name ⇒ String
readonly
The name of the repository.
-
#placeholder_regexes ⇒ Object
readonly
Returns the value of attribute placeholder_regexes.
-
#repo ⇒ Repo
readonly
A Repo instance that can be used to perform git operations on the local working copy of the associated git repository.
-
#rosette_config ⇒ Object
readonly
Returns the value of attribute rosette_config.
-
#serializer_configs ⇒ Array<SerializerConfig>
readonly
A list of the currently configured serializers.
-
#tms ⇒ Rosette::Tms::Repository
readonly
A repository instance from the chosen translation management system.
Instance Method Summary collapse
-
#add_extractor(extractor_id) {|config| ... } ⇒ void
Adds an extractor to this repo.
-
#add_locale(locale_code, format = Locale::DEFAULT_FORMAT) ⇒ void
Adds a locale to the list of locales this repo supports.
-
#add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) ⇒ void
Adds multiple locales to the list of locales this repo supports.
-
#add_placeholder_regex(placeholder_regex) ⇒ void
Adds a regex that matches a placeholder in translation text.
-
#add_serializer(name, options = {}) {|config| ... } ⇒ void
Adds a serializer to this repo.
-
#after(action, &block) ⇒ void
Adds an after hook.
-
#get_extractor_config(extractor_id) ⇒ nil, ExtractorConfig
Retrieves the extractor config by either name or extractor id.
-
#get_extractor_configs(path) ⇒ Array<ExtractorConfig>
Retrieves the extractor configs that match the given path.
-
#get_locale(code, format = Locale::DEFAULT_FORMAT) ⇒ nil, Locale
Retrieves the locale object by locale code.
-
#get_serializer_config(name_or_id) ⇒ nil, SerializerConfig
Retrieves the serializer config by either name or serializer id.
-
#initialize(name, rosette_config) ⇒ RepoConfig
constructor
Creates a new repo config object.
-
#path ⇒ String
Gets the path to the repository’s .git directory.
-
#set_description(desc) ⇒ void
Sets the description of the repository.
-
#set_path(path) ⇒ void
Sets the path to the repository’s .git directory.
-
#set_source_locale(code, format = Locale::DEFAULT_FORMAT) ⇒ void
Sets the source locale.
-
#source_locale ⇒ Locale
Gets the source locale (i.e. the locale all the source files are in).
-
#use_tms(tms, options = {}) ⇒ void
Set the TMS (translation management system).
Methods included from Integrations::Integratable
#add_integration, #apply_integrations, #get_integration, #integrations
Constructor Details
#initialize(name, rosette_config) ⇒ RepoConfig
Creates a new repo config object.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 118 def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end |
Instance Attribute Details
#description ⇒ String (readonly)
Returns a description of the repository.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
#extractor_configs ⇒ Array<ExtractorConfig> (readonly)
Returns a list of the currently configured extractors.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
#hooks ⇒ Hash<Hash<Array<Proc>>> (readonly)
Returns a hash of callbacks. The outer hash contains the order while the inner hash contains the action. For example, if the hooks hash has been configured to do something after commit, it might look like this:
{ after: { commit: [<Proc #0x238d3a>] } }.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
#locales ⇒ Array<Locale> (readonly)
Returns a list of the locales this repo supports.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
#name ⇒ String (readonly)
Returns the name of the repository.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
#placeholder_regexes ⇒ Object (readonly)
Returns the value of attribute placeholder_regexes.
112 113 114 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 112 def placeholder_regexes @placeholder_regexes end |
#repo ⇒ Repo (readonly)
Returns a Rosette::Core::Repo instance that can be used to perform git operations on the local working copy of the associated git repository.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
#rosette_config ⇒ Object (readonly)
Returns the value of attribute rosette_config.
110 111 112 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 110 def rosette_config @rosette_config end |
#serializer_configs ⇒ Array<SerializerConfig> (readonly)
Returns a list of the currently configured serializers.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
#tms ⇒ Rosette::Tms::Repository (readonly)
Returns a repository instance from the chosen translation management system.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 107 class RepoConfig include Integrations::Integratable attr_reader :name, :rosette_config, :repo, :locales, :hooks, :description attr_reader :extractor_configs, :serializer_configs, :tms attr_reader :placeholder_regexes # Creates a new repo config object. # # @param [String] name The name of the repository. Usually matches the # name of the directory on disk, but that's not required. def initialize(name, rosette_config) @name = name @rosette_config = rosette_config @extractor_configs = [] @serializer_configs = [] @locales = [] @placeholder_regexes = [] @hooks = Hash.new do |h, key| h[key] = Hash.new do |h2, key2| h2[key2] = [] end end end # Sets the path to the repository's .git directory. # # @param [String] path The path to the repository's .git directory. # @return [void] def set_path(path) @repo = Repo.from_path(path) end # Sets the description of the repository. This is really just for # annotation purposes, the description isn't used by Rosette. # # @param [String] desc The description text. # @return [void] def set_description(desc) @description = desc end # Gets the path to the repository's .git directory. # # @return [String] def path repo.path if repo end # Gets the source locale (i.e. the locale all the source files are in). # Defaults to en-US. # # @return [Locale] the source locale. def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end # Sets the source locale. # # @param [String] code The locale code. # @param [Symbol] format The format +locale+ is in. # @return [void] def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end # Set the TMS (translation management system). TMSs must contain a class # named +Repository+ that implements the [Rosette::Tms::Repository] # interface. # # @param [Const, String] tms The TMS to use. When this parameter is a # string, +use_tms+ will try to look up the corresponding +Tms+ # constant. If a constant is given instead, it's used without # modifications. In both cases, the +Tms+ constant will have +configure+ # called on it and is expected to yield a configuration object. # @param [Hash] options A hash of options passed to the TMS's # constructor. # @return [void] def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end # Adds an extractor to this repo. # # @param [String] extractor_id The id of the extractor you'd like to add. # @yield [config] yields the extractor config # @yieldparam config [ExtractorConfig] # @return [void] def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end # Adds a serializer to this repo. # # @param [String] name A semantic name for this serializer. Means nothing # to Rosette, simply a way for you to label the serializer. # @param [Hash] options A hash of options containing the following entries: # * +format+: The id of the serializer, eg. "yaml/rails". # @yield [config] yields the serializer config # @yieldparam config [SerializerConfig] # @return [void] def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end # Adds a locale to the list of locales this repo supports. # # @param [String] locale_code The locale you'd like to add. # @param [Symbol] format The format of +locale_code+. # @return [void] def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end # Adds multiple locales to the list of locales this repo supports. # # @param [Array<String>] locale_codes The list of locales to add. # @param [Symbol] format The format of +locale_codes+. # @return [void] def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end # Adds an after hook. You should pass a block to this method. The # block will be executed when the hook fires. # # @param [Symbol] action The action to hook. Currently the only # supported action is +:commit+. # @return [void] def after(action, &block) hooks[:after][action] << block end # Retrieves the extractor configs that match the given path. # # @param [String] path The path to match. # @return [Array<ExtractorConfig>] a list of the extractor configs that # were found to match +path+. def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end # Retrieves the extractor config by either name or extractor id. # # @param [String] name_or_id The name or extractor id. # @return [nil, ExtractorConfig] the first matching extractor config. # Potentially returns +nil+ if no matching extractor config can be # found. def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end # Retrieves the serializer config by either name or serializer id. # # @param [String] name_or_id The name or serializer id. # @return [nil, SerializerConfig] the first matching serializer config. # Potentially returns +nil+ if no matching serializer config can be # found. def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end # Retrieves the locale object by locale code. # # @param [String] code The locale code to look for. # @param [Symbol] format The locale format +code+ is in. # @return [nil, Locale] The locale who's code matches +code+. Potentially # returns +nil+ if the locale can't be found. def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end # Adds a regex that matches a placeholder in translation text. For # example, Ruby placeholders often look like this "Hello %{name}!". # Some integrations rely on these regexes to detect and format # placeholders correctly. # # @param [Regexp] placeholder_regex The regex to add. # @return [void] def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end protected def find_tms_const(name) const_str = "#{Rosette::Core::StringUtils.camelize(name)}Tms" if Rosette::Tms.const_defined?(const_str) Rosette::Tms.const_get(const_str) end end end |
Instance Method Details
#add_extractor(extractor_id) {|config| ... } ⇒ void
This method returns an undefined value.
Adds an extractor to this repo.
213 214 215 216 217 218 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 213 def add_extractor(extractor_id) klass = ExtractorId.resolve(extractor_id) extractor_configs << ExtractorConfig.new(extractor_id, klass).tap do |config| yield config if block_given? end end |
#add_locale(locale_code, format = Locale::DEFAULT_FORMAT) ⇒ void
This method returns an undefined value.
Adds a locale to the list of locales this repo supports.
242 243 244 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 242 def add_locale(locale_code, format = Locale::DEFAULT_FORMAT) add_locales(locale_code) end |
#add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) ⇒ void
This method returns an undefined value.
Adds multiple locales to the list of locales this repo supports.
251 252 253 254 255 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 251 def add_locales(locale_codes, format = Locale::DEFAULT_FORMAT) @locales += Array(locale_codes).map do |locale_code| Locale.parse(locale_code, format) end end |
#add_placeholder_regex(placeholder_regex) ⇒ void
This method returns an undefined value.
Adds a regex that matches a placeholder in translation text. For example, Ruby placeholders often look like this “Hello %#name!”. Some integrations rely on these regexes to detect and format placeholders correctly.
324 325 326 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 324 def add_placeholder_regex(placeholder_regex) placeholder_regexes << placeholder_regex end |
#add_serializer(name, options = {}) {|config| ... } ⇒ void
This method returns an undefined value.
Adds a serializer to this repo.
229 230 231 232 233 234 235 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 229 def add_serializer(name, = {}) serializer_id = [:format] klass = SerializerId.resolve(serializer_id) serializer_configs << SerializerConfig.new(name, klass, serializer_id).tap do |config| yield config if block_given? end end |
#after(action, &block) ⇒ void
This method returns an undefined value.
Adds an after hook. You should pass a block to this method. The block will be executed when the hook fires.
263 264 265 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 263 def after(action, &block) hooks[:after][action] << block end |
#get_extractor_config(extractor_id) ⇒ nil, ExtractorConfig
Retrieves the extractor config by either name or extractor id.
284 285 286 287 288 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 284 def get_extractor_config(extractor_id) extractor_configs.find do |config| config.extractor_id == extractor_id end end |
#get_extractor_configs(path) ⇒ Array<ExtractorConfig>
Retrieves the extractor configs that match the given path.
272 273 274 275 276 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 272 def get_extractor_configs(path) extractor_configs.select do |config| config.matches?(path) end end |
#get_locale(code, format = Locale::DEFAULT_FORMAT) ⇒ nil, Locale
Retrieves the locale object by locale code.
312 313 314 315 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 312 def get_locale(code, format = Locale::DEFAULT_FORMAT) locale_to_find = Locale.parse(code, format) locales.find { |locale| locale == locale_to_find } end |
#get_serializer_config(name_or_id) ⇒ nil, SerializerConfig
Retrieves the serializer config by either name or serializer id.
296 297 298 299 300 301 302 303 304 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 296 def get_serializer_config(name_or_id) found = serializer_configs.find do |config| config.name == name_or_id end found || serializer_configs.find do |config| config.serializer_id == name_or_id end end |
#path ⇒ String
Gets the path to the repository’s .git directory.
153 154 155 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 153 def path repo.path if repo end |
#set_description(desc) ⇒ void
This method returns an undefined value.
Sets the description of the repository. This is really just for annotation purposes, the description isn’t used by Rosette.
146 147 148 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 146 def set_description(desc) @description = desc end |
#set_path(path) ⇒ void
This method returns an undefined value.
Sets the path to the repository’s .git directory.
137 138 139 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 137 def set_path(path) @repo = Repo.from_path(path) end |
#set_source_locale(code, format = Locale::DEFAULT_FORMAT) ⇒ void
This method returns an undefined value.
Sets the source locale.
170 171 172 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 170 def set_source_locale(code, format = Locale::DEFAULT_FORMAT) @source_locale = Locale.parse(code, format) end |
#source_locale ⇒ Locale
Gets the source locale (i.e. the locale all the source files are in). Defaults to en-US.
161 162 163 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 161 def source_locale @source_locale ||= Locale.parse('en-US', Locale::DEFAULT_FORMAT) end |
#use_tms(tms, options = {}) ⇒ void
This method returns an undefined value.
Set the TMS (translation management system). TMSs must contain a class named Repository that implements the [Rosette::Tms::Repository] interface.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rosette/core/extractor/repo_config.rb', line 186 def use_tms(tms, = {}) const = case tms when String if const = find_tms_const(tms) const else raise ArgumentError, "'#{tms}' couldn't be found." end when Class, Module tms else raise ArgumentError, "'#{tms}' must be a String, Class, or Module." end @tms = const.configure(rosette_config, self) do |configurator| yield configurator if block_given? end nil end |