The process of changing the case in strings is not the same for all languages. For best results, use data-specific formatter objects and preset styles, described in Formatting Dates and Times and Formatting Numbers. To specify the user’s locale preference, pass as the locale parameter to either the initWithFormat:locale: or initWithFormat:locale:arguments: method. To observe locale preference changes, read Registering for Locale and Time Zone Changes. However, you are not notified if the returned object changes. If you use the autoupdatingCurrentLocale method, the property values can change when the user changes the region settings. Therefore, use the currentLocale method if you want to perform operations that need to be consistent. If you use the currentLocale method, the property values of the returned object are guaranteed not to change. You can obtain the user’s locale using either the currentLocale or autoupdatingCurrentLocale class methods in the NSLocale class. The NSLocale class provides class methods for obtaining the user’s locale object and other information about supported locales. When you format user-facing text, you pass an NSLocale object representing the user’s selected region. Using the Locale ObjectĪn NSLocale object encapsulates information about the formatting standards of a particular region. Mac users can also customize the formats of dates, times, and numbers by clicking the Advanced button, as described in Reviewing Language and Region Preferences on Your Mac. This screenshot shows sample data formats when English is the language and Japan is the region: When you choose a geographic region from the Region pop-up menu, samples of the date, time, and number formats appear. On a Mac, you can preview modified locale preferences in System Preferences. Table 4-1 Data formats in United States and Germany The day precedes the month and a 24-hour clock represents times, as shown in Table 4-1. Text appears in English but dates, times, and numbers follow German formatting rules. For example, a native English speaker who lives in Germany might select English as the language and Germany as the region. These are often the same but can be different. Locales represent the formatting choices for a particular user, not the user’s preferred language. This chapter explains how to write locale-sensitive code. You can use custom formats as long as you convert them to locale-sensitive formats before presenting them to users. You can use preset styles that automatically generate locale-sensitive formats. You do not need to know how to format data in all the different locales. Therefore, if you manipulate data objects in your code and then present them to the user, use the locale APIs to format the data correctly. The user can also change locale settings while the app is running. The user sets the locale by choosing a region in Settings on iOS devices and System Preferences on a Mac. Locale settings provide information about the formats used by the current user and must be considered when writing code that handles user-facing data types. Next Previous Formatting Data Using the Locale Settingsĭifferent countries and regions have different conventions for formatting numerical and time-based information.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |