@@ -54,9 +54,10 @@ def set(key, val)
54
54
@labels [ key ] = val
55
55
end
56
56
57
- def initialize ( lang , script , i18nyaml : nil , i18nhash : nil )
57
+ def initialize ( lang , script , locale : nil , i18nyaml : nil , i18nhash : nil )
58
58
@lang = lang
59
59
@script = script
60
+ @locale = locale
60
61
y = load_yaml ( lang , script , i18nyaml , i18nhash )
61
62
@labels = y
62
63
@labels [ "language" ] = @lang
@@ -66,17 +67,16 @@ def initialize(lang, script, i18nyaml: nil, i18nhash: nil)
66
67
end
67
68
end
68
69
69
- def self . l10n ( text , lang = @lang , script = @script )
70
- l10n ( text , lang , script )
70
+ def self . l10n ( text , lang = @lang , script = @script , locale = @locale )
71
+ l10n ( text , lang , script , locale )
71
72
end
72
73
73
- # TODO: move to localization file
74
74
# function localising spaces and punctuation.
75
75
# Not clear if period needs to be localised for zh
76
- def l10n ( text , lang = @lang , script = @script )
77
- if lang == "zh" && script == "Hans" then l10n_zh ( text )
78
- else bidiwrap ( text , lang , script )
79
- end
76
+ def l10n ( text , lang = @lang , script = @script , locale = @locale )
77
+ lang == "zh" && script == "Hans" and text = l10n_zh ( text )
78
+ lang == "fr" && text = l10n_fr ( text , locale || "FR" )
79
+ bidiwrap ( text , lang , script )
80
80
end
81
81
82
82
def bidiwrap ( text , lang , script )
@@ -107,23 +107,51 @@ def l10n_zh(text)
107
107
xml . to_xml . gsub ( /<b>/ , "" ) . gsub ( "</b>" , "" ) . gsub ( /<\? [^>]+>/ , "" )
108
108
end
109
109
110
+ def l10n_fr ( text , locale )
111
+ xml = Nokogiri ::HTML ::DocumentFragment . parse ( text )
112
+ xml . traverse do |n |
113
+ next unless n . text?
114
+
115
+ n . replace ( cleanup_entities ( l10n_fr1 ( n . text , locale ) , is_xml : false ) )
116
+ end
117
+ xml . to_xml
118
+ end
119
+
110
120
ZH_CHAR = "\\ p{Han}|\\ p{In CJK Symbols And Punctuation}|" \
111
121
"\\ p{In Halfwidth And Fullwidth Forms}" . freeze
112
122
113
123
# note: we can't differentiate comma from enumeration comma 、
114
124
def l10_zh1 ( text )
125
+ l10n_zh_remove_space ( l10n_zh_punct ( text ) )
126
+ end
127
+
128
+ def l10n_zh_punct ( text )
115
129
[ "::" , ",," , ".。" , "))" , "]】" , "::" , ";;" , "??" , "!!" ] . each do |m |
116
130
text = text . gsub ( /(?<=#{ ZH_CHAR } )#{ Regexp . quote m [ 0 ] } / , m [ 1 ] )
131
+ text = text . gsub ( /^#{ Regexp . quote m [ 0 ] } / , m [ 1 ] )
117
132
end
118
133
[ "((" , "[【" ] . each do |m |
119
134
text = text . gsub ( /#{ Regexp . quote m [ 0 ] } (?=#{ ZH_CHAR } )/ , m [ 1 ] )
120
135
end
136
+ text
137
+ end
138
+
139
+ def l10n_zh_remove_space ( text )
121
140
text . gsub ( /(?<=#{ ZH_CHAR } ) (?=#{ ZH_CHAR } )/o , "" )
122
141
. gsub ( /(?<=\d ) (?=#{ ZH_CHAR } )/o , "" )
123
142
. gsub ( /(?<=#{ ZH_CHAR } ) (?=\d )/o , "" )
124
143
. gsub ( /(?<=#{ ZH_CHAR } ) (?=[A-Za-z](#{ ZH_CHAR } |$))/o , "" )
125
144
end
126
145
146
+ def l10n_fr1 ( text , locale )
147
+ text = text . gsub ( /(?<=\p {Alnum})([»›;?!])/ , "\u202f \\ 1" )
148
+ text = text . gsub ( /^([»›;?!])/ , "\u202f \\ 1" )
149
+ text = text . gsub ( /([«‹])/ , "\\ 1\u202f " )
150
+ colonsp = locale == "CH" ? "\u202f " : "\u00a0 "
151
+ text = text . gsub ( /(?<=\p {Alnum})(:)/ , "#{ colonsp } \\ 1" )
152
+ text . gsub ( /^(:)/ , "#{ colonsp } \\ 1" )
153
+ end
154
+
127
155
def boolean_conj ( list , conn )
128
156
case list . size
129
157
when 0 then ""
0 commit comments