Skip to content

Instantly share code, notes, and snippets.

@hpyhacking
Last active February 6, 2022 13:09
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hpyhacking/4308935 to your computer and use it in GitHub Desktop.
Save hpyhacking/4308935 to your computer and use it in GitHub Desktop.
Can use index by pinyin with English language setting on iPhone.
require 'rubygems'
require 'chinese_pinyin'
name = []
cp = false
def p(z)
pinyin = Pinyin.t(z, '')
pinyin[0] = pinyin[0].upcase
pinyin
end
ARGF.readlines.each do |x|
if cp and (not (/X-PHONETIC/ =~ x))
if name and name.length == 2
puts "X-PHONETIC-FIRST-NAME:#{p(name[0])}"
puts "X-PHONETIC-LAST-NAME:#{p(name[1])}"
elsif name and name.length == 1
puts "X-PHONETIC-LAST-NAME:#{p(name[0])}"
else
raise "ERROR NAME FORMAT #{name}"
end
end
cp = (/FN:/ =~ x)
name = x.split(':')[1].split(' ') if cp
puts x
end
@hpyhacking
Copy link
Author

  1. export contacts to cards.vcf file from Concats application.
  2. execute sudo gem install chinese_pinyin
  3. execute ruby add_pinyin_to_vcard.rb cards.vcf > new_cards.vcf
  4. inport (update) new_cards.vcf to Concats application.
  5. index contacts by pinyin from English lunguage setting.

@kollway-app
Copy link

kollway-app commented Apr 24, 2017

在Mac OS X 10.12.4上运行会报:
/Library/Ruby/Gems/2.0.0/gems/chinese_pinyin-1.0.1/lib/chinese_pinyin.rb:54:in translate': undefined method fetch' for "":String (NoMethodError)

系统自带的ruby版本:ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin16]

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