motion-i18n 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8c6cb8331129a88b35944dd40da95f9848724d0
4
- data.tar.gz: 70c4366f974d526c1b5cc7546ae24bf554a92071
3
+ metadata.gz: d676471d45904385e6697965801e24e5e2d9735b
4
+ data.tar.gz: f9fb82da081092ea176ea5dea420e53ae371c5d7
5
5
  SHA512:
6
- metadata.gz: a16fba8ba021cd852e56809f104a0f0bef4f3811715fdf45bce28779e2b3cada3d8a7191bd15e1fa6064b9dc8ee6b87b0084539fb782405defd4115058615223
7
- data.tar.gz: 4fc2185cfae832da2a1d8123bc05cfe1145ba4bfe5ccf0fbf28114c2967b7b642df654057fd2eacfe6b0c7862a8d77d1ff9755998c6fc70f2a5f48dbeee9b45e
6
+ metadata.gz: 0febb52ac33d19e9df01ec8ac9b54610a92921c8e227161e0a1ab18720b63a621f9c06f6a947f3c1835166f092eed315da1a738f5f039c1e7d42c9d4a32eba36
7
+ data.tar.gz: b4a62246aabf6aefb5fd533a85e873f6a4d7bdf94ed4565ee359a05ec39b642a766dc14718450ecaba1b5e83930b108c82b0ed379526e372140afe09f84d8962
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ .repl_history
2
+ build
3
+ resources/*.nib
4
+ resources/*.momd
5
+ resources/*.storyboardc
6
+ *.gem
7
+ .bundle
8
+ Gemfile.lock
9
+ pkg/*
10
+ .dat*
11
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "motion-require"
4
+
5
+ # Specify your gem's dependencies in motion-support.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # MotionI18n
2
+
3
+ Rails-style translations for RubyMotion apps.
4
+
5
+ This gem allows you to specify your RubyMotion translations as `.yml` files. The translations are automatically converted to Cocoa's native format for translations.
6
+
7
+ # Installation
8
+
9
+ ```
10
+ gem install motion-i18n
11
+ ```
12
+
13
+ and in your `Rakefile`:
14
+
15
+ ```ruby
16
+ require 'motion-i18n'
17
+ ```
18
+
19
+ or using bundler:
20
+
21
+ ```ruby
22
+ gem "motion-i18n"
23
+ ```
24
+
25
+ # Usage
26
+
27
+ Put your translations in YAML files in `config/locales/`. For each locale there should be a file named as the locale with the extension `.yml`, e.g. `en.yml`, `de.yml` etc.
28
+
29
+ ## Translations
30
+
31
+ For a guide on how the translation files should look like, see <http://guides.rubyonrails.org/i18n.html#adding-translations>
32
+
33
+ To translate a string, use `I18n.translate` or the shortcut `I18n.t` like this:
34
+
35
+ ```ruby
36
+ I18n.t("main_screen.title")
37
+ ```
38
+
39
+ To substitute variables in the string, specify the substitutions in second a hash argument
40
+
41
+ ```ruby
42
+ I18n.t("maps.directions", :from => origin, :to => destination)
43
+ ```
44
+
45
+ ## Locale
46
+
47
+ You can get the current locale by calling
48
+
49
+ ```ruby
50
+ I18n.locale
51
+ ```
52
+
53
+ ## Workflow
54
+
55
+ This gem works perfectly together with the `i18n_tools` gem:
56
+
57
+ ```
58
+ gem install i18n_tools
59
+ ```
60
+
61
+ or in `Gemfile`;
62
+
63
+ ```ruby
64
+ gem "i18n_tools", :require => "i18n_tools/tasks"
65
+ ```
66
+
67
+ The gem will give you the tasks `rake translations:missing` which shows untranslated strings as well as `rake translations:unused` which shows strings which are translated but not used in the code. For more information, see [i18n_tools](https://github.com/tkadauke/i18n_tools).
68
+
69
+ # Warning
70
+
71
+ For each locale `loc`, this gem overwrites the file `resources/loc.lproj/Localizable.strings` before building. Make sure you don't edit this file. Also, you should add these files to `.gitignore`:
72
+
73
+ ```
74
+ Localizable.strings
75
+ ```
76
+
77
+ # Forking
78
+
79
+ Feel free to fork and submit pull requests!
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env rake
2
+ $:.unshift("/Library/RubyMotion/lib")
3
+
4
+ if ENV['android']
5
+ require 'motion/project/template/android'
6
+ elsif ENV['osx']
7
+ require 'motion/project/template/osx'
8
+ else
9
+ require 'motion/project/template/ios'
10
+ end
11
+
12
+ require './lib/motion-i18n'
13
+
14
+ begin
15
+ require 'bundler'
16
+ require 'motion/project/template/gem/gem_tasks'
17
+ Bundler.require
18
+ rescue LoadError
19
+ end
20
+
21
+ Motion::Project::App.setup do |app|
22
+ # Use `rake config' to see complete project settings.
23
+ app.name = 'MotionI18n'
24
+ end
@@ -0,0 +1,7 @@
1
+ class AppDelegate
2
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
3
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
4
+ @window.rootViewController = UIViewController.alloc.init
5
+ true
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ module I18n
2
+ class << self
3
+ def translate(key, substitutions = {})
4
+ pkg_name = RMQ.app.context.getPackageName # please use RMQ for now.
5
+ id = RMQ.app.resources.getIdentifier(key.gsub(" ", "_"), "string", pkg_name)
6
+ str = RMQ.app.resources.getString(id)
7
+ str.tap do |result|
8
+ substitutions.each do |key, value|
9
+ result.gsub!("%{#{key}}", value.to_s)
10
+ end
11
+ end
12
+ end
13
+ alias_method :t, :translate
14
+
15
+ def locale
16
+ Java::Util::Locale.getDefault
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ def droid_convert(file, namespace, value)
2
+ if value.is_a?(Hash)
3
+ value.each do |key, value|
4
+ droid_convert(file, "#{namespace}.#{key}", value)
5
+ end
6
+ else
7
+ encoded_val = value.to_s.gsub("'", "&apos;").gsub('"', "&quot;").encode(:xml => :text)
8
+ encoded_name = namespace.to_s.gsub(" ", "_")
9
+ file.puts %{<string name="#{encoded_name}">#{encoded_val}</string>}
10
+ end
11
+ end
12
+
13
+ def droid_translate
14
+ require 'i18n'
15
+
16
+ files = Dir.glob("config/locales/*.yml")
17
+
18
+ I18n.load_path.clear
19
+ I18n.load_path << files
20
+
21
+ files.each do |locale_file|
22
+ locale = File.basename(locale_file).sub(".yml", "")
23
+
24
+ all_translations = I18n.backend.send(:lookup, locale.to_sym, "")
25
+ FileUtils.mkdir_p("resources/values-#{locale}")
26
+ File.open("resources/values-#{locale}/strings.xml", 'w') do |file|
27
+ file.puts '<?xml version ="1.0" encoding ="utf-8"?>'
28
+ file.puts '<resources>'
29
+ all_translations.each do |key, value|
30
+ droid_convert(file, key, value)
31
+ end
32
+ file.puts '</resources>'
33
+ end
34
+ end
35
+ # get rid of annoying warnings
36
+ FileUtils.mkdir_p("resources/values-en")
37
+ FileUtils.cp("resources/values-en/strings.xml", "resources/values/strings.xml")
38
+ end
39
+
40
+ desc "Translate (android)"
41
+ task :translate do
42
+ droid_translate
43
+ end
44
+
45
+ namespace :build do
46
+ task :simulator => :translate
47
+ task :device => :translate
48
+ end
@@ -0,0 +1,16 @@
1
+ module I18n
2
+ class << self
3
+ def translate(key, substitutions = {})
4
+ String.new((NSBundle.mainBundle.localizedStringForKey(key, value:"", table:nil))).tap do |result|
5
+ substitutions.each do |key, value|
6
+ result.gsub!("%{#{key}}", value.to_s)
7
+ end
8
+ end
9
+ end
10
+ alias t translate
11
+
12
+ def locale
13
+ NSLocale.preferredLanguages.first
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ def convert(file, namespace, value)
2
+ if value.is_a?(Hash)
3
+ value.each do |key, value|
4
+ convert(file, "#{namespace}.#{key}", value)
5
+ end
6
+ else
7
+ file.puts %{"#{namespace}" = "#{value.to_s}";}
8
+ end
9
+ end
10
+
11
+ def ios_translate
12
+ require 'i18n'
13
+
14
+ files = Dir.glob("config/locales/*.yml")
15
+
16
+ I18n.load_path.clear
17
+ I18n.load_path << files
18
+
19
+ files.each do |locale_file|
20
+ locale = File.basename(locale_file).sub(".yml", "")
21
+
22
+ all_translations = I18n.backend.send(:lookup, locale.to_sym, "")
23
+ FileUtils.mkdir_p("resources/#{locale}.lproj")
24
+ File.open("resources/#{locale}.lproj/Localizable.strings", 'w') do |file|
25
+ all_translations.each do |key, value|
26
+ convert(file, key, value)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ desc "Translate (ios)"
33
+ task :translate do
34
+ ios_translate
35
+ end
36
+
37
+ namespace :build do
38
+ task :simulator => :translate
39
+ task :device => :translate
40
+ end
@@ -0,0 +1,27 @@
1
+ require 'motion-require'
2
+
3
+ platform = Motion::Project::App.respond_to?(:template) ? Motion::Project::App.template : :ios
4
+ if platform.to_s.start_with?('ios') or platform.to_s.start_with?('osx')
5
+ platform_name = 'ios'
6
+ elsif platform.to_s.start_with?('android')
7
+ platform_name = 'android'
8
+ end
9
+
10
+ require "motion-i18n-#{platform_name}/translate"
11
+
12
+ Motion::Project::App.setup do |app|
13
+ platform_lib = File.join(File.dirname(__FILE__), "motion-i18n-#{platform_name}")
14
+ unless File.exists? platform_lib
15
+ raise "Sorry, the platform #{platform.inspect} (aka #{platform_name}) is not supported by motion-i18n"
16
+ end
17
+
18
+ # scans app.files until it finds app/ (the default)
19
+ # if found, it inserts just before those files, otherwise it will insert to
20
+ # the end of the list
21
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
22
+
23
+ Dir.glob(File.join(platform_lib, 'i18n.rb')).reverse.each do |file|
24
+ app.files.insert(insert_point, file)
25
+ end
26
+
27
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "motion-i18n"
4
+ s.version = "0.0.6"
5
+ s.authors = ["Thomas Kadauke"]
6
+ s.email = ["thomas.kadauke@googlemail.com"]
7
+ s.homepage = "https://github.com/tkadauke/motion-i18n"
8
+ s.summary = "Wrapper for translations in RubyMotion"
9
+ s.description = "Wrapper for translations in RubyMotion"
10
+
11
+ s.files = `git ls-files`.split($\)
12
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
13
+ s.require_paths = ["lib"]
14
+
15
+ s.add_dependency 'motion-require'
16
+ s.add_dependency 'i18n'
17
+ s.add_development_dependency 'rake'
18
+ end
@@ -0,0 +1,3 @@
1
+ "test.string" = "Test string";
2
+ "test.substitutions" = "%{count} substitutions";
3
+ "test.multiple_substitutions" = "%{count} substitutions %{count}";
@@ -0,0 +1,29 @@
1
+ describe "I18n" do
2
+ describe "translate" do
3
+ it "should translate string" do
4
+ I18n.translate("test.string").should == "Test string"
5
+ end
6
+
7
+ it "should return key for untranslated string" do
8
+ I18n.translate("foo.bar").should == "foo.bar"
9
+ end
10
+
11
+ it "should plug in substitutions" do
12
+ I18n.translate("test.substitutions", :count => 5).should == "5 substitutions"
13
+ end
14
+
15
+ it "substitutes multiple instances" do
16
+ I18n.translate("test.multiple_substitutions", :count => 5).should == "5 substitutions 5"
17
+ end
18
+
19
+ it "should have shortcut" do
20
+ I18n.t("test.string").should == I18n.translate("test.string")
21
+ end
22
+ end
23
+
24
+ describe "locale" do
25
+ it "should return the current locale" do
26
+ I18n.locale.should == "en-US"
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Kadauke
@@ -58,7 +58,20 @@ email:
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
- files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - app/app_delegate.rb
67
+ - lib/motion-i18n-android/i18n.rb
68
+ - lib/motion-i18n-android/translate.rb
69
+ - lib/motion-i18n-ios/i18n.rb
70
+ - lib/motion-i18n-ios/translate.rb
71
+ - lib/motion-i18n.rb
72
+ - motion-i18n.gemspec
73
+ - resources/en.lproj/Localizable.strings
74
+ - spec/motion-i18n/i18n_spec.rb
62
75
  homepage: https://github.com/tkadauke/motion-i18n
63
76
  licenses: []
64
77
  metadata: {}
@@ -82,4 +95,5 @@ rubygems_version: 2.2.2
82
95
  signing_key:
83
96
  specification_version: 4
84
97
  summary: Wrapper for translations in RubyMotion
85
- test_files: []
98
+ test_files:
99
+ - spec/motion-i18n/i18n_spec.rb