]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiHiiLib/HiiLanguage.c
Add GetCurrentLanguage() to HiiLib in MdeModulePkg
[mirror_edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiLanguage.c
1 /** @file
2 Language related HII Library implementation.
3
4 Copyright (c) 2006 - 2008, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "InternalHiiLib.h"
17
18 /**
19 Get next language from language code list (with separator ';').
20
21 If LangCode is NULL, then ASSERT.
22 If Lang is NULL, then ASSERT.
23
24 @param LangCode On input: point to first language in the list. On
25 output: point to next language in the list, or
26 NULL if no more language in the list.
27 @param Lang The first language in the list.
28
29 **/
30 VOID
31 EFIAPI
32 HiiLibGetNextLanguage (
33 IN OUT CHAR8 **LangCode,
34 OUT CHAR8 *Lang
35 )
36 {
37 UINTN Index;
38 CHAR8 *StringPtr;
39
40 ASSERT (LangCode != NULL);
41 ASSERT (*LangCode != NULL);
42 ASSERT (Lang != NULL);
43
44 Index = 0;
45 StringPtr = *LangCode;
46 while (StringPtr[Index] != 0 && StringPtr[Index] != ';') {
47 Index++;
48 }
49
50 CopyMem (Lang, StringPtr, Index);
51 Lang[Index] = 0;
52
53 if (StringPtr[Index] == ';') {
54 Index++;
55 }
56 *LangCode = StringPtr + Index;
57 }
58
59
60 /**
61 This function returns the list of supported languages, in the format specified
62 in UEFI specification Appendix M.
63
64 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
65
66 @param HiiHandle The HII package list handle.
67
68 @retval !NULL The supported languages.
69 @retval NULL If Supported Languages can not be retrived.
70
71 **/
72 CHAR8 *
73 EFIAPI
74 HiiLibGetSupportedLanguages (
75 IN EFI_HII_HANDLE HiiHandle
76 )
77 {
78 EFI_STATUS Status;
79 UINTN BufferSize;
80 CHAR8 *LanguageString;
81
82 ASSERT (IsHiiHandleRegistered (HiiHandle));
83 //
84 // Collect current supported Languages for given HII handle
85 // First try allocate 4K buffer to store the current supported languages.
86 //
87 BufferSize = 0x1000;
88 LanguageString = AllocateZeroPool (BufferSize);
89 if (LanguageString == NULL) {
90 return NULL;
91 }
92
93 Status = mHiiStringProt->GetLanguages (mHiiStringProt, HiiHandle, LanguageString, &BufferSize);
94
95 if (Status == EFI_BUFFER_TOO_SMALL) {
96 FreePool (LanguageString);
97 LanguageString = AllocateZeroPool (BufferSize);
98 if (LanguageString == NULL) {
99 return NULL;
100 }
101
102 Status = mHiiStringProt->GetLanguages (mHiiStringProt, HiiHandle, LanguageString, &BufferSize);
103 }
104
105 if (EFI_ERROR (Status)) {
106 LanguageString = NULL;
107 }
108
109 return LanguageString;
110 }
111
112
113 /**
114 This function returns the number of supported languages on HiiHandle.
115
116 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
117 If not enough resource to complete the operation, then ASSERT.
118
119 @param HiiHandle The HII package list handle.
120
121 @return The number of supported languages.
122
123 **/
124 UINT16
125 EFIAPI
126 HiiLibGetSupportedLanguageNumber (
127 IN EFI_HII_HANDLE HiiHandle
128 )
129 {
130 CHAR8 *Languages;
131 CHAR8 *LanguageString;
132 UINT16 LangNumber;
133 CHAR8 Lang[RFC_3066_ENTRY_SIZE];
134
135 Languages = HiiLibGetSupportedLanguages (HiiHandle);
136 if (Languages == NULL) {
137 return 0;
138 }
139
140 LangNumber = 0;
141 LanguageString = Languages;
142 while (*LanguageString != 0) {
143 HiiLibGetNextLanguage (&LanguageString, Lang);
144 LangNumber++;
145 }
146 FreePool (Languages);
147
148 return LangNumber;
149 }
150
151 /**
152 This function returns the list of supported 2nd languages, in the format specified
153 in UEFI specification Appendix M.
154
155 If HiiHandle is not a valid Handle in the default HII database, then ASSERT.
156 If not enough resource to complete the operation, then ASSERT.
157
158 @param HiiHandle The HII package list handle.
159 @param FirstLanguage Pointer to language name buffer.
160
161 @return The supported languages.
162
163 **/
164 CHAR8 *
165 EFIAPI
166 HiiLibGetSupportedSecondaryLanguages (
167 IN EFI_HII_HANDLE HiiHandle,
168 IN CONST CHAR8 *FirstLanguage
169 )
170 {
171 EFI_STATUS Status;
172 UINTN BufferSize;
173 CHAR8 *LanguageString;
174
175 ASSERT (HiiHandle != NULL);
176 ASSERT (IsHiiHandleRegistered (HiiHandle));
177 //
178 // Collect current supported 2nd Languages for given HII handle
179 // First try allocate 4K buffer to store the current supported 2nd languages.
180 //
181 BufferSize = 0x1000;
182 LanguageString = AllocateZeroPool (BufferSize);
183 if (LanguageString == NULL) {
184 return NULL;
185 }
186
187 Status = mHiiStringProt->GetSecondaryLanguages (mHiiStringProt, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
188
189 if (Status == EFI_BUFFER_TOO_SMALL) {
190 FreePool (LanguageString);
191 LanguageString = AllocateZeroPool (BufferSize);
192 if (LanguageString == NULL) {
193 return NULL;
194 }
195
196 Status = mHiiStringProt->GetSecondaryLanguages (mHiiStringProt, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
197 }
198
199 if (EFI_ERROR (Status)) {
200 LanguageString = NULL;
201 }
202
203 return LanguageString;
204 }
205
206
207 /**
208 Determine what is the current language setting. The space reserved for Lang
209 must be at least RFC_3066_ENTRY_SIZE bytes;
210
211 If Lang is NULL, then ASSERT.
212
213 @param Lang Pointer of system language. Lang will always be filled with
214 a valid RFC 3066 language string. If "PlatformLang" is not
215 set in the system, the default language specifed by PcdUefiVariableDefaultPlatformLang
216 is returned.
217
218 @return EFI_SUCCESS If the EFI Variable with "PlatformLang" is set and return in Lang.
219 @return EFI_NOT_FOUND If the EFI Variable with "PlatformLang" is not set, but a valid default language is return in Lang.
220
221 **/
222 EFI_STATUS
223 EFIAPI
224 GetCurrentLanguage (
225 OUT CHAR8 *Lang
226 )
227 {
228 EFI_STATUS Status;
229 UINTN Size;
230
231 ASSERT (Lang != NULL);
232
233 //
234 // Get current language setting
235 //
236 Size = RFC_3066_ENTRY_SIZE;
237 Status = gRT->GetVariable (
238 L"PlatformLang",
239 &gEfiGlobalVariableGuid,
240 NULL,
241 &Size,
242 Lang
243 );
244
245 if (EFI_ERROR (Status)) {
246 AsciiStrCpy (Lang, (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang));
247 }
248
249 return Status;
250 }
251
252
253