You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/locale-sensitive.Rmd
+33-39Lines changed: 33 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -16,55 +16,72 @@ knitr::opts_chunk$set(
16
16
library(stringr)
17
17
```
18
18
19
-
stringr provides a number of locale-sensitive functions, i.e. functions whose behaviour depends on your locale, of which your language is a very important part. stringr defaults to English, `locale = "en"`, but you can override by providing a different `locale` specified by a lower-case language abbreviation, optionally followed by an underscore (_) and an upper-case region identifier. For example, "en" is English, "en_GB" is British English, and "en_US" is American English. For a list of language codes see [Wikipedia](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) and to see which are supported in stringr, run `stringi::stri_locale_list()`.
19
+
A localeis a set of parameters that define a user's language, region, and cultural preferences. It determines language-specific rules for text processing, including how to:
20
20
21
-
There are three main types of function that vary based on locale:
21
+
- Convert between uppercase and lowercase letters
22
+
- Sort text alphabetically
23
+
- Format dates, numbers, and currency
24
+
- Handle character encoding and display
25
+
26
+
In stringr, you can control the locale using the `locale` argument, which takes language codes like "en" (English), "tr" (Turkish), or "es_MX" (Mexican Spanish). In general, a locale is a lower-case language abbreviation, optionally followed by an underscore (_) and an upper-case region identifier. You can see which locales are supported in stringr by running run `stringi::stri_locale_list()`.
27
+
28
+
This vignette describes locale-sensitive stringr functions, i.e. functions with a `locale` argument. These functions fall into two broad categories:
22
29
23
30
1. Case conversion
24
31
2. Sorting and ordering
25
-
3. String comparison
26
32
27
33
## Case conversion
28
34
29
-
Most languages that use the Latin alphabet (like English) have upper and lower case, but the rules for converting between the two aren't always the same. For example, Turkish has two forms of the letter "I", dotted and dotless:
35
+
`str_to_lower()`, `str_to_upper()`, `str_to_title()`, and `str_to_sentence()` all change the case of their inputs. But while most languages that use the Latin alphabet (like English) have upper and lower case, the rules for converting between the two aren't always the same. For example, Turkish has two forms of the letter "I": as well as "i" and "I", Turkish also has "ı", the dotless lowercase i, and "İ" is the dotted uppercase I. This means the rules for coverting i to upper case and I to lower case are different from English:
30
36
31
37
```{r}
32
-
str_to_upper(c("i", "ı"))
33
-
str_to_upper(c("i", "ı"), locale = "tr")
38
+
# English
39
+
str_to_upper("i")
40
+
str_to_lower("I")
34
41
35
-
str_to_lower(c("İ", "I"), locale = "tr")
42
+
# Turkish
43
+
str_to_upper("i", locale = "tr")
44
+
str_to_lower("I", locale = "tr")
36
45
```
37
46
38
-
Another example is Dutch, where "ij" is a digraph treated as a single letter. This means that `string_to_title()` will incorrectly capitalize it unless you use a Dutch locale:
47
+
Another example is Dutch, where "ij" is a digraph treated as a single letter. This means that `str_to_sentence()` will incorrectly capitalize "ij" at the start of a sentence unless you use a Dutch locale:
dutch_sentence <- "ijsland is een prachtig land in Noord-Europa."
43
52
44
-
str_to_title(dutch_words)
45
-
str_to_title(dutch_words, locale = "nl")
53
+
# Incorrect
54
+
str_to_sentence(dutch_sentence)
55
+
# Correct
56
+
str_to_sentence(dutch_sentence, locale = "nl")
46
57
```
47
58
48
-
(Note that `str_to_title()` handles character-level locale differences but it doesn't implement locale-specific rules about which words not to capitalize. Fortunately, title case appears to be concept that applies primarily to English.)
49
-
50
-
Case-sensitive string comparison also comes up in `str_equal()`/`str_unique()` and in pattern matching functions. To take advantage of locale-specific case matching, supply `locale` to `str_equal()`/`str_unique()` and use `coll()` instead of `fixed()` in pattern matching functions.
59
+
Case conversion also comes up in another situation: case-insensitive comparison. Case-insensitive comparison comes up in two places. Firstly, `str_equal()` and `str_unique()` can optionally ignore case, so it's important to also supply locale when working with non-English text. For example, imagine we're searching for a Turkish name, ignoring case:
Case conversion also comes up in pattern matching functions like `str_detect()`. You might be accustomed to use `ignore_case = TRUE` with `regex()` or `fixed()`, but if you want to use locale-sensitive comparison you instead need to use `coll()`:
Alphabetical order can vary dramatically across languages. For example, Lithuanian places 'y' between 'i' and 'k' and Czech treats "ch" as a single compound letter that sorts after all other 'h' words.
84
+
`str_sort()`, `str_order()`, and `str_rank()` all rely on the alphabetical ordering of letters. But not every language uses the same ordering as English. For example, Lithuanian places 'y' between 'i' and 'k' and Czech treats "ch" as a single compound letter that sorts after all other 'h' words. That means that if you want to correctly sort words in these languages you must provide the correct locale:
68
85
69
86
```{r}
70
87
czech_words <- c("had", "chata", "hrad", "chůze")
@@ -78,26 +95,3 @@ str_sort(lithuanian_words)
78
95
str_sort(czech_words, locale = "cs")
79
96
str_sort(lithuanian_words, locale = "lt")
80
97
```
81
-
82
-
## String comparison
83
-
84
-
Letters that appear the same can have different Unicode representations:
85
-
86
-
```{r}
87
-
name1 <- "José" # precomposed é (single character)
0 commit comments