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 'stringex', require: false
|
||||
|
||||
gem 'logster'
|
||||
|
||||
# perftools only works on 1.9 atm
|
||||
|
|
|
@ -371,6 +371,7 @@ GEM
|
|||
activesupport (>= 3.0)
|
||||
sprockets (~> 2.8)
|
||||
stackprof (0.2.7)
|
||||
stringex (2.5.2)
|
||||
therubyracer (0.12.1)
|
||||
libv8 (~> 3.16.14.0)
|
||||
ref
|
||||
|
@ -488,6 +489,7 @@ DEPENDENCIES
|
|||
sinatra
|
||||
spork-rails
|
||||
stackprof
|
||||
stringex
|
||||
therubyracer
|
||||
thin
|
||||
timecop
|
||||
|
|
|
@ -15,6 +15,10 @@
|
|||
# http://yamllint.com/
|
||||
|
||||
en:
|
||||
stringex:
|
||||
characters:
|
||||
ellipsis: ""
|
||||
number: "-"
|
||||
# some default transliteration rules may be missing, add them to your locale
|
||||
i18n:
|
||||
transliterate:
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
# encoding: utf-8
|
||||
|
||||
|
||||
module Slug
|
||||
|
||||
def self.for(string)
|
||||
slug = string.gsub("'", "").parameterize
|
||||
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.
|
||||
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
|
||||
SiteSetting.default_locale = 'zh_TW'
|
||||
topic = Fabricate(:topic, title: 'My Title Discourse社区指南')
|
||||
topic = Fabricate(:topic, title: 'My Title Discourse社區指南')
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
|
@ -48,12 +48,17 @@ describe Slug do
|
|||
end
|
||||
|
||||
it "doesn't generate slugs that are just numbers" do
|
||||
Slug.for('電車男 2').should be_blank
|
||||
Slug.for('2').should be_blank
|
||||
end
|
||||
|
||||
it "doesn't keep single quotes within word" do
|
||||
Slug.for("Jeff hate's this").should == "jeff-hates-this"
|
||||
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
|
||||
|
||||
|
|
|
@ -169,16 +169,22 @@ describe Category do
|
|||
end
|
||||
|
||||
describe 'non-english characters' do
|
||||
let(:category) { Fabricate(:category, name: "電車男") }
|
||||
let(:category) { Fabricate(:category, name: "测试") }
|
||||
|
||||
it "creates a blank slug, this is OK." do
|
||||
category.slug.should be_blank
|
||||
category.slug_for_url.should == "#{category.id}-category"
|
||||
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
|
||||
|
||||
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
|
||||
category.slug.should be_blank
|
||||
|
|
|
@ -21,6 +21,14 @@ describe Topic do
|
|||
Fabricate.build(:topic, title: title).slug.should == slug
|
||||
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
|
||||
Slug.expects(:for).with(title).returns("")
|
||||
Fabricate.build(:topic, title: title).slug.should == "topic"
|
||||
|
|
Loading…
Reference in New Issue