Merge pull request #2943 from fantasticfears/slug
Add stringex for Chinese slug generation
This commit is contained in:
commit
a2ba9a735e
2
Gemfile
2
Gemfile
|
@ -245,6 +245,8 @@ gem 'memory_profiler', require: false, platform: :mri_21
|
||||||
|
|
||||||
gem 'rmmseg-cpp', require: false
|
gem 'rmmseg-cpp', require: false
|
||||||
|
|
||||||
|
gem 'stringex', require: false
|
||||||
|
|
||||||
gem 'logster'
|
gem 'logster'
|
||||||
|
|
||||||
# perftools only works on 1.9 atm
|
# perftools only works on 1.9 atm
|
||||||
|
|
|
@ -371,6 +371,7 @@ GEM
|
||||||
activesupport (>= 3.0)
|
activesupport (>= 3.0)
|
||||||
sprockets (~> 2.8)
|
sprockets (~> 2.8)
|
||||||
stackprof (0.2.7)
|
stackprof (0.2.7)
|
||||||
|
stringex (2.5.2)
|
||||||
therubyracer (0.12.1)
|
therubyracer (0.12.1)
|
||||||
libv8 (~> 3.16.14.0)
|
libv8 (~> 3.16.14.0)
|
||||||
ref
|
ref
|
||||||
|
@ -488,6 +489,7 @@ DEPENDENCIES
|
||||||
sinatra
|
sinatra
|
||||||
spork-rails
|
spork-rails
|
||||||
stackprof
|
stackprof
|
||||||
|
stringex
|
||||||
therubyracer
|
therubyracer
|
||||||
thin
|
thin
|
||||||
timecop
|
timecop
|
||||||
|
|
|
@ -15,6 +15,10 @@
|
||||||
# http://yamllint.com/
|
# http://yamllint.com/
|
||||||
|
|
||||||
en:
|
en:
|
||||||
|
stringex:
|
||||||
|
characters:
|
||||||
|
ellipsis: ""
|
||||||
|
number: "-"
|
||||||
# some default transliteration rules may be missing, add them to your locale
|
# some default transliteration rules may be missing, add them to your locale
|
||||||
i18n:
|
i18n:
|
||||||
transliterate:
|
transliterate:
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
|
||||||
module Slug
|
module Slug
|
||||||
|
|
||||||
def self.for(string)
|
def self.for(string)
|
||||||
slug = string.gsub("'", "").parameterize
|
slug = string.gsub("'", "").parameterize
|
||||||
slug.gsub!("_", "-")
|
slug.gsub!("_", "-")
|
||||||
|
if ['zh_CN', 'ja', 'ko'].include?(SiteSetting.default_locale)
|
||||||
|
unless defined? Stringex
|
||||||
|
require 'stringex_lite'
|
||||||
|
end
|
||||||
|
slug = string.to_url
|
||||||
|
end
|
||||||
slug =~ /[^\d]/ ? slug : '' # Reject slugs that only contain numbers, because they would be indistinguishable from id's.
|
slug =~ /[^\d]/ ? slug : '' # Reject slugs that only contain numbers, because they would be indistinguishable from id's.
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
zh_CN:
|
||||||
|
admin_js:
|
||||||
|
admin:
|
||||||
|
site_settings:
|
||||||
|
categories:
|
||||||
|
plugins: "插件"
|
|
@ -0,0 +1,3 @@
|
||||||
|
zh_CN:
|
||||||
|
site_settings:
|
||||||
|
enable_emoji: "启用绘文字(emoji)插件"
|
|
@ -302,10 +302,10 @@ describe Search do
|
||||||
|
|
||||||
it 'finds chinese topic based on title' do
|
it 'finds chinese topic based on title' do
|
||||||
SiteSetting.default_locale = 'zh_TW'
|
SiteSetting.default_locale = 'zh_TW'
|
||||||
topic = Fabricate(:topic, title: 'My Title Discourse社区指南')
|
topic = Fabricate(:topic, title: 'My Title Discourse社區指南')
|
||||||
post = Fabricate(:post, topic: topic)
|
post = Fabricate(:post, topic: topic)
|
||||||
|
|
||||||
Search.execute('社区指南').posts.first.id.should == post.id
|
Search.execute('社區指南').posts.first.id.should == post.id
|
||||||
Search.execute('指南').posts.first.id.should == post.id
|
Search.execute('指南').posts.first.id.should == post.id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -48,12 +48,17 @@ describe Slug do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't generate slugs that are just numbers" do
|
it "doesn't generate slugs that are just numbers" do
|
||||||
Slug.for('電車男 2').should be_blank
|
Slug.for('2').should be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't keep single quotes within word" do
|
it "doesn't keep single quotes within word" do
|
||||||
Slug.for("Jeff hate's this").should == "jeff-hates-this"
|
Slug.for("Jeff hate's this").should == "jeff-hates-this"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "translate the chineses" do
|
||||||
|
SiteSetting.default_locale = 'zh_CN'
|
||||||
|
Slug.for("习近平:中企承建港口电站等助斯里兰卡发展").should == "xi-jin-ping-zhong-qi-cheng-jian-gang-kou-dian-zhan-deng-zhu-si-li-lan-qia-fa-zhan"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -169,16 +169,22 @@ describe Category do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'non-english characters' do
|
describe 'non-english characters' do
|
||||||
let(:category) { Fabricate(:category, name: "電車男") }
|
let(:category) { Fabricate(:category, name: "测试") }
|
||||||
|
|
||||||
it "creates a blank slug, this is OK." do
|
it "creates a blank slug, this is OK." do
|
||||||
category.slug.should be_blank
|
category.slug.should be_blank
|
||||||
category.slug_for_url.should == "#{category.id}-category"
|
category.slug_for_url.should == "#{category.id}-category"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "creates a localized slug if default locale is zh_CN" do
|
||||||
|
SiteSetting.default_locale = 'zh_CN'
|
||||||
|
category.slug.should_not be_blank
|
||||||
|
category.slug_for_url.should == "ce-shi"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'slug would be a number' do
|
describe 'slug would be a number' do
|
||||||
let(:category) { Fabricate(:category, name: "電車男 2") }
|
let(:category) { Fabricate(:category, name: "2") }
|
||||||
|
|
||||||
it 'creates a blank slug' do
|
it 'creates a blank slug' do
|
||||||
category.slug.should be_blank
|
category.slug.should be_blank
|
||||||
|
|
|
@ -21,6 +21,14 @@ describe Topic do
|
||||||
Fabricate.build(:topic, title: title).slug.should == slug
|
Fabricate.build(:topic, title: title).slug.should == slug
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let(:chinese_title) { "习近平:中企承建港口电站等助斯里兰卡发展" }
|
||||||
|
let(:chinese_slug) { "xi-jin-ping-zhong-qi-cheng-jian-gang-kou-dian-zhan-deng-zhu-si-li-lan-qia-fa-zhan" }
|
||||||
|
|
||||||
|
it "returns a symbolized slug for a chinese title" do
|
||||||
|
SiteSetting.default_locale = 'zh_CN'
|
||||||
|
Fabricate.build(:topic, title: chinese_title).slug.should == chinese_slug
|
||||||
|
end
|
||||||
|
|
||||||
it "returns 'topic' when the slug is empty (say, non-english chars)" do
|
it "returns 'topic' when the slug is empty (say, non-english chars)" do
|
||||||
Slug.expects(:for).with(title).returns("")
|
Slug.expects(:for).with(title).returns("")
|
||||||
Fabricate.build(:topic, title: title).slug.should == "topic"
|
Fabricate.build(:topic, title: title).slug.should == "topic"
|
||||||
|
|
Loading…
Reference in New Issue