In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of iOS text multilingual adaptation. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Multilingual adaptation and practice of Font package
Demand analysis
First of all, after understanding the product requirements and design, combined with the pain points of business R & D personnel, sort out the following requirements.
Product and design requirements
The corresponding font packages are different in different languages.
Global fonts default to the font package specified by the designer.
Font packs in some languages lack certain word weight versions and require downgrading to use the next word weight version.
There are some special copywriters that do not use global font packages (for example, Chinese, which has its own font package and is independent of the locale).
Product iteration needs to support expansion quickly and minimize R & D investment costs.
Font package resources required by the designer
Pain points and needs of research and development
There are common components (other business lines are in use, companion fish common business components currently have 50 +), and general components cannot be modified.
Only shell projects support and rely on font packages.
The source of font package resources should be flexible.
To sum up, the requirements of product and design emphasize the overall situation, diversity and expansibility of font adaptation, while R & D is concerned with decoupling, single responsibility and flexibility.
Technical design
After the analysis, the layering of the technical framework is determined.
Vertical layering and horizontal module
It is divided into three layers as shown in the picture, 1. Basic components provide core implementation and support requirements extension 2. 5. Business components (no related modifications) 3. Shell projects provide resource bundles and agents.
What is the responsibility of the FontPackage component?
FontPackageManager is responsible for binding agents to obtain resource bundles and controlling process logic.
FontPackageExtension, who is in charge of AOP, adds text attributes to meet the diversity of special scenes.
FontPackageModel, which maps the configuration information of font package resources, clarifies the use of the protocol. The upper layer business can add and adjust parameters to configure font package resources.
Resource package allocation of shell engineering
Env: international coding, default represents the default font specified by the designer. Note that some international codes represent one language, for example, English has multiple codes such as en-US, en-GB and so on, which need to be unified into en.
Font: word weight type, 0:light, 1:medium, 2:bold. Italics are replaced by medium by default
Name: the name of the font source file. For example: GothamRndSSm-Medium
Note: because the designer only requires three kinds of word weight, the default light word weight, which is not consistent with the UIFontWeight provided by the system.
/ / the configuration file in the shell project is deserialized and returned to FontPackage layer / / appfont.json {"list": [{"env": "vi", "note": "Vietnamese, according to the international code: vi, vi-VN. FontPackageManager judges international codes to correspond to "," data ": [{" font ": 0," name ":" genjyuu_light "}, {" font ": 1," name ":" genjyuu_medium (Vietnam) "} {"font": 2, "name": "genjyuu_bold"}]}, {"env": "default", "note": "other languages use fonts by default But priority is given to the international code of the device to match the font package "," data ": [{" font ": 0," name ":" GothamRndSSm-Light "}, {" font ": 1," name ":" GothamRndSSm-Medium "} {"font": 2, "name": "GothamRndSSm-Bold"}]}}
Add font packages and configuration files, as well as the cold start process:
Cold start flow chart
Technology development
There are 3 Class,200+ lines of code for the FontPackage functional component.
First, FontPackage caches the font pack resource Model that matches the language encoding according to the json configuration at cold startup.
Then use several constructors of the runtime hook UIFont class to change the fontName parameter of the constructor. Currently, five constructors have been identified:
/ / processed + (UIFont *) systemFontOfSize: (CGFloat) fontSize;+ (UIFont *) systemFontOfSize: (CGFloat) fontSize weight: (UIFontWeight) weight;+ (UIFont *) boldSystemFontOfSize: (CGFloat) fontSize;+ (UIFont *) italicSystemFontOfSize: (CGFloat) fontSize;+ (UIFont *) fontWithName: (NSString *) fontName size: (CGFloat) fontSize
Finally, it is initialized with the + fontWithName:size: function, and fontName is a custom font package.
Function-fontpackage_name: change to the corresponding custom font package according to the original fontName.
/ / FontPackageExtension.m / / UIFont+FontPackage.m+ (UIFont *) xxxFontPackage_systemFontOfSize: (CGFloat) fontSize weight: (UIFontWeight) weight {NSString * fontName = @ "; if (weight = = UIFontWeightMedium) {fontName = @" medium ";} else if (weight > UIFontWeightMedium) {fontName = @" bold ";} return [self fontWithName:fontName size:fontSize] } + (UIFont *) xxxFontPackage_italicSystemFontOfSize: (CGFloat) fontSize {/ / default is medium return [self fontWithName:@ "medium" size:fontSize];} + (UIFont *) xxxFontPackage_boldSystemFontOfSize: (CGFloat) fontSize {return [self fontWithName:@ "bold" size:fontSize];} + (UIFont *) xxxFontPackage_systemFontOfSize: (CGFloat) fontSize {return [self fontWithName:@ "" size:fontSize] } + (UIFont *) xxxFontPackage_fontWithName: (NSString *) fontName size: (CGFloat) fontSize {fontName = [self fontpackage_name:fontName]; return [self xxxFontPackage_fontWithName:fontName size:fontSize];} + (NSString *) fontpackage_name: (NSString *) fontName {fontName = [fontName lowercaseString]; FontPackageFont replaceFont = FontPackageFontLight; / / default light if ([fontName containsString:@ "medium"]) {replaceFont = FontPackageFontMedium } else if ([fontName containsString:@ "bold"]) {replaceFont = FontPackageFontBold;} / / matching replacement fonts NSString * replaceFontName = [[FontPackageManager shareInstance] .fontPackageInfo.dataMap objectForKey:@ (replaceFont)]; multilingual adaptation and practice of return replaceFontName;} text information
Language localization for overseas users is also an important product feature, but many components do not reserve the interface of localization expansion at the beginning of development, and the client needs to provide an elegant solution to deal with this problem.
Demand analysis
1. Product and design requirements
Language localization
No localized language is provided, and the product-specified language is used by default
Rapid support for localization of new languages
2. Technical requirements
Low access cost, no need to change mature components
Decoupling, other components do not need to rely on this function
Technical design
Vertical layering and horizontal module
As shown in the figure, it is divided into three layers: 1, basic components provide requirement extension 2, business components (basically do not need to be modified, if there are special attribute requirements can rely on basic components) 3, shell engineering provides resource packages and updates of resource packages
What is the responsibility of the LocalizedString component?
LocalizedString, responsible for text localization adaptation.
LocalizedTool, responsible for the configuration, reading and replacement of language packages.
LocalizedExtension, who is in charge of AOP, adds some attributes.
The language pack directory is as follows:
Language pack catalog
As you can see, the language pack is named according to the language code, so it is convenient to locate and read the corresponding file in time (there are multiple encoded languages, and their basic classes are used uniformly). At the same time, the local language pack will be refreshed in the shell project. After App starts, it will check whether a new language pack is available, and if so, it will ensure data synchronization.
After configuring the language pack, the next step is to initialize the LocalizedString component at a cold start. The flowchart of the component task at startup is as follows:
Cold start flow chart
Technology development
Considering that the string will eventually be displayed based on UILabel, [UILabel setText:] will be the only closing point for setting the display text. So we hook and extend [UILabel setText:], and its internal operation flow chart is as follows:
AOP flow chart
LocalizedString components have NSString, UILabel classification to do attribute expansion. The specific code is as follows:
@ interface UILabel (Localized) @ property (nonatomic, assign) BOOL isAutoLocalized; / < whether the text set should be automatically converted to the localized language. The default YES@end@interface NSString (Localized) @ property (nonatomic, copy) NSString * oriStr; / < the last localized string original value @ property (nonatomic, copy) NSString * localizedStr; / / < oriStr localized string @ end
The classification extension of UILabel can determine whether Label needs to be localized; the classification extension of NSString will cache the localized results, and when the same string object is localized again, you can quickly get the results from the cache to reduce the number of searches in map and improve efficiency. The way the class is expanded also ensures that the component is extremely intrusive.
The whole project uses pod for integration, the basic components do not need to declare dependencies, and those with dependency requirements for this component only appear in specific businesses. The way of hook + pod ensures the flexible use and full decoupling of this component.
Compatibility with NSLocalizedString
As you can see from the process description above, localization substitution occurs when setting text for Label, which is different from the way NSLocalizedString needs to explicitly localize the text before setting it. Therefore, when the user localizes the text in advance, the automatic localization of this component does not take effect. Considering that this component is mainly used in new language areas, NSLocalizedString has not yet configured the corresponding results, so it is still possible to use this component. We will also optimize this component later to complete the compatibility with NSLocalizedString, making it more convenient for this group to use.
Development
Since the above method is only suitable for non-intrusive adjustment in the form of [UILabel setText:], in the case of string concatenation, developers still need to use the LocalizedString class to localize the substrings one by one. At the same time, in order to support the possible intra-application change language in the future, LocalizedString also provides the function of dynamic change language pack. The main API of LocalizedString is as follows:
/ * * @ brief directly returns the key * / + (NSString *) forKey: (NSString *) key; / * * @ brief in the localized text @ param key translation file table corresponding to the specified key, and returns the key @ param langCode language code * / + (NSString *) forKey: (NSString *) key langCode: (NSString *) langCode in the localized text @ param key translation file table corresponding to key according to the specified language code. / * * @ brief sets the current default language coding @ param langCode language coding * / + (void) setCurrentLangCode: (NSString *) langCode; 's article on "sample analysis of iOS text multilingual adaptation" ends here. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.